Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(init): ask to remove v1 config #836

Merged
merged 4 commits into from
Apr 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
jerome-quere marked this conversation as resolved.
Show resolved Hide resolved
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