feat(init): Added a --force flag for overwriting an existing config.

This commit is contained in:
2025-11-07 11:46:14 -05:00
parent 8074f7ae6d
commit 169653d756
2 changed files with 13 additions and 11 deletions

View File

@@ -11,10 +11,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// TODO: Add --force (-f) flag.
var initCmd = &cobra.Command{ var initCmd = &cobra.Command{
Use: "init", Use: "init",
DisableFlagsInUseLine: true,
Short: "Set up envr", Short: "Set up envr",
Long: `The init command generates your initial config and saves it to Long: `The init command generates your initial config and saves it to
~/.envr/config in JSON format. ~/.envr/config in JSON format.
@@ -23,11 +21,10 @@ During setup, you will be prompted to select one or more ssh keys with which to
encrypt your databse. **Make 100% sure** that you have **a remote copy** of this encrypt your databse. **Make 100% sure** that you have **a remote copy** of this
key somewhere, otherwise your data could be lost forever.`, key somewhere, otherwise your data could be lost forever.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
force, _ := cmd.Flags().GetBool("force")
config, _ := app.LoadConfig() config, _ := app.LoadConfig()
if config != nil { if config == nil || force {
return fmt.Errorf("You have already initialized envr")
} else {
keys, err := selectSSHKeys() keys, err := selectSSHKeys()
if err != nil { if err != nil {
return fmt.Errorf("Error selecting SSH keys: %v", err) return fmt.Errorf("Error selecting SSH keys: %v", err)
@@ -43,13 +40,17 @@ key somewhere, otherwise your data could be lost forever.`,
} }
fmt.Printf("Config initialized with %d SSH key(s). You are ready to use envr.\n", len(keys)) fmt.Printf("Config initialized with %d SSH key(s). You are ready to use envr.\n", len(keys))
}
return nil return nil
} else {
return fmt.Errorf(`You have already initialized envr.
Run again with the --force flag if you want to reinitialize.
`)
}
}, },
} }
func init() { func init() {
initCmd.Flags().BoolP("force", "f", false, "Overwrite an existing config")
rootCmd.AddCommand(initCmd) rootCmd.AddCommand(initCmd)
} }

View File

@@ -12,12 +12,13 @@ encrypt your databse. **Make 100% sure** that you have **a remote copy** of this
key somewhere, otherwise your data could be lost forever. key somewhere, otherwise your data could be lost forever.
``` ```
envr init envr init [flags]
``` ```
### Options ### Options
``` ```
-f, --force Overwrite an existing config
-h, --help help for init -h, --help help for init
``` ```