From b0f49e5d47786984e24731b200d3d3d7d6add263 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 18 May 2021 17:52:14 +0200 Subject: [PATCH] safcm: add ParseLogLevel() --- cmd/safcm/sync.go | 19 +++---------------- log.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/cmd/safcm/sync.go b/cmd/safcm/sync.go index 5776264..10edc68 100644 --- a/cmd/safcm/sync.go +++ b/cmd/safcm/sync.go @@ -66,22 +66,9 @@ func MainSync(args []string) error { flag.CommandLine.Parse(args[2:]) - var level safcm.LogLevel - switch *optionLog { - case "error": - level = safcm.LogError - case "info": - level = safcm.LogInfo - case "verbose": - level = safcm.LogVerbose - case "debug": - level = safcm.LogDebug - case "debug2": - level = safcm.LogDebug2 - case "debug3": - level = safcm.LogDebug3 - default: - return fmt.Errorf("invalid -log value %q", *optionLog) + level, err := safcm.ParseLogLevel(*optionLog) + if err != nil { + return fmt.Errorf("-log: %v", err) } names := flag.Args() diff --git a/log.go b/log.go index 029d975..6e063ce 100644 --- a/log.go +++ b/log.go @@ -17,6 +17,10 @@ package safcm +import ( + "fmt" +) + // LogLevel controls the number of displayed log messages. Higher levels // include all messages from lower levels (e.g. LogVerbose includes all // messages from LogInfo). @@ -37,3 +41,24 @@ const ( // Log all RPC messages LogDebug3 ) + +func ParseLogLevel(s string) (LogLevel, error) { + var x LogLevel + switch s { + case "error": + x = LogError + case "info": + x = LogInfo + case "verbose": + x = LogVerbose + case "debug": + x = LogDebug + case "debug2": + x = LogDebug2 + case "debug3": + x = LogDebug3 + default: + return 0, fmt.Errorf("invalid LogLevel %q", s) + } + return x, nil +} -- 2.45.2