Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
exit automatically on survey interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 8, 2020
1 parent a1d7809 commit de09be0
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 134 deletions.
24 changes: 4 additions & 20 deletions internal/commands/configcmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ":")
Expand All @@ -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)
}
Expand Down
18 changes: 3 additions & 15 deletions internal/commands/envscmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/envscmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
16 changes: 5 additions & 11 deletions internal/commands/initcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down
12 changes: 2 additions & 10 deletions internal/commands/profilecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions internal/commands/profilecmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ 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))

profileMap[profileName] = nil

viper.Set(fmt.Sprintf("envs.%s.profiles", activeEnv), profileMap)

err = config.Save()
err := config.Save()

utils.Error(err)

Expand Down
14 changes: 3 additions & 11 deletions internal/commands/profilecmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "-" {
Expand All @@ -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)

Expand Down
19 changes: 3 additions & 16 deletions internal/commands/profilecmd/update.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package profilecmd

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 4 additions & 20 deletions internal/commands/upcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
44 changes: 24 additions & 20 deletions internal/survey/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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
}
6 changes: 1 addition & 5 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit de09be0

Please sign in to comment.