Skip to content

Commit 161e241

Browse files
committed
Adds version information to builds; fixes #19
1 parent 02f6d7b commit 161e241

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

CHANGELOG.mkd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Changelog
22

3-
## HEAD
3+
## 0.3.3
44
- Slightly improved error reporting when ungronning
55
- 20 second timeout on HTTP(s) requests (thanks @gummiboll!)
6+
- Version information added at build time + `--version` option (issue #19)
67

78
## 0.3.2
89
- Adds handling of `--` lines produced by grep -A etc (issue #15)

README.mkd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Usage:
183183
Options:
184184
-u, --ungron Reverse the operation (turn assignments back into JSON)
185185
-m, --monochrome Monochrome (don't colorize output)
186+
--version Print version information
186187
187188
Exit Codes:
188189
0 OK

main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/pkg/errors"
1616
)
1717

18+
// Exit codes
1819
const (
1920
exitOK = iota
2021
exitOpenFile
@@ -25,6 +26,7 @@ const (
2526
exitJSONEncode
2627
)
2728

29+
// Output colors
2830
var (
2931
strColor = color.New(color.FgYellow)
3032
braceColor = color.New(color.FgMagenta)
@@ -33,6 +35,10 @@ var (
3335
boolColor = color.New(color.FgCyan)
3436
)
3537

38+
// gronVersion stores the current gron version, set at build
39+
// time with the ldflags -X option
40+
var gronVersion = "dev"
41+
3642
func init() {
3743
flag.Usage = func() {
3844
h := "Transform JSON (from a file, URL, or stdin) into discrete assignments to make it greppable\n\n"
@@ -42,7 +48,8 @@ func init() {
4248

4349
h += "Options:\n"
4450
h += " -u, --ungron Reverse the operation (turn assignments back into JSON)\n"
45-
h += " -m, --monochrome Monochrome (don't colorize output)\n\n"
51+
h += " -m, --monochrome Monochrome (don't colorize output)\n"
52+
h += " --version Print version information\n\n"
4653

4754
h += "Exit Codes:\n"
4855
h += fmt.Sprintf(" %d\t%s\n", exitOK, "OK")
@@ -68,15 +75,23 @@ func main() {
6875
var (
6976
ungronFlag bool
7077
monochromeFlag bool
78+
versionFlag bool
7179
)
7280

7381
flag.BoolVar(&ungronFlag, "ungron", false, "Turn statements into JSON instead")
7482
flag.BoolVar(&ungronFlag, "u", false, "Turn statements into JSON instead")
7583
flag.BoolVar(&monochromeFlag, "monochrome", false, "Monochrome (don't colorize output)")
7684
flag.BoolVar(&monochromeFlag, "m", false, "Monochrome (don't colorize output)")
85+
flag.BoolVar(&versionFlag, "version", false, "Print version information")
7786

7887
flag.Parse()
7988

89+
// Print version information
90+
if versionFlag {
91+
fmt.Printf("gron version %s\n", gronVersion)
92+
os.Exit(exitOK)
93+
}
94+
8095
var raw io.Reader
8196

8297
filename := flag.Arg(0)

script/release

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ for ARCH in "amd64" "386"; do
5252

5353
rm -f ${BINFILE}
5454

55-
GOOS=${OS} GOARCH=${ARCH} go build github.com/${USER}/${REPO}
55+
GOOS=${OS} GOARCH=${ARCH} go build -ldflags "-X main.gronVersion=${VERSION}" github.com/${USER}/${REPO}
5656

5757
if [[ "${OS}" == "windows" ]]; then
5858
ARCHIVE="${BINARY}-${OS}-${ARCH}-${VERSION}.zip"

url.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bufio"
5+
"fmt"
56
"io"
67
"net/http"
78
"strings"
@@ -21,7 +22,7 @@ func getURL(url string) (io.Reader, error) {
2122
if err != nil {
2223
return nil, err
2324
}
24-
req.Header.Set("User-Agent", "gron/0.2")
25+
req.Header.Set("User-Agent", fmt.Sprintf("gron/%s", gronVersion))
2526
req.Header.Set("Accept", "application/json")
2627

2728
resp, err := client.Do(req)

0 commit comments

Comments
 (0)