From de09be08cac4ae02adfb2264351b1ce1f3eb9cff Mon Sep 17 00:00:00 2001 From: Martin Nirtl Date: Sat, 8 Feb 2020 14:40:35 +0100 Subject: [PATCH] exit automatically on survey interrupt --- internal/commands/configcmd/set.go | 24 +++----------- internal/commands/envscmd/init.go | 18 ++--------- internal/commands/envscmd/remove.go | 4 +-- internal/commands/initcmd/root.go | 16 +++------- internal/commands/profilecmd/create.go | 12 ++----- internal/commands/profilecmd/delete.go | 6 ++-- internal/commands/profilecmd/rename.go | 14 ++------ internal/commands/profilecmd/update.go | 19 ++--------- internal/commands/upcmd/root.go | 24 +++----------- internal/survey/survey.go | 44 ++++++++++++++------------ internal/utils/utils.go | 6 +--- 11 files changed, 53 insertions(+), 134 deletions(-) diff --git a/internal/commands/configcmd/set.go b/internal/commands/configcmd/set.go index d1c1ea9..32e896d 100644 --- a/internal/commands/configcmd/set.go +++ b/internal/commands/configcmd/set.go @@ -23,11 +23,7 @@ var setCmd = &cobra.Command{ fmt.Sprintf("username: %s", viper.GetString("username")), } - selected, err := survey.MultiSelect("Select config items to change", options, nil) - - if err != nil { - utils.Abort() - } + selected := survey.MultiSelect("Select config items to change", options, nil) for _, varnameRaw := range selected { varname := strings.Split(varnameRaw, ":") @@ -44,29 +40,17 @@ func init() { func setConfigVar(varname string) error { switch varname { case "hidesubcommandoutput": - hide, err := survey.Confirm("Hide output of external commands [true: show output only on error, false: always pipe output]", viper.GetBool("hidesubcommandoutput")) - - if err != nil { - utils.Abort() - } + hide := survey.Confirm("Hide output of external commands [true: show output only on error, false: always pipe output]", viper.GetBool("hidesubcommandoutput")) viper.Set("hidesubcommandoutput", hide) case "logfile": - logfile, err := survey.Input("Enter name of logfile [stored in dockma home dir]", viper.GetString("logfile")) - - if err != nil { - utils.Abort() - } + logfile := survey.Input("Enter name of logfile [stored in dockma home dir]", viper.GetString("logfile")) viper.Set("logfile", logfile) case "username": - username, err := survey.Input("Enter new username", viper.GetString("username")) - - if err != nil { - utils.Abort() - } + username := survey.Input("Enter new username", viper.GetString("username")) viper.Set("username", username) } diff --git a/internal/commands/envscmd/init.go b/internal/commands/envscmd/init.go index ff7363a..96a4f6a 100644 --- a/internal/commands/envscmd/init.go +++ b/internal/commands/envscmd/init.go @@ -39,11 +39,7 @@ var initCmd = &cobra.Command{ } } - env, err := survey.Input("Enter a name for the new environment (has to be unique)", "") - - if err != nil { - utils.Abort() - } + env = survey.Input("Enter a name for the new environment (has to be unique)", "") if env == "" { utils.Error(errors.New("Got empty string for environment name")) @@ -59,20 +55,12 @@ var initCmd = &cobra.Command{ os.Exit(0) } - autoPull, err := survey.Confirm(fmt.Sprintf("Run %sgit pull%s before %sdockma up%s", chalk.Cyan, chalk.Reset, chalk.Cyan, chalk.ResetColor), false) - - if err != nil { - utils.Abort() - } + autoPull := survey.Confirm(fmt.Sprintf("Run %sgit pull%s before %sdockma up%s", chalk.Cyan, chalk.Reset, chalk.Cyan, chalk.ResetColor), false) - proceed, err := survey.Confirm(fmt.Sprintf("Add new environment %s%s%s (location: %s)", chalk.Cyan, env, chalk.ResetColor, workingDir), true) + proceed := survey.Confirm(fmt.Sprintf("Add new environment %s%s%s (location: %s)", chalk.Cyan, env, chalk.ResetColor, workingDir), true) if !proceed { utils.Abort() - } else if err != nil { - fmt.Printf("%sError. %s%s\n", chalk.Red, err, chalk.ResetColor) - - os.Exit(0) } viper.Set(fmt.Sprintf("envs.%s.home", env), workingDir) diff --git a/internal/commands/envscmd/remove.go b/internal/commands/envscmd/remove.go index 30cc5dc..8aa84e0 100644 --- a/internal/commands/envscmd/remove.go +++ b/internal/commands/envscmd/remove.go @@ -32,9 +32,9 @@ var removeCmd = &cobra.Command{ env = utils.GetEnvironment(args[0]) } - sure, err := survey.Confirm(fmt.Sprintf("Are you sure to remove '%s'", env), false) + sure := survey.Confirm(fmt.Sprintf("Are you sure to remove '%s'", env), false) - if err != nil || !sure { + if !sure { utils.Abort() } diff --git a/internal/commands/initcmd/root.go b/internal/commands/initcmd/root.go index 1061ff6..88a4092 100644 --- a/internal/commands/initcmd/root.go +++ b/internal/commands/initcmd/root.go @@ -25,17 +25,15 @@ var InitCommand = &cobra.Command{ func initPreRunHook(cmd *cobra.Command, args []string) { if init := viper.GetTime("init"); !init.IsZero() { - proceed, err := survey.Confirm(fmt.Sprintf("%sDockma CLI has already been initialized!%s Do you want to proceed", chalk.Yellow, chalk.ResetColor), false) + proceed := survey.Confirm(fmt.Sprintf("%sDockma CLI has already been initialized!%s Do you want to proceed", chalk.Yellow, chalk.ResetColor), false) - if err != nil || !proceed { + if !proceed { utils.Abort() } } else { - accept, err := survey.Confirm(fmt.Sprintf("Dockma CLI config will be stored at: %s", viper.GetString("home")), true) + accept := survey.Confirm(fmt.Sprintf("Dockma CLI config will be stored at: %s", viper.GetString("home")), true) - if err != nil { - utils.Abort() - } else if !accept { + if !accept { fmt.Printf("Ok, you can change the config default location by setting %sDOCKMA_HOME%s environment variable.\n", chalk.Cyan, chalk.ResetColor) os.Exit(0) @@ -49,11 +47,7 @@ func initCommandHandler(cmd *cobra.Command, args []string) { username = sysUser.Username } - username, err := survey.Input("What is your name", strings.Title(username)) - - if err != nil { - utils.Abort() - } + username = survey.Input("What is your name", strings.Title(username)) viper.Set("username", username) viper.Set("init", time.Now()) diff --git a/internal/commands/profilecmd/create.go b/internal/commands/profilecmd/create.go index 72bdd53..65c4a30 100644 --- a/internal/commands/profilecmd/create.go +++ b/internal/commands/profilecmd/create.go @@ -22,11 +22,7 @@ var createCmd = &cobra.Command{ activeEnv := config.GetActiveEnv() envHomeDir := config.GetEnvHomeDir(activeEnv) - profileName, err := survey.Input("Enter name for profile", "") - - if err != nil { - utils.Abort() - } + profileName := survey.Input("Enter name for profile", "") if config.HasProfileName(activeEnv, profileName) { utils.Error(errors.New("Profile name already taken. Use 'update' to reselect services")) @@ -43,11 +39,7 @@ var createCmd = &cobra.Command{ utils.Error(errors.New("Could not read services")) } - selected, err := survey.MultiSelect(fmt.Sprintf("Select services for profile %s%s%s", chalk.Cyan, profileName, chalk.ResetColor), services.All, nil) - - if err != nil { - utils.Abort() - } + selected := survey.MultiSelect(fmt.Sprintf("Select services for profile %s%s%s", chalk.Cyan, profileName, chalk.ResetColor), services.All, nil) if len(selected) == 0 { fmt.Printf("%sNo services selected%s\n\n", chalk.Yellow, chalk.ResetColor) diff --git a/internal/commands/profilecmd/delete.go b/internal/commands/profilecmd/delete.go index e911d92..17494f9 100644 --- a/internal/commands/profilecmd/delete.go +++ b/internal/commands/profilecmd/delete.go @@ -28,9 +28,7 @@ var deleteCmd = &cobra.Command{ os.Exit(0) } - profileName, err := survey.Select("Select profile to be deleted", profileNames) - - utils.Error(err) + profileName := survey.Select("Select profile to be deleted", profileNames) profileMap := viper.GetStringMap(fmt.Sprintf("envs.%s.profiles", activeEnv)) @@ -38,7 +36,7 @@ var deleteCmd = &cobra.Command{ viper.Set(fmt.Sprintf("envs.%s.profiles", activeEnv), profileMap) - err = config.Save() + err := config.Save() utils.Error(err) diff --git a/internal/commands/profilecmd/rename.go b/internal/commands/profilecmd/rename.go index f0e8681..28d3695 100644 --- a/internal/commands/profilecmd/rename.go +++ b/internal/commands/profilecmd/rename.go @@ -29,17 +29,9 @@ var renameCmd = &cobra.Command{ os.Exit(0) } - renameProfile, err := survey.Select("Select profile to update", profileNames) + renameProfile := survey.Select("Select profile to update", profileNames) - if err != nil { - utils.Abort() - } - - profileName, err := survey.Input("Enter name for profile", renameProfile) - - if err != nil { - utils.Abort() - } + profileName := survey.Input("Enter name for profile", renameProfile) // FIXME use regex if profileName == "" || profileName == "-" { @@ -60,7 +52,7 @@ var renameCmd = &cobra.Command{ viper.Set(fmt.Sprintf("envs.%s.profiles", activeEnv), profileMap) - err = config.Save() + err := config.Save() utils.Error(err) diff --git a/internal/commands/profilecmd/update.go b/internal/commands/profilecmd/update.go index c91885b..4e844da 100644 --- a/internal/commands/profilecmd/update.go +++ b/internal/commands/profilecmd/update.go @@ -1,7 +1,6 @@ package profilecmd import ( - "errors" "fmt" "os" @@ -31,27 +30,15 @@ var updateCmd = &cobra.Command{ os.Exit(0) } - profileName, err := survey.Select("Select profile to update", profileNames) - - if err != nil { - utils.Abort() - } + profileName := survey.Select("Select profile to update", profileNames) services, err := dockercompose.GetServices(envHomeDir) - - if err != nil { - utils.Error(errors.New("Could not read services")) - } + utils.Error(err) profile, err := config.GetProfile(activeEnv, profileName) - utils.Error(err) - selected, err := survey.MultiSelect(fmt.Sprintf("Select services for profile %s%s%s", chalk.Cyan, profileName, chalk.ResetColor), services.All, profile.Selected) - - if err != nil { - utils.Abort() - } + selected := survey.MultiSelect(fmt.Sprintf("Select services for profile %s%s%s", chalk.Cyan, profileName, chalk.ResetColor), services.All, profile.Selected) if len(selected) == 0 { fmt.Printf("%sNo services selected%s\n\n", chalk.Yellow, chalk.ResetColor) diff --git a/internal/commands/upcmd/root.go b/internal/commands/upcmd/root.go index 681a69f..cc82c72 100644 --- a/internal/commands/upcmd/root.go +++ b/internal/commands/upcmd/root.go @@ -53,11 +53,7 @@ var UpCommand = &cobra.Command{ if len(profileNames) > 0 { profileNames = append(profileNames, "latest") - profileName, err := survey.Select(fmt.Sprintf("Select profile to run or %slatest%s", chalk.Cyan, chalk.ResetColor), profileNames) - - if err != nil { - utils.Abort() - } + profileName := survey.Select(fmt.Sprintf("Select profile to run or %slatest%s", chalk.Cyan, chalk.ResetColor), profileNames) if profileName != "latest" { profile, err := config.GetProfile(activeEnv, profileName) @@ -86,25 +82,13 @@ var UpCommand = &cobra.Command{ fmt.Printf("%sFound %d services in docker-compose.override.y(a)ml: %s%s\n\n", chalk.Yellow, len(services.Override), strings.Join(services.Override, ", "), chalk.ResetColor) } - selectedServices, err := survey.MultiSelect("Select services to start", services.All, preselected) - - if err != nil { - utils.Abort() - } + selectedServices := survey.MultiSelect("Select services to start", services.All, preselected) if profileName == "latest" { - saveProfile, err := survey.Confirm("Save as profile", false) - - if err != nil { - utils.Abort() - } + saveProfile := survey.Confirm("Save as profile", false) if saveProfile { - profileName, err := survey.Input("Enter profile name", "") - - if err != nil { - utils.Abort() - } + profileName = survey.Input("Enter profile name", "") viper.Set(fmt.Sprintf("envs.%s.profiles.%s", activeEnv, profileName), selectedServices) } else { diff --git a/internal/survey/survey.go b/internal/survey/survey.go index c25e87e..e357ba1 100644 --- a/internal/survey/survey.go +++ b/internal/survey/survey.go @@ -2,41 +2,49 @@ package survey import ( "fmt" + "os" "github.com/AlecAivazis/survey/v2" + "github.com/AlecAivazis/survey/v2/terminal" ) +func checkError(err error) { + if err == terminal.InterruptErr { + fmt.Println("Interrupted.") + + os.Exit(0) + } else if err != nil { + panic(err) + } +} + // Confirm abstracts survey's confirm and adds styling -func Confirm(message string, preselected bool) (confirm bool, err error) { - err = survey.AskOne(&survey.Confirm{ +func Confirm(message string, preselected bool) (confirm bool) { + err := survey.AskOne(&survey.Confirm{ Message: message, Default: preselected, }, &confirm) - if err != nil { - return false, fmt.Errorf("confirm got interrupted") - } + checkError(err) return } // Input abstracts survey's input and adds styling -func Input(message string, suggestion string) (response string, err error) { - err = survey.AskOne(&survey.Input{ +func Input(message string, suggestion string) (response string) { + err := survey.AskOne(&survey.Input{ Message: message, Default: suggestion, }, &response) - if err != nil { - return "", fmt.Errorf("input got interrupted") - } + checkError(err) return } // Select abstracts survey's select and adds styling -func Select(message string, options []string) (selection string, err error) { - err = survey.AskOne(&survey.Select{ +func Select(message string, options []string) (selection string) { + err := survey.AskOne(&survey.Select{ Message: message, Options: options, }, &selection, survey.WithIcons(func(icons *survey.IconSet) { @@ -45,16 +53,14 @@ func Select(message string, options []string) (selection string, err error) { icons.SelectFocus.Format = "cyan+b" })) - if err != nil { - return "", fmt.Errorf("select got interrupted") - } + checkError(err) return } // MultiSelect abstracts survey's multiselect and adds styling -func MultiSelect(message string, options []string, preselected []string) (selection []string, err error) { - err = survey.AskOne(&survey.MultiSelect{ +func MultiSelect(message string, options []string, preselected []string) (selection []string) { + err := survey.AskOne(&survey.MultiSelect{ Message: message, Options: options, Default: preselected, @@ -68,9 +74,7 @@ func MultiSelect(message string, options []string, preselected []string) (select icons.SelectFocus.Format = "cyan+b" })) - if err != nil { - return nil, fmt.Errorf("multiselect got interrupted") - } + checkError(err) return } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index cf1f8b1..5108b0a 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -61,11 +61,7 @@ func GetEnvironment(env string) string { fmt.Printf("%sNo such environment: %s%s\n", chalk.Yellow, env, chalk.ResetColor) - env, err := survey.Select("Choose an environment", envs) - - if err != nil || env == "" { - Abort() - } + env = survey.Select("Choose an environment", envs) return env }