From 11d9fa0d6314af29c2fd4b77b4b6e17f58196253 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 21 Nov 2023 20:36:20 -0800 Subject: [PATCH] use new logger and envhelp command (#24) * use new logger and envhelp comamnd * latest from PR * latest log * use release version for log * linter fix --- cli.go | 30 +++++++++++++++++++++++------- go.mod | 4 +++- go.sum | 6 ++++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/cli.go b/cli.go index e4ba0c7..91fd39a 100644 --- a/cli.go +++ b/cli.go @@ -57,6 +57,8 @@ var ( BeforeFlagParseHook = func() {} // Calculated base exe name from args (will be used if ProgramName if not set). baseExe string + // List of functions to call for env help. + EnvHelpFuncs = []func(w io.Writer){log.EnvHelp} ) // ChangeFlagsDefault sets some flags to a different default. @@ -92,7 +94,7 @@ func usage(w io.Writer, msg string, args ...any) { } _, _ = fmt.Fprintf(w, log.Colors.Reset+"%s %s usage:\n\t%s %s["+ log.Colors.Cyan+"flags"+log.Colors.Reset+"]%s\nor 1 of the special arguments\n\t%s {"+ - ColorJoin(log.Colors.Purple, "help", "version", "buildinfo")+"}\n"+"flags:\n"+log.Colors.Cyan, + ColorJoin(log.Colors.Purple, "help", "envhelp", "version", "buildinfo")+"}\n"+"flags:\n"+log.Colors.Cyan, ProgramName, log.Colors.Blue+ShortVersion+log.Colors.Reset, baseExe, @@ -114,6 +116,13 @@ func usage(w io.Writer, msg string, args ...any) { } } +func EnvHelp(w io.Writer) { + fmt.Println("# Environment variables recognized and current values:") + for _, f := range EnvHelpFuncs { + f(w) + } +} + // Main handles your commandline and flag parsing. Sets up flags first then call Main. // For a server with dynamic flags, call ServerMain instead. // Will either have called [ExitFunction] (defaults to [os.Exit]) @@ -156,17 +165,20 @@ func Main() { BeforeFlagParseHook() nArgs := len(os.Args) if nArgs == 2 { + specialCmd := true switch strings.ToLower(os.Args[1]) { case "version": fmt.Println(ShortVersion) - ExitFunction(0) - return // not typically reached, unless ExitFunction doesn't exit case "buildinfo": fmt.Print(FullVersion) - ExitFunction(0) - return // not typically reached, unless ExitFunction doesn't exit case "help": usage(os.Stdout, "") + case "envhelp": + EnvHelp(os.Stdout) + default: + specialCmd = false + } + if specialCmd { ExitFunction(0) return // not typically reached, unless ExitFunction doesn't exit } @@ -183,7 +195,11 @@ func Main() { os.Stderr.WriteString(log.Colors.BrightRed) flag.Parse() os.Stderr.WriteString(log.Colors.Reset) - log.Config.ConsoleColor = !*nocolor + if *nocolor { + // Don't override the env if the flag isn't set + // (downside is if LOGGER_FORCE_COLOR is set to false, this -logger-no-color=false can't override it) + log.Config.ForceColor = !*nocolor + } log.SetColorMode() nArgs = len(flag.Args()) argsRange := (MinArgs != MaxArgs) @@ -197,7 +213,7 @@ func Main() { } if MaxArgs >= 0 && nArgs > MaxArgs { if MaxArgs <= 0 { - ErrUsage("No arguments expected (except for version, buildinfo or help and -flags), got %d", nArgs) + ErrUsage("No arguments expected (except for version, buildinfo, help or envhelp and -flags), got %d", nArgs) return // not typically reached, unless ExitFunction doesn't exit } if argsRange { diff --git a/go.mod b/go.mod index b0a09aa..90580a3 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module fortio.org/cli go 1.18 require ( - fortio.org/log v1.11.0 + fortio.org/log v1.12.0 fortio.org/version v1.0.3 ) + +require fortio.org/struct2env v0.4.0 // indirect diff --git a/go.sum b/go.sum index c9960e6..0b3794d 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ -fortio.org/log v1.11.0 h1:w7ueGPGbXz0A3+ApMz/5Q9gwEMrwSo/ohTlLo2Um6dU= -fortio.org/log v1.11.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU= +fortio.org/log v1.12.0 h1:5Yg4pL9Pp0jcWeJYixm2xikMCldVaSDMgDFDmQJZfho= +fortio.org/log v1.12.0/go.mod h1:1tMBG/Elr6YqjmJCWiejJp2FPvXg7/9UAN0Rfpkyt1o= +fortio.org/struct2env v0.4.0 h1:k5alSOTf3YHiB3MuacjDHQ3YhVWvNZ95ZP/a6MqvyLo= +fortio.org/struct2env v0.4.0/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410= fortio.org/version v1.0.3 h1:5gJ3plj6isAOMq52cI5ifo4cC+QHmJF76Wevc5Cp4x0= fortio.org/version v1.0.3/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0=