Merge pull request #976 from jpreese/valid-loglevels

Ignore casing on log levels
This commit is contained in:
Luke Kysow
2020-04-06 12:14:44 -07:00
committed by GitHub
2 changed files with 47 additions and 10 deletions

View File

@@ -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
@@ -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")
@@ -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
}

View File

@@ -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) {