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 all commits
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
30 changes: 23 additions & 7 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){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 All @@ -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])
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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 {
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.12.0
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.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=