Skip to content

Commit

Permalink
Add color support
Browse files Browse the repository at this point in the history
Also had to run godep again, which led to changes due to dropping of '-r' in Go 1.6+.

Modified .travis.yml to use 1.6
  • Loading branch information
James Haggerty committed Aug 9, 2017
1 parent e287c64 commit 62693a3
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.5
- 1.7

install:
- go build jp.go
Expand Down
24 changes: 24 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 29 additions & 3 deletions jp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"io/ioutil"
"os"

"github.com/jmespath/jp/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/jmespath/jp/Godeps/_workspace/src/github.com/jmespath/go-jmespath"
"github.com/codegangsta/cli"
"github.com/fatih/color"
"github.com/jmespath/go-jmespath"
"github.com/nwidger/jsoncolor"
)

const version = "0.1.2"
Expand All @@ -28,6 +30,11 @@ func main() {
Name: "expr-file, e",
Usage: "Read JMESPath expression from the specified file.",
},
cli.StringFlag{
Name: "color, c",
Value: "auto",
Usage: "Change the color setting (none, auto, always). auto is based on whether output is a tty.",
},
cli.BoolFlag{
Name: "unquoted, u",
Usage: "If the final result is a string, it will be printed without quotes.",
Expand Down Expand Up @@ -67,6 +74,18 @@ func runMain(c *cli.Context) int {
}
expression = c.Args()[0]
}
// Unfortunately, there's a global setting in the underlying library
// which we have to toggle here...
switch c.String("color") {
case "always":
color.NoColor = false
case "auto":
// this is the default in the library
case "never":
color.NoColor = true
default:
return errMsg("Invalid color specification. Must use always/auto/never")
}
if c.Bool("ast") {
parser := jmespath.NewParser()
parsed, err := parser.Parse(expression)
Expand Down Expand Up @@ -111,7 +130,14 @@ func runMain(c *cli.Context) int {
if c.Bool("unquoted") && isString {
os.Stdout.WriteString(converted)
} else {
toJSON, err := json.MarshalIndent(result, "", " ")
var toJSON []byte
var err error
if color.NoColor {
// avoid doing the extra processing in jsoncolor
toJSON, err = json.MarshalIndent(result, "", " ")
} else {
toJSON, err = jsoncolor.MarshalIndent(result, "", " ")
}
if err != nil {
errMsg("Error marshalling result to JSON: %s\n", err)
return 3
Expand Down

0 comments on commit 62693a3

Please sign in to comment.