Skip to content

Commit

Permalink
feat(init): ask to remove v1 config (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermoumoute authored Apr 3, 2020
1 parent 29efee9 commit db6073d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/namespaces/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path"
"reflect"

"github.com/fatih/color"
Expand Down Expand Up @@ -65,6 +66,7 @@ type initArgs struct {
SendTelemetry *bool
WithSSHKey *bool
InstallAutocomplete *bool
RemoveV1Config *bool
}

func initCommand() *core.Command {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit db6073d

Please sign in to comment.