diff --git a/internal/namespaces/init/init.go b/internal/namespaces/init/init.go index f52377fdc6..7bd4fabae3 100644 --- a/internal/namespaces/init/init.go +++ b/internal/namespaces/init/init.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "path" "reflect" "github.com/fatih/color" @@ -65,6 +66,7 @@ type initArgs struct { SendTelemetry *bool WithSSHKey *bool InstallAutocomplete *bool + RemoveV1Config *bool } func initCommand() *core.Command { @@ -104,6 +106,10 @@ func initCommand() *core.Command { Name: "install-autocomplete", Short: "Whether the autocomplete script should be installed during initialisation", }, + { + Name: "remove-v1-config", + Short: "Whether to remove the v1 configuration file if it exists", + }, core.ZoneArgSpec(), }, SeeAlsos: []*core.SeeAlso{ @@ -233,6 +239,23 @@ func initCommand() *core.Command { args.InstallAutocomplete = scw.BoolPtr(installAutocomplete) } + // Ask whether to remove v1 configuration file if it exists + homeDir, err := os.UserHomeDir() + if err == nil { + configPath := path.Join(homeDir, ".scwrc") + if _, err := os.Stat(configPath); err == nil { + removeV1ConfigFile, err := interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{ + Prompt: "Do you want to permanently remove old configuration file (" + configPath + ")?", + DefaultValue: false, + }) + if err != nil { + return err + } + + args.RemoveV1Config = &removeV1ConfigFile + } + } + return nil }, Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) { @@ -299,6 +322,15 @@ func initCommand() *core.Command { } } + // Remove old configuration file + if args.RemoveV1Config != nil && *args.RemoveV1Config { + homeDir, _ := os.UserHomeDir() + err = os.Remove(path.Join(homeDir, ".scwrc")) + if err != nil { + successDetails += "\n except for removing old configuration: " + err.Error() + } + } + _, _ = interactive.Println() return &core.SuccessResult{