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

use new logger and envhelp command #24

Merged
merged 5 commits into from
Nov 22, 2023
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
Next Next commit
use new logger and envhelp comamnd
  • Loading branch information
ldemailly committed Nov 22, 2023
commit ba5e1b7c1af07c82fca0a8736acdf5d1fdb07772
18 changes: 15 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=