diff --git a/.travis.yml b/.travis.yml index 23b41f2..3879697 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - 1.5 + - 1.7 install: - go build jp.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 7fb8dce..c4311e1 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,16 +1,40 @@ { "ImportPath": "github.com/jmespath/jp", "GoVersion": "go1.4.2", + "GodepVersion": "v79", "Deps": [ { "ImportPath": "github.com/codegangsta/cli", "Comment": "1.2.0-66-g6086d79", "Rev": "6086d7927ec35315964d9fea46df6c04e6d697c1" }, + { + "ImportPath": "github.com/fatih/color", + "Comment": "v1.5.0-4-g67c513e", + "Rev": "67c513e5729f918f5e69786686770c27141a4490" + }, { "ImportPath": "github.com/jmespath/go-jmespath", "Comment": "0.2.0", "Rev": "bbaa0945d0cfe535ca7ce710b888e230d525dc63" + }, + { + "ImportPath": "github.com/mattn/go-colorable", + "Comment": "v0.0.9-2-g6c0fd4a", + "Rev": "6c0fd4aa6ec5818d5e3ea9e03ae436972a6c5a9a" + }, + { + "ImportPath": "github.com/mattn/go-isatty", + "Comment": "v0.0.2", + "Rev": "fc9e8d8ef48496124e79ae0df75490096eccf6fe" + }, + { + "ImportPath": "github.com/nwidger/jsoncolor", + "Rev": "75a6de4340e59be95f0884b9cebdda246e0fdf40" + }, + { + "ImportPath": "golang.org/x/sys/unix", + "Rev": "b0e0dd72976dc482b6cb37c5640440f876ac1907" } ] } diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/app_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/app_test.go index f9f368c..2cbb0e3 100644 --- a/Godeps/_workspace/src/github.com/codegangsta/cli/app_test.go +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/app_test.go @@ -6,7 +6,7 @@ import ( "os" "testing" - "github.com/jmespath/jp/Godeps/_workspace/src/github.com/codegangsta/cli" + "github.com/codegangsta/cli" ) func ExampleApp() { diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/cli_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/cli_test.go index f812954..879a793 100644 --- a/Godeps/_workspace/src/github.com/codegangsta/cli/cli_test.go +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/cli_test.go @@ -3,7 +3,7 @@ package cli_test import ( "os" - "github.com/jmespath/jp/Godeps/_workspace/src/github.com/codegangsta/cli" + "github.com/codegangsta/cli" ) func Example() { diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/command_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/command_test.go index 7e414a4..c0f556a 100644 --- a/Godeps/_workspace/src/github.com/codegangsta/cli/command_test.go +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/command_test.go @@ -4,7 +4,7 @@ import ( "flag" "testing" - "github.com/jmespath/jp/Godeps/_workspace/src/github.com/codegangsta/cli" + "github.com/codegangsta/cli" ) func TestCommandDoNotIgnoreFlags(t *testing.T) { diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/context_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/context_test.go index 38416f9..7c9a443 100644 --- a/Godeps/_workspace/src/github.com/codegangsta/cli/context_test.go +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/context_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "github.com/jmespath/jp/Godeps/_workspace/src/github.com/codegangsta/cli" + "github.com/codegangsta/cli" ) func TestNewContext(t *testing.T) { diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/flag_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/flag_test.go index 6367b8d..f0f096a 100644 --- a/Godeps/_workspace/src/github.com/codegangsta/cli/flag_test.go +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/flag_test.go @@ -7,7 +7,7 @@ import ( "strings" "testing" - "github.com/jmespath/jp/Godeps/_workspace/src/github.com/codegangsta/cli" + "github.com/codegangsta/cli" ) var boolFlagTests = []struct { diff --git a/Godeps/_workspace/src/github.com/jmespath/go-jmespath/fuzz/jmespath.go b/Godeps/_workspace/src/github.com/jmespath/go-jmespath/fuzz/jmespath.go index 7f75bc9..c7df087 100644 --- a/Godeps/_workspace/src/github.com/jmespath/go-jmespath/fuzz/jmespath.go +++ b/Godeps/_workspace/src/github.com/jmespath/go-jmespath/fuzz/jmespath.go @@ -1,6 +1,6 @@ package jmespath -import "github.com/jmespath/jp/Godeps/_workspace/src/github.com/jmespath/go-jmespath" +import "github.com/jmespath/go-jmespath" // Fuzz will fuzz test the JMESPath parser. func Fuzz(data []byte) int { diff --git a/jp.go b/jp.go index 9af9124..5fb2c46 100644 --- a/jp.go +++ b/jp.go @@ -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" @@ -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.", @@ -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) @@ -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