Skip to content

Commit badf401

Browse files
committed
Adds -v/--values option; updates release script
1 parent 6d4fe18 commit badf401

File tree

5 files changed

+70
-25
lines changed

5 files changed

+70
-25
lines changed

README.mkd

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ If you're a Mac user you can also [install gron via brew](http://braumeister.org
4646
▶ brew install gron
4747
```
4848

49-
Or if you're a Go user you can use `go get` (if you're using Go 1.7 or newer):
49+
Or if you're a Go user you can use `go install`:
5050

5151
```
52-
▶ go get -u github.com/tomnomnom/gron
52+
▶ go install github.com/tomnomnom/gron@latest
5353
```
5454

5555
It's recommended that you alias `ungron` or `norg` (or both!) to `gron --ungron`. Put something like this in your shell profile (e.g. in `~/.bashrc`):
@@ -217,6 +217,7 @@ Usage:
217217
218218
Options:
219219
-u, --ungron Reverse the operation (turn assignments back into JSON)
220+
-v, --values Print just the values of provided assignments
220221
-c, --colorize Colorize output (default on tty)
221222
-m, --monochrome Monochrome (don't colorize output)
222223
-s, --stream Treat each line of input as a separate JSON object

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
module github.com/tomnomnom/gron
22

3+
go 1.15
4+
35
require (
46
github.com/fatih/color v1.7.0
57
github.com/mattn/go-colorable v0.0.9
68
github.com/mattn/go-isatty v0.0.4 // indirect
79
github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5
810
github.com/pkg/errors v0.8.0
11+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
912
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5 h1:d+C3xJdxZT7wN
88
github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5/go.mod h1:GYFm0zZgTNeoK1QxuIofRDasy2ibmaJZhZLzwsMXUF4=
99
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
1010
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
11+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
12+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

main.go

+50-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func init() {
5757

5858
h += "Options:\n"
5959
h += " -u, --ungron Reverse the operation (turn assignments back into JSON)\n"
60+
h += " -v, --values Print just the values of provided assignments\n"
6061
h += " -c, --colorize Colorize output (default on tty)\n"
6162
h += " -m, --monochrome Monochrome (don't colorize output)\n"
6263
h += " -s, --stream Treat each line of input as a separate JSON object\n"
@@ -95,6 +96,7 @@ func main() {
9596
versionFlag bool
9697
insecureFlag bool
9798
jsonFlag bool
99+
valuesFlag bool
98100
)
99101

100102
flag.BoolVar(&ungronFlag, "ungron", false, "")
@@ -111,6 +113,9 @@ func main() {
111113
flag.BoolVar(&insecureFlag, "insecure", false, "")
112114
flag.BoolVar(&jsonFlag, "j", false, "")
113115
flag.BoolVar(&jsonFlag, "json", false, "")
116+
flag.BoolVar(&valuesFlag, "values", false, "")
117+
flag.BoolVar(&valuesFlag, "value", false, "")
118+
flag.BoolVar(&valuesFlag, "v", false, "")
114119

115120
flag.Parse()
116121

@@ -161,10 +166,12 @@ func main() {
161166
opts = opts | optJSON
162167
}
163168

164-
// Pick the appropriate action: gron, ungron or gronStream
169+
// Pick the appropriate action: gron, ungron, gronValues, or gronStream
165170
var a actionFn = gron
166171
if ungronFlag {
167172
a = ungron
173+
} else if valuesFlag {
174+
a = gronValues
168175
} else if streamFlag {
169176
a = gronStream
170177
}
@@ -391,6 +398,48 @@ func ungron(r io.Reader, w io.Writer, opts int) (int, error) {
391398
return exitOK, nil
392399
}
393400

401+
// gronValues prints just the scalar values from some input gron statements
402+
// without any quotes or anything of that sort; a bit like jq -r
403+
// e.g. json[0].user.name = "Sam"; -> Sam
404+
func gronValues(r io.Reader, w io.Writer, opts int) (int, error) {
405+
scanner := bufio.NewScanner(os.Stdin)
406+
407+
for scanner.Scan() {
408+
s := statementFromString(scanner.Text())
409+
410+
// strip off the leading 'json' bare key
411+
if s[0].typ == typBare && s[0].text == "json" {
412+
s = s[1:]
413+
}
414+
415+
// strip off the leading dots
416+
if s[0].typ == typDot || s[0].typ == typLBrace {
417+
s = s[1:]
418+
}
419+
420+
for _, t := range s {
421+
switch t.typ {
422+
case typString:
423+
var text string
424+
err := json.Unmarshal([]byte(t.text), &text)
425+
if err != nil {
426+
// just swallow errors and try to continue
427+
continue
428+
}
429+
fmt.Println(text)
430+
431+
case typNumber, typTrue, typFalse, typNull:
432+
fmt.Println(t.text)
433+
434+
default:
435+
// Nothing
436+
}
437+
}
438+
}
439+
440+
return exitOK, nil
441+
}
442+
394443
func colorizeJSON(src []byte) ([]byte, error) {
395444
out := &bytes.Buffer{}
396445
f := jsoncolor.NewFormatter()

script/release

+12-22
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,15 @@ if [ $? -ne 0 ]; then
2626
exit 3
2727
fi
2828

29-
# Check if tag exists
30-
git fetch --tags
31-
git tag | grep "^${TAG}$"
32-
33-
if [ $? -ne 0 ]; then
34-
github-release release \
35-
--user ${USER} \
36-
--repo ${REPO} \
37-
--tag ${TAG} \
38-
--name "${REPO} ${TAG}" \
39-
--description "${TAG}" \
40-
--pre-release
41-
fi
42-
29+
FILELIST=""
4330

4431
for ARCH in "amd64" "386"; do
4532
for OS in "darwin" "linux" "windows" "freebsd"; do
4633

34+
if [[ "${OS}" == "darwin" && "${ARCH}" == "386" ]]; then
35+
continue
36+
fi
37+
4738
BINFILE="${BINARY}"
4839

4940
if [[ "${OS}" == "windows" ]]; then
@@ -52,22 +43,21 @@ for ARCH in "amd64" "386"; do
5243

5344
rm -f ${BINFILE}
5445

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

5748
if [[ "${OS}" == "windows" ]]; then
5849
ARCHIVE="${BINARY}-${OS}-${ARCH}-${VERSION}.zip"
5950
zip ${ARCHIVE} ${BINFILE}
51+
rm ${BINFILE}
6052
else
6153
ARCHIVE="${BINARY}-${OS}-${ARCH}-${VERSION}.tgz"
6254
tar --create --gzip --file=${ARCHIVE} ${BINFILE}
6355
fi
6456

65-
echo "Uploading ${ARCHIVE}..."
66-
github-release upload \
67-
--user ${USER} \
68-
--repo ${REPO} \
69-
--tag ${TAG} \
70-
--name "${ARCHIVE}" \
71-
--file ${PROJDIR}/${ARCHIVE}
57+
FILELIST="${FILELIST} ${PROJDIR}/${ARCHIVE}"
7258
done
7359
done
60+
61+
gh release create ${TAG} ${FILELIST}
62+
rm ${FILELIST}
63+

0 commit comments

Comments
 (0)