Skip to content

Commit

Permalink
Adds -v/--values option; updates release script
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Apr 13, 2022
1 parent 6d4fe18 commit badf401
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 25 deletions.
5 changes: 3 additions & 2 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ If you're a Mac user you can also [install gron via brew](http://braumeister.org
▶ brew install gron
```

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

```
▶ go get -u github.com/tomnomnom/gron
▶ go install github.com/tomnomnom/gron@latest
```

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`):
Expand Down Expand Up @@ -217,6 +217,7 @@ Usage:
Options:
-u, --ungron Reverse the operation (turn assignments back into JSON)
-v, --values Print just the values of provided assignments
-c, --colorize Colorize output (default on tty)
-m, --monochrome Monochrome (don't colorize output)
-s, --stream Treat each line of input as a separate JSON object
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module github.com/tomnomnom/gron

go 1.15

require (
github.com/fatih/color v1.7.0
github.com/mattn/go-colorable v0.0.9
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5
github.com/pkg/errors v0.8.0
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5 h1:d+C3xJdxZT7wN
github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5/go.mod h1:GYFm0zZgTNeoK1QxuIofRDasy2ibmaJZhZLzwsMXUF4=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
51 changes: 50 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func init() {

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

flag.BoolVar(&ungronFlag, "ungron", false, "")
Expand All @@ -111,6 +113,9 @@ func main() {
flag.BoolVar(&insecureFlag, "insecure", false, "")
flag.BoolVar(&jsonFlag, "j", false, "")
flag.BoolVar(&jsonFlag, "json", false, "")
flag.BoolVar(&valuesFlag, "values", false, "")
flag.BoolVar(&valuesFlag, "value", false, "")
flag.BoolVar(&valuesFlag, "v", false, "")

flag.Parse()

Expand Down Expand Up @@ -161,10 +166,12 @@ func main() {
opts = opts | optJSON
}

// Pick the appropriate action: gron, ungron or gronStream
// Pick the appropriate action: gron, ungron, gronValues, or gronStream
var a actionFn = gron
if ungronFlag {
a = ungron
} else if valuesFlag {
a = gronValues
} else if streamFlag {
a = gronStream
}
Expand Down Expand Up @@ -391,6 +398,48 @@ func ungron(r io.Reader, w io.Writer, opts int) (int, error) {
return exitOK, nil
}

// gronValues prints just the scalar values from some input gron statements
// without any quotes or anything of that sort; a bit like jq -r
// e.g. json[0].user.name = "Sam"; -> Sam
func gronValues(r io.Reader, w io.Writer, opts int) (int, error) {
scanner := bufio.NewScanner(os.Stdin)

for scanner.Scan() {
s := statementFromString(scanner.Text())

// strip off the leading 'json' bare key
if s[0].typ == typBare && s[0].text == "json" {
s = s[1:]
}

// strip off the leading dots
if s[0].typ == typDot || s[0].typ == typLBrace {
s = s[1:]
}

for _, t := range s {
switch t.typ {
case typString:
var text string
err := json.Unmarshal([]byte(t.text), &text)
if err != nil {
// just swallow errors and try to continue
continue
}
fmt.Println(text)

case typNumber, typTrue, typFalse, typNull:
fmt.Println(t.text)

default:
// Nothing
}
}
}

return exitOK, nil
}

func colorizeJSON(src []byte) ([]byte, error) {
out := &bytes.Buffer{}
f := jsoncolor.NewFormatter()
Expand Down
34 changes: 12 additions & 22 deletions script/release
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,15 @@ if [ $? -ne 0 ]; then
exit 3
fi

# Check if tag exists
git fetch --tags
git tag | grep "^${TAG}$"

if [ $? -ne 0 ]; then
github-release release \
--user ${USER} \
--repo ${REPO} \
--tag ${TAG} \
--name "${REPO} ${TAG}" \
--description "${TAG}" \
--pre-release
fi

FILELIST=""

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

if [[ "${OS}" == "darwin" && "${ARCH}" == "386" ]]; then
continue
fi

BINFILE="${BINARY}"

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

rm -f ${BINFILE}

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

if [[ "${OS}" == "windows" ]]; then
ARCHIVE="${BINARY}-${OS}-${ARCH}-${VERSION}.zip"
zip ${ARCHIVE} ${BINFILE}
rm ${BINFILE}
else
ARCHIVE="${BINARY}-${OS}-${ARCH}-${VERSION}.tgz"
tar --create --gzip --file=${ARCHIVE} ${BINFILE}
fi

echo "Uploading ${ARCHIVE}..."
github-release upload \
--user ${USER} \
--repo ${REPO} \
--tag ${TAG} \
--name "${ARCHIVE}" \
--file ${PROJDIR}/${ARCHIVE}
FILELIST="${FILELIST} ${PROJDIR}/${ARCHIVE}"
done
done

gh release create ${TAG} ${FILELIST}
rm ${FILELIST}

0 comments on commit badf401

Please sign in to comment.