From ba5e1b7c1af07c82fca0a8736acdf5d1fdb07772 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 21 Nov 2023 18:24:57 -0800 Subject: [PATCH 1/5] use new logger and envhelp comamnd --- cli.go | 18 +++++++++++++++--- go.mod | 4 +++- go.sum | 6 ++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cli.go b/cli.go index e4ba0c7..cea3111 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) = []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, @@ -169,6 +171,12 @@ func Main() { usage(os.Stdout, "") ExitFunction(0) return // not typically reached, unless ExitFunction doesn't exit + case "envhelp": + for _, f := range EnvHelpFuncs { + f(os.Stdout) + } + ExitFunction(0) + return // not typically reached, unless ExitFunction doesn't exit } } if CommandBeforeFlags { @@ -183,7 +191,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 +209,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..7b8d1db 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.11.1-0.20231121235628-a4e21bc00e4f fortio.org/version v1.0.3 ) + +require fortio.org/struct2env v0.4.0 // indirect diff --git a/go.sum b/go.sum index c9960e6..226ba23 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.11.1-0.20231121235628-a4e21bc00e4f h1:/2v4FpF5n2rkAJ/wZBXvFvn2sDgvooNzMc1gbPkFlmA= +fortio.org/log v1.11.1-0.20231121235628-a4e21bc00e4f/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= From 37d4d945aadfc2da933cfac7ad2e38a3dd15aadb Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 21 Nov 2023 19:09:12 -0800 Subject: [PATCH 2/5] latest from PR --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 7b8d1db..87f7730 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module fortio.org/cli go 1.18 require ( - fortio.org/log v1.11.1-0.20231121235628-a4e21bc00e4f + fortio.org/log v1.11.1-0.20231122030540-3f5c8cea573c fortio.org/version v1.0.3 ) diff --git a/go.sum b/go.sum index 226ba23..85e491b 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -fortio.org/log v1.11.1-0.20231121235628-a4e21bc00e4f h1:/2v4FpF5n2rkAJ/wZBXvFvn2sDgvooNzMc1gbPkFlmA= -fortio.org/log v1.11.1-0.20231121235628-a4e21bc00e4f/go.mod h1:1tMBG/Elr6YqjmJCWiejJp2FPvXg7/9UAN0Rfpkyt1o= +fortio.org/log v1.11.1-0.20231122030540-3f5c8cea573c h1:wCwiVY2kVuaJjkwdkr1CJ/xmEsxTltax9D7oYCEWqKc= +fortio.org/log v1.11.1-0.20231122030540-3f5c8cea573c/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= From b56aa0e139af98269b896d3b733dfbc55d9d283c Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 21 Nov 2023 19:16:11 -0800 Subject: [PATCH 3/5] latest log --- cli.go | 1 + go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cli.go b/cli.go index cea3111..0caeac1 100644 --- a/cli.go +++ b/cli.go @@ -172,6 +172,7 @@ func Main() { ExitFunction(0) return // not typically reached, unless ExitFunction doesn't exit case "envhelp": + fmt.Println("# Environment variables recognized and current values:") for _, f := range EnvHelpFuncs { f(os.Stdout) } diff --git a/go.mod b/go.mod index 87f7730..67add67 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module fortio.org/cli go 1.18 require ( - fortio.org/log v1.11.1-0.20231122030540-3f5c8cea573c + fortio.org/log v1.11.1-0.20231122031455-08c39b67f0de fortio.org/version v1.0.3 ) diff --git a/go.sum b/go.sum index 85e491b..d24a71a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -fortio.org/log v1.11.1-0.20231122030540-3f5c8cea573c h1:wCwiVY2kVuaJjkwdkr1CJ/xmEsxTltax9D7oYCEWqKc= -fortio.org/log v1.11.1-0.20231122030540-3f5c8cea573c/go.mod h1:1tMBG/Elr6YqjmJCWiejJp2FPvXg7/9UAN0Rfpkyt1o= +fortio.org/log v1.11.1-0.20231122031455-08c39b67f0de h1:SXOc8efqcIIYZgTe6M6zgigRtV9eKWT78mlzcyYu7hA= +fortio.org/log v1.11.1-0.20231122031455-08c39b67f0de/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= From 55aa738b12f6d3236262eb1ca176cc483ee4af89 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 21 Nov 2023 20:25:25 -0800 Subject: [PATCH 4/5] use release version for log --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 67add67..90580a3 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module fortio.org/cli go 1.18 require ( - fortio.org/log v1.11.1-0.20231122031455-08c39b67f0de + fortio.org/log v1.12.0 fortio.org/version v1.0.3 ) diff --git a/go.sum b/go.sum index d24a71a..0b3794d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -fortio.org/log v1.11.1-0.20231122031455-08c39b67f0de h1:SXOc8efqcIIYZgTe6M6zgigRtV9eKWT78mlzcyYu7hA= -fortio.org/log v1.11.1-0.20231122031455-08c39b67f0de/go.mod h1:1tMBG/Elr6YqjmJCWiejJp2FPvXg7/9UAN0Rfpkyt1o= +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= From 0daa01276ba1e839c673128504030a254efc52ee Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 21 Nov 2023 20:34:32 -0800 Subject: [PATCH 5/5] linter fix --- cli.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/cli.go b/cli.go index 0caeac1..91fd39a 100644 --- a/cli.go +++ b/cli.go @@ -57,8 +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) = []func(w io.Writer){log.EnvHelp} + // List of functions to call for env help. + EnvHelpFuncs = []func(w io.Writer){log.EnvHelp} ) // ChangeFlagsDefault sets some flags to a different default. @@ -116,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]) @@ -158,24 +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, "") - ExitFunction(0) - return // not typically reached, unless ExitFunction doesn't exit case "envhelp": - fmt.Println("# Environment variables recognized and current values:") - for _, f := range EnvHelpFuncs { - f(os.Stdout) - } + EnvHelp(os.Stdout) + default: + specialCmd = false + } + if specialCmd { ExitFunction(0) return // not typically reached, unless ExitFunction doesn't exit }