Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore casing on log levels #976

Merged
merged 2 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 strings.EqualFold(logLevel, level) {
lkysow marked this conversation as resolved.
Show resolved Hide resolved
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