Skip to content

Commit

Permalink
Merge pull request #976 from jpreese/valid-loglevels
Browse files Browse the repository at this point in the history
Ignore casing on log levels
  • Loading branch information
lkysow committed Apr 6, 2020
2 parents 9edca4f + f51be62 commit df357bd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
20 changes: 17 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ var intFlags = map[string]intFlag{
},
}

// ValidLogLevels are the valid log levels that can be set
var ValidLogLevels = []string{"debug", "info", "warn", "error"}

type stringFlag struct {
description string
defaultValue string
Expand Down Expand Up @@ -494,10 +497,11 @@ func (s *ServerCmd) setDefaults(c *server.UserConfig) {
}

func (s *ServerCmd) validate(userConfig server.UserConfig) error {
logLevel := userConfig.LogLevel
if logLevel != "debug" && logLevel != "info" && logLevel != "warn" && logLevel != "error" {
return errors.New("invalid log level: not one of debug, info, warn, error")
userConfig.LogLevel = strings.ToLower(userConfig.LogLevel)
if !isValidLogLevel(userConfig.LogLevel) {
return fmt.Errorf("invalid log level: must be one of %v", ValidLogLevels)
}

checkoutStrategy := userConfig.CheckoutStrategy
if checkoutStrategy != "branch" && checkoutStrategy != "merge" {
return errors.New("invalid checkout strategy: not one of branch or merge")
Expand Down Expand Up @@ -693,3 +697,13 @@ func (s *ServerCmd) withErrPrint(f func(*cobra.Command, []string) error) func(*c
func (s *ServerCmd) printErr(err error) {
fmt.Fprintf(os.Stderr, "%sError: %s%s\n", "\033[31m", err.Error(), "\033[39m")
}

func isValidLogLevel(level string) bool {
for _, logLevel := range ValidLogLevels {
if logLevel == level {
return true
}
}

return false
}
37 changes: 30 additions & 7 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,36 @@ func TestExecute_RepoWhitelistScheme(t *testing.T) {
}

func TestExecute_ValidateLogLevel(t *testing.T) {
t.Log("Should validate log level.")
c := setupWithDefaults(map[string]interface{}{
LogLevelFlag: "invalid",
})
err := c.Execute()
Assert(t, err != nil, "should be an error")
Equals(t, "invalid log level: not one of debug, info, warn, error", err.Error())
cases := []struct {
description string
flags map[string]interface{}
expectError bool
}{
{
"log level is invalid",
map[string]interface{}{
LogLevelFlag: "invalid",
},
true,
},
{
"log level is valid uppercase",
map[string]interface{}{
LogLevelFlag: "DEBUG",
},
false,
},
}
for _, testCase := range cases {
t.Log("Should validate log level when " + testCase.description)
c := setupWithDefaults(testCase.flags)
err := c.Execute()
if testCase.expectError {
Assert(t, err != nil, "should be an error")
} else {
Ok(t, err)
}
}
}

func TestExecute_ValidateCheckoutStrategy(t *testing.T) {
Expand Down

0 comments on commit df357bd

Please sign in to comment.