Skip to content

Commit 11d9fa0

Browse files
authored
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
1 parent 1f11db3 commit 11d9fa0

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

cli.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ var (
5757
BeforeFlagParseHook = func() {}
5858
// Calculated base exe name from args (will be used if ProgramName if not set).
5959
baseExe string
60+
// List of functions to call for env help.
61+
EnvHelpFuncs = []func(w io.Writer){log.EnvHelp}
6062
)
6163

6264
// ChangeFlagsDefault sets some flags to a different default.
@@ -92,7 +94,7 @@ func usage(w io.Writer, msg string, args ...any) {
9294
}
9395
_, _ = fmt.Fprintf(w, log.Colors.Reset+"%s %s usage:\n\t%s %s["+
9496
log.Colors.Cyan+"flags"+log.Colors.Reset+"]%s\nor 1 of the special arguments\n\t%s {"+
95-
ColorJoin(log.Colors.Purple, "help", "version", "buildinfo")+"}\n"+"flags:\n"+log.Colors.Cyan,
97+
ColorJoin(log.Colors.Purple, "help", "envhelp", "version", "buildinfo")+"}\n"+"flags:\n"+log.Colors.Cyan,
9698
ProgramName,
9799
log.Colors.Blue+ShortVersion+log.Colors.Reset,
98100
baseExe,
@@ -114,6 +116,13 @@ func usage(w io.Writer, msg string, args ...any) {
114116
}
115117
}
116118

119+
func EnvHelp(w io.Writer) {
120+
fmt.Println("# Environment variables recognized and current values:")
121+
for _, f := range EnvHelpFuncs {
122+
f(w)
123+
}
124+
}
125+
117126
// Main handles your commandline and flag parsing. Sets up flags first then call Main.
118127
// For a server with dynamic flags, call ServerMain instead.
119128
// Will either have called [ExitFunction] (defaults to [os.Exit])
@@ -156,17 +165,20 @@ func Main() {
156165
BeforeFlagParseHook()
157166
nArgs := len(os.Args)
158167
if nArgs == 2 {
168+
specialCmd := true
159169
switch strings.ToLower(os.Args[1]) {
160170
case "version":
161171
fmt.Println(ShortVersion)
162-
ExitFunction(0)
163-
return // not typically reached, unless ExitFunction doesn't exit
164172
case "buildinfo":
165173
fmt.Print(FullVersion)
166-
ExitFunction(0)
167-
return // not typically reached, unless ExitFunction doesn't exit
168174
case "help":
169175
usage(os.Stdout, "")
176+
case "envhelp":
177+
EnvHelp(os.Stdout)
178+
default:
179+
specialCmd = false
180+
}
181+
if specialCmd {
170182
ExitFunction(0)
171183
return // not typically reached, unless ExitFunction doesn't exit
172184
}
@@ -183,7 +195,11 @@ func Main() {
183195
os.Stderr.WriteString(log.Colors.BrightRed)
184196
flag.Parse()
185197
os.Stderr.WriteString(log.Colors.Reset)
186-
log.Config.ConsoleColor = !*nocolor
198+
if *nocolor {
199+
// Don't override the env if the flag isn't set
200+
// (downside is if LOGGER_FORCE_COLOR is set to false, this -logger-no-color=false can't override it)
201+
log.Config.ForceColor = !*nocolor
202+
}
187203
log.SetColorMode()
188204
nArgs = len(flag.Args())
189205
argsRange := (MinArgs != MaxArgs)
@@ -197,7 +213,7 @@ func Main() {
197213
}
198214
if MaxArgs >= 0 && nArgs > MaxArgs {
199215
if MaxArgs <= 0 {
200-
ErrUsage("No arguments expected (except for version, buildinfo or help and -flags), got %d", nArgs)
216+
ErrUsage("No arguments expected (except for version, buildinfo, help or envhelp and -flags), got %d", nArgs)
201217
return // not typically reached, unless ExitFunction doesn't exit
202218
}
203219
if argsRange {

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module fortio.org/cli
33
go 1.18
44

55
require (
6-
fortio.org/log v1.11.0
6+
fortio.org/log v1.12.0
77
fortio.org/version v1.0.3
88
)
9+
10+
require fortio.org/struct2env v0.4.0 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
fortio.org/log v1.11.0 h1:w7ueGPGbXz0A3+ApMz/5Q9gwEMrwSo/ohTlLo2Um6dU=
2-
fortio.org/log v1.11.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
1+
fortio.org/log v1.12.0 h1:5Yg4pL9Pp0jcWeJYixm2xikMCldVaSDMgDFDmQJZfho=
2+
fortio.org/log v1.12.0/go.mod h1:1tMBG/Elr6YqjmJCWiejJp2FPvXg7/9UAN0Rfpkyt1o=
3+
fortio.org/struct2env v0.4.0 h1:k5alSOTf3YHiB3MuacjDHQ3YhVWvNZ95ZP/a6MqvyLo=
4+
fortio.org/struct2env v0.4.0/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410=
35
fortio.org/version v1.0.3 h1:5gJ3plj6isAOMq52cI5ifo4cC+QHmJF76Wevc5Cp4x0=
46
fortio.org/version v1.0.3/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0=

0 commit comments

Comments
 (0)