Skip to content

Commit

Permalink
fix: fix empty command name (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyp11 committed Oct 5, 2020
1 parent ad99bf1 commit 36fb9f5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 1 addition & 2 deletions cmd/config_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -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{
Expand Down
4 changes: 1 addition & 3 deletions cmd/config_set.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -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":
Expand Down
13 changes: 13 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"path/filepath"
"strings"

"github.com/asdine/storm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
}

0 comments on commit 36fb9f5

Please sign in to comment.