From 36fb9f562a2c005ca4390fdebde0b4690201dff9 Mon Sep 17 00:00:00 2001 From: Daniel Pham Date: Mon, 5 Oct 2020 00:52:27 -0700 Subject: [PATCH] fix: fix empty command name (#1106) --- cmd/config_init.go | 3 +-- cmd/config_set.go | 4 +--- cmd/utils.go | 13 +++++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/config_init.go b/cmd/config_init.go index 86a69914..584d382e 100644 --- a/cmd/config_init.go +++ b/cmd/config_init.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "strings" "github.com/spf13/cobra" @@ -32,7 +31,7 @@ override the options.`, s := &settings.Settings{ Key: generateKey(), Signup: mustGetBool(flags, "signup"), - Shell: strings.Split(strings.TrimSpace(mustGetString(flags, "shell")), " "), + Shell: convertCmdStrToCmdArray(mustGetString(flags, "shell")), AuthMethod: authMethod, Defaults: defaults, Branding: settings.Branding{ diff --git a/cmd/config_set.go b/cmd/config_set.go index 9b49b234..e959bc97 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -1,8 +1,6 @@ package cmd import ( - "strings" - "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -50,7 +48,7 @@ you want to change. Other options will remain unchanged.`, case "auth.method": hasAuth = true case "shell": - set.Shell = strings.Split(strings.TrimSpace(mustGetString(flags, flag.Name)), " ") + set.Shell = convertCmdStrToCmdArray(mustGetString(flags, flag.Name)) case "branding.name": set.Branding.Name = mustGetString(flags, flag.Name) case "branding.disableExternal": diff --git a/cmd/utils.go b/cmd/utils.go index bd5ec2f3..f0ff8a26 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -7,6 +7,7 @@ import ( "log" "os" "path/filepath" + "strings" "github.com/asdine/storm" "github.com/spf13/cobra" @@ -178,3 +179,15 @@ func cleanUpMapValue(v interface{}) interface{} { return v } } + +// convertCmdStrToCmdArray checks if cmd string is blank (whitespace included) +// then returns empty string array, else returns the splitted word array of cmd. +// This is to ensure the result will never be []string{""} +func convertCmdStrToCmdArray(cmd string) []string { + var cmdArray []string + trimmedCmdStr := strings.TrimSpace(cmd) + if trimmedCmdStr != "" { + cmdArray = strings.Split(trimmedCmdStr, " ") + } + return cmdArray +}