From 161e241f53763790382b6e69b886b7683e1643cd Mon Sep 17 00:00:00 2001 From: Tom Hudson Date: Sat, 3 Sep 2016 15:49:56 +0100 Subject: [PATCH] Adds version information to builds; fixes #19 --- CHANGELOG.mkd | 3 ++- README.mkd | 1 + main.go | 17 ++++++++++++++++- script/release | 2 +- url.go | 3 ++- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.mkd b/CHANGELOG.mkd index 0f2701e..9b385f0 100644 --- a/CHANGELOG.mkd +++ b/CHANGELOG.mkd @@ -1,8 +1,9 @@ # Changelog -## HEAD +## 0.3.3 - Slightly improved error reporting when ungronning - 20 second timeout on HTTP(s) requests (thanks @gummiboll!) +- Version information added at build time + `--version` option (issue #19) ## 0.3.2 - Adds handling of `--` lines produced by grep -A etc (issue #15) diff --git a/README.mkd b/README.mkd index 73ffeb0..c228a63 100644 --- a/README.mkd +++ b/README.mkd @@ -183,6 +183,7 @@ Usage: Options: -u, --ungron Reverse the operation (turn assignments back into JSON) -m, --monochrome Monochrome (don't colorize output) + --version Print version information Exit Codes: 0 OK diff --git a/main.go b/main.go index 0cdf2eb..409c748 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "github.com/pkg/errors" ) +// Exit codes const ( exitOK = iota exitOpenFile @@ -25,6 +26,7 @@ const ( exitJSONEncode ) +// Output colors var ( strColor = color.New(color.FgYellow) braceColor = color.New(color.FgMagenta) @@ -33,6 +35,10 @@ var ( boolColor = color.New(color.FgCyan) ) +// gronVersion stores the current gron version, set at build +// time with the ldflags -X option +var gronVersion = "dev" + func init() { flag.Usage = func() { h := "Transform JSON (from a file, URL, or stdin) into discrete assignments to make it greppable\n\n" @@ -42,7 +48,8 @@ func init() { h += "Options:\n" h += " -u, --ungron Reverse the operation (turn assignments back into JSON)\n" - h += " -m, --monochrome Monochrome (don't colorize output)\n\n" + h += " -m, --monochrome Monochrome (don't colorize output)\n" + h += " --version Print version information\n\n" h += "Exit Codes:\n" h += fmt.Sprintf(" %d\t%s\n", exitOK, "OK") @@ -68,15 +75,23 @@ func main() { var ( ungronFlag bool monochromeFlag bool + versionFlag bool ) flag.BoolVar(&ungronFlag, "ungron", false, "Turn statements into JSON instead") flag.BoolVar(&ungronFlag, "u", false, "Turn statements into JSON instead") flag.BoolVar(&monochromeFlag, "monochrome", false, "Monochrome (don't colorize output)") flag.BoolVar(&monochromeFlag, "m", false, "Monochrome (don't colorize output)") + flag.BoolVar(&versionFlag, "version", false, "Print version information") flag.Parse() + // Print version information + if versionFlag { + fmt.Printf("gron version %s\n", gronVersion) + os.Exit(exitOK) + } + var raw io.Reader filename := flag.Arg(0) diff --git a/script/release b/script/release index 33b8fb0..34a2d1e 100755 --- a/script/release +++ b/script/release @@ -52,7 +52,7 @@ for ARCH in "amd64" "386"; do rm -f ${BINFILE} - GOOS=${OS} GOARCH=${ARCH} go build github.com/${USER}/${REPO} + GOOS=${OS} GOARCH=${ARCH} go build -ldflags "-X main.gronVersion=${VERSION}" github.com/${USER}/${REPO} if [[ "${OS}" == "windows" ]]; then ARCHIVE="${BINARY}-${OS}-${ARCH}-${VERSION}.zip" diff --git a/url.go b/url.go index 14a203d..b357093 100644 --- a/url.go +++ b/url.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "fmt" "io" "net/http" "strings" @@ -21,7 +22,7 @@ func getURL(url string) (io.Reader, error) { if err != nil { return nil, err } - req.Header.Set("User-Agent", "gron/0.2") + req.Header.Set("User-Agent", fmt.Sprintf("gron/%s", gronVersion)) req.Header.Set("Accept", "application/json") resp, err := client.Do(req)