feat(config): add config init subcommand with --new flag
This commit is contained in:
parent
6bba227654
commit
db607ac696
2 changed files with 55 additions and 0 deletions
|
|
@ -2,8 +2,11 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -64,8 +67,50 @@ var configPathCmd = &cobra.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeConfigFile(cfg Config) error {
|
||||||
|
p, err := configPath()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot determine config path: %w", err)
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll(filepath.Dir(p), 0o750); err != nil {
|
||||||
|
return fmt.Errorf("cannot create config directory: %w", err)
|
||||||
|
}
|
||||||
|
f, err := os.Create(p)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot write config: %w", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
enc := toml.NewEncoder(f)
|
||||||
|
return enc.Encode(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
var configInitCmd = &cobra.Command{
|
||||||
|
Use: "init",
|
||||||
|
Short: "Generate default config file",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
SilenceUsage: true,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
p, err := configPath()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot determine config path: %w", err)
|
||||||
|
}
|
||||||
|
newFlag, _ := cmd.Flags().GetBool("new")
|
||||||
|
if !newFlag {
|
||||||
|
if _, err := os.Stat(p); err == nil {
|
||||||
|
return withHint(
|
||||||
|
fmt.Errorf("config file already exists"),
|
||||||
|
"use 'pda config edit' or 'pda config init --new'",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return writeConfigFile(defaultConfig())
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
configInitCmd.Flags().Bool("new", false, "overwrite existing config file")
|
||||||
configCmd.AddCommand(configGetCmd)
|
configCmd.AddCommand(configGetCmd)
|
||||||
|
configCmd.AddCommand(configInitCmd)
|
||||||
configCmd.AddCommand(configListCmd)
|
configCmd.AddCommand(configListCmd)
|
||||||
configCmd.AddCommand(configPathCmd)
|
configCmd.AddCommand(configPathCmd)
|
||||||
rootCmd.AddCommand(configCmd)
|
rootCmd.AddCommand(configCmd)
|
||||||
|
|
|
||||||
10
testdata/config-init.ct
vendored
Normal file
10
testdata/config-init.ct
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Init creates a config file
|
||||||
|
$ pda config init
|
||||||
|
|
||||||
|
# Second init fails
|
||||||
|
$ pda config init --> FAIL
|
||||||
|
FAIL config file already exists
|
||||||
|
hint use 'pda config edit' or 'pda config init --new'
|
||||||
|
|
||||||
|
# Init --new overwrites
|
||||||
|
$ pda config init --new
|
||||||
Loading…
Add table
Add a link
Reference in a new issue