From ff85fe8049dfe271bd919f3e7ef6512c1b9ad203 Mon Sep 17 00:00:00 2001 From: Jonathan Rockway <2367+jrockway@users.noreply.github.com> Date: Sat, 28 May 2022 06:18:10 -0400 Subject: [PATCH] main: add an option to print the version --- README.md | 1 + cmd/jlog/main.go | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2cc7311..b2e1d37 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Here's the `--help` message: -M, --no-color Disable the use of color. [$JLOG_FORCE_MONOCHROME] -C, --no-monochrome Force the use of color. Note: the short flag will change in a future release. --profile= If set, collect a CPU profile and write it to this file. + -v, --version Print version information and exit. Help Options: -h, --help Show this help message diff --git a/cmd/jlog/main.go b/cmd/jlog/main.go index b89dd38..2ab3549 100644 --- a/cmd/jlog/main.go +++ b/cmd/jlog/main.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "io" "os" "os/signal" "runtime/debug" @@ -42,6 +43,8 @@ type general struct { NoColor bool `short:"M" long:"no-color" description:"Disable the use of color." env:"JLOG_FORCE_MONOCHROME"` NoMonochrome bool `short:"C" long:"no-monochrome" description:"Force the use of color. Note: the short flag will change in a future release." ENV:"JLOG_FORCE_COLOR"` Profile string `long:"profile" description:"If set, collect a CPU profile and write it to this file."` + + Version bool `short:"v" long:"version" description:"Print version information and exit."` } type input struct { @@ -51,6 +54,19 @@ type input struct { MessageKey string `long:"messagekey" description:"JSON key that holds the log message." env:"JLOG_MESSAGE_KEY"` } +func printVersion(w io.Writer) { + fmt.Fprintf(w, "jlog - Search and pretty-print your JSON logs.\nMore info: https://github.com/jrockway/json-logs\n") + fmt.Fprintf(w, "Version %s (%s) built on %s by %s\n", version, commit, date, builtBy) + if buildinfo, ok := debug.ReadBuildInfo(); ok { + fmt.Fprintf(w, " go: %v\n", buildinfo.GoVersion) + if commit == "none" { + for _, x := range buildinfo.Settings { + fmt.Fprintf(w, " %v: %v\n", x.Key, x.Value) + } + } + } +} + func main() { var gen general var in input @@ -69,16 +85,7 @@ func main() { extraArgs, err := fp.Parse() if err != nil { if ferr, ok := err.(*flags.Error); ok && ferr.Type == flags.ErrHelp { - fmt.Fprintf(os.Stderr, "jlog - Search and pretty-print your JSON logs.\nMore info: https://github.com/jrockway/json-logs\n") - fmt.Fprintf(os.Stderr, "Version %s (%s) built on %s by %s\n", version, commit, date, builtBy) - if buildinfo, ok := debug.ReadBuildInfo(); ok { - fmt.Fprintf(os.Stderr, " built with go %v\n", buildinfo.GoVersion) - if commit == "none" { - for _, x := range buildinfo.Settings { - fmt.Fprintf(os.Stderr, " %v: %v\n", x.Key, x.Value) - } - } - } + printVersion(os.Stderr) fmt.Fprintf(os.Stderr, ferr.Message) os.Exit(2) } @@ -89,6 +96,10 @@ func main() { fmt.Fprintf(os.Stderr, "unexpected command-line arguments after flag parsing: %v\n", extraArgs) os.Exit(1) } + if gen.Version { + printVersion(os.Stdout) + os.Exit(0) + } var f *os.File if gen.Profile != "" { var err error