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

add flags for color control (from log's 1.6.0) #8

Merged
merged 1 commit into from
Jul 6, 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
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,42 @@ func main() {
cli.Main() // Will have either called cli.ExitFunction or everything is valid
// Next line output won't show when passed -quiet
log.Infof("Info test, -myflag is %q", *myFlag)
// This always shows
log.Printf("Hello world, version %s, args %v", cli.ShortVersion, flag.Args())
// This shows and is colorized and structured, unless loglevel is set to critical.
log.S(log.Error, "Error test",
log.Str("myflag", *myFlag),
log.Attr("num_args", len(flag.Args())),
log.Attr("args", flag.Args()))
}
```

```bash
$ sampleTool a
sampleTool 1.0.0 usage:
sampleTool 1.2.0 usage:
sampleTool [flags] arg1 arg2 [arg3...arg4]
or 1 of the special arguments
sampleTool {help|version|buildinfo}
flags:
-logger-force-color
Force color output even if stderr isn't a terminal
-logger-no-color
Prevent colorized output even if stderr is a terminal
-loglevel level
log level, one of [Debug Verbose Info Warning Error Critical Fatal] (default Info)
-myflag string
my flag (default "default")
-quiet
Quiet mode, sets log level to warning
Quiet mode, sets loglevel to Error (quietly) to reduces the output
At least 2 arguments expected, got 1
```

or normal case
or normal case (and now in color when on console)
```bash
$ sampleTool a b
15:42:17 I Info test, -myflag is "default"
15:42:17 Hello world, version dev, args [a b]
15:30:50.217 I Info test, -myflag is "default"
15:30:50.217 I Hello world, version dev, args [a b]
15:30:50.217 E Error test, myflag="default", num_args="2", args="[a b]"
```

## Additional builtins
Expand Down Expand Up @@ -94,6 +105,10 @@ or 1 of the special arguments
multicurl {help|version|buildinfo}
flags:
[...]
-logger-force-color
Force color output even if stderr isn't a terminal
-logger-no-color
Prevent colorized output even if stderr is a terminal
-loglevel level
log level, one of [Debug Verbose Info Warning Error Critical Fatal] (default Info)
-quiet
Expand Down
6 changes: 6 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func usage(w io.Writer, msg string, args ...any) {
func Main() {
quietFlag := flag.Bool("quiet", false,
"Quiet mode, sets loglevel to Error (quietly) to reduces the output")
flag.BoolVar(&log.Config.ForceColor, "logger-force-color", false,
"Force color output even if stderr isn't a terminal")
nocolor := flag.Bool("logger-no-color", false,
"Prevent colorized output even if stderr is a terminal")
ShortVersion, LongVersion, FullVersion = version.FromBuildInfo()
log.Config.FatalExit = ExitFunction
baseExe = filepath.Base(os.Args[0])
Expand Down Expand Up @@ -132,6 +136,8 @@ func Main() {
os.Args = append([]string{os.Args[0]}, os.Args[2:]...)
}
flag.Parse()
log.Config.ConsoleColor = !*nocolor
log.SetColorMode()
nArgs = len(flag.Args())
argsRange := (MinArgs != MaxArgs)
exactly := "Exactly"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module fortio.org/cli
go 1.18

require (
fortio.org/log v1.2.2
fortio.org/log v1.6.0
fortio.org/version v1.0.2
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fortio.org/log v1.2.2 h1:vs42JjNwiqbMbacittZjJE9+oi72Za6aekML9gKmILg=
fortio.org/log v1.2.2/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
fortio.org/log v1.6.0 h1:GYt2UjoZdURBEb7J1fxBmLOirOrKvKhKKi7xLScqg1A=
fortio.org/log v1.6.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
fortio.org/version v1.0.2 h1:8NwxdX58aoeKx7T5xAPO0xlUu1Hpk42nRz5s6e6eKZ0=
fortio.org/version v1.0.2/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0=
6 changes: 6 additions & 0 deletions sampleTool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ func main() {
cli.Main() // Will have either called cli.ExitFunction or everything is valid
// Next line output won't show when passed -quiet
log.Infof("Info test, -myflag is %q", *myFlag)
// This always shows
log.Printf("Hello world, version %s, args %v", cli.ShortVersion, flag.Args())
// This shows and is colorized and structured, unless loglevel is set to critical.
log.S(log.Error, "Error test",
log.Str("myflag", *myFlag),
log.Attr("num_args", len(flag.Args())),
log.Attr("args", flag.Args()))
}