Skip to content

Commit

Permalink
main: add an option to print the version
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed May 28, 2022
1 parent 589d85e commit ff85fe8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 21 additions & 10 deletions cmd/jlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"io"
"os"
"os/signal"
"runtime/debug"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand Down

0 comments on commit ff85fe8

Please sign in to comment.