Skip to content

Commit

Permalink
chore: add normalizeStringFlag helper func, improve flag handling
Browse files Browse the repository at this point in the history
intended for use with flags that accept a custom string input, but which
are used/mapped to a specific value. there are lots of flags which do
not need this normalize because they are _intended_ to be accepted and
used literally (inventory path, hostname, reload interval, etc). but the
flags here accept a custom string input which maps to a log level or log
format, respectively. adding this func standardizes that normalization.
  • Loading branch information
tjhop committed Aug 4, 2024
1 parent 1176a5c commit 1ae59d5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cmd/mango/mango.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func main() {
}

// parse log level from flag
logLevelFlagVal := strings.TrimSpace(strings.ToLower(viper.GetString("logging.level")))
logLevelFlagVal := normalizeStringFlag(viper.GetString("logging.level"))
switch logLevelFlagVal {
case "info": // default is info, we're good
case "warn":
Expand All @@ -485,7 +485,7 @@ func main() {
metricMangoRuntimeInfo.With(metricMangoRuntimeInfoLabels).Set(1)

// parse log output format from flag
logOutputFormat := strings.TrimSpace(strings.ToLower(viper.GetString("logging.output")))
logOutputFormat := normalizeStringFlag(viper.GetString("logging.output"))
if logOutputFormat == "json" {
jsonLogHandler := slog.NewJSONHandler(os.Stdout, logHandlerOpts)
logger = slog.New(jsonLogHandler)
Expand Down Expand Up @@ -543,3 +543,7 @@ func main() {
// run mango daemon
mango(rootCtx, mainLogger, inventoryPath, me)
}

func normalizeStringFlag(s string) string {
return strings.TrimSpace(strings.ToLower(s))
}

0 comments on commit 1ae59d5

Please sign in to comment.