From f752c98c930105fc42033c3633fc42045a563333 Mon Sep 17 00:00:00 2001 From: Springcomp Date: Mon, 20 Mar 2023 11:55:08 +0100 Subject: [PATCH 1/6] Added colorized output. Signed-off-by: Springcomp --- go.mod | 5 +++++ go.sum | 18 ++++++++++++++++++ jp.go | 14 +++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b818279..1dea824 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,17 @@ module github.com/jmespath-community/jp go 1.18 require ( + github.com/fatih/color v1.15.0 github.com/jmespath-community/go-jmespath v1.1.0 + github.com/nwidger/jsoncolor v0.3.1 github.com/urfave/cli v1.22.12 ) require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect + golang.org/x/sys v0.6.0 // indirect ) diff --git a/go.sum b/go.sum index 50f5c77..cafdeda 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,21 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/jmespath-community/go-jmespath v1.1.0 h1:jDWE/sGR5ELgzAczUNX/maQFmh3BeLsvQX/0PXZBrA0= github.com/jmespath-community/go-jmespath v1.1.0/go.mod h1:sY3q31u+PPDRp5yLcckgzg6eltf6Dw8nUkDYuRH8OtI= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/nwidger/jsoncolor v0.3.1 h1:5CZXA1TgqCjzKMyWP/WCuxKEzcC6f+HUlmJez9OKsHw= +github.com/nwidger/jsoncolor v0.3.1/go.mod h1:Cs34umxLbJvgBMnVNVqhji9BhoT/N/KinHqZptQ7cf4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= @@ -21,6 +34,11 @@ github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8= github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8= golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI= golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/jp.go b/jp.go index 8a90f6e..e467481 100644 --- a/jp.go +++ b/jp.go @@ -6,7 +6,9 @@ import ( "io/ioutil" "os" + "github.com/fatih/color" "github.com/jmespath-community/go-jmespath" + "github.com/nwidger/jsoncolor" "github.com/urfave/cli" ) @@ -32,6 +34,11 @@ func main() { Name: "expr-file, e", Usage: "Read JMESPath expression from the specified file.", }, + cli.StringFlag{ + Name: "color", + 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.", @@ -119,7 +126,12 @@ func runMain(c *cli.Context) int { if c.Bool("compact") { toJSON, err = json.Marshal(result) } else { - toJSON, err = json.MarshalIndent(result, "", " ") + 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) From c1b329a622a0d1ba8468de815d5048ccc1a852ea Mon Sep 17 00:00:00 2001 From: Springcomp Date: Mon, 20 Mar 2023 17:36:21 +0100 Subject: [PATCH 2/6] Colorized compact output. Signed-off-by: Springcomp --- go.mod | 4 +-- go.sum | 4 +-- jp.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 1dea824..cb509ff 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,14 @@ module github.com/jmespath-community/jp go 1.18 require ( - github.com/fatih/color v1.15.0 github.com/jmespath-community/go-jmespath v1.1.0 - github.com/nwidger/jsoncolor v0.3.1 + github.com/springcomp/jsoncolor v0.3.2-rc2 github.com/urfave/cli v1.22.12 ) require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/fatih/color v1.15.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect diff --git a/go.sum b/go.sum index cafdeda..de94d10 100644 --- a/go.sum +++ b/go.sum @@ -17,12 +17,12 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/nwidger/jsoncolor v0.3.1 h1:5CZXA1TgqCjzKMyWP/WCuxKEzcC6f+HUlmJez9OKsHw= -github.com/nwidger/jsoncolor v0.3.1/go.mod h1:Cs34umxLbJvgBMnVNVqhji9BhoT/N/KinHqZptQ7cf4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/springcomp/jsoncolor v0.3.2-rc2 h1:6iWfH3O8UW8NdyHg7wvH2R5Mwd7vrUXfBZdvQMyHnik= +github.com/springcomp/jsoncolor v0.3.2-rc2/go.mod h1:AsAsacerXZjz6F3kg/bhfPdIPY8Sz8Ny8+0J9niPYIM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/jp.go b/jp.go index e467481..5bff538 100644 --- a/jp.go +++ b/jp.go @@ -6,9 +6,8 @@ import ( "io/ioutil" "os" - "github.com/fatih/color" "github.com/jmespath-community/go-jmespath" - "github.com/nwidger/jsoncolor" + "github.com/springcomp/jsoncolor" "github.com/urfave/cli" ) @@ -78,6 +77,21 @@ func runMain(c *cli.Context) int { } expression = c.Args()[0] } + // NoColor defines if the output is colorized or not. It's dynamically set to + // false or true based on the stdout's file descriptor referring to a terminal + // or not. It's also set to true if the NO_COLOR environment variable is + // set (regardless of its value). This is a global option and affects all + // colors. + switch c.String("color") { + case "always": + EnableColor(true) + case "auto": + // this is the default in the library + case "never": + EnableColor(false) + default: + return errMsg("Invalid color specification. Must use always/auto/never") + } if c.Bool("ast") { parser := jmespath.NewParser() parsed, err := parser.Parse(expression) @@ -123,16 +137,8 @@ func runMain(c *cli.Context) int { os.Stdout.WriteString(converted) } else { var toJSON []byte - if c.Bool("compact") { - toJSON, err = json.Marshal(result) - } else { - if color.NoColor { - // avoid doing the extra processing in jsoncolor - toJSON, err = json.MarshalIndent(result, "", " ") - } else { - toJSON, err = jsoncolor.MarshalIndent(result, "", " ") - } - } + var jsonWriter = getJSONWriter(c.Bool("compact")) + toJSON, err = jsonWriter(result) if err != nil { errMsg("Error marshalling result to JSON: %s\n", err) return 3 @@ -142,3 +148,53 @@ func runMain(c *cli.Context) int { os.Stdout.WriteString("\n") return 0 } + +func EnableColor(enabled bool) { + + if enabled { + jsoncolor.DefaultArrayColor.EnableColor() + jsoncolor.DefaultColonColor.EnableColor() + jsoncolor.DefaultCommaColor.EnableColor() + jsoncolor.DefaultFalseColor.EnableColor() + jsoncolor.DefaultFieldColor.EnableColor() + jsoncolor.DefaultFieldQuoteColor.EnableColor() + jsoncolor.DefaultNullColor.EnableColor() + jsoncolor.DefaultNumberColor.EnableColor() + jsoncolor.DefaultObjectColor.EnableColor() + jsoncolor.DefaultSpaceColor.EnableColor() + jsoncolor.DefaultStringColor.EnableColor() + jsoncolor.DefaultStringQuoteColor.EnableColor() + jsoncolor.DefaultTrueColor.EnableColor() + + } else { + jsoncolor.DefaultArrayColor.DisableColor() + jsoncolor.DefaultColonColor.DisableColor() + jsoncolor.DefaultCommaColor.DisableColor() + jsoncolor.DefaultFalseColor.DisableColor() + jsoncolor.DefaultFieldColor.DisableColor() + jsoncolor.DefaultFieldQuoteColor.DisableColor() + jsoncolor.DefaultNullColor.DisableColor() + jsoncolor.DefaultNumberColor.DisableColor() + jsoncolor.DefaultObjectColor.DisableColor() + jsoncolor.DefaultSpaceColor.DisableColor() + jsoncolor.DefaultStringColor.DisableColor() + jsoncolor.DefaultStringQuoteColor.DisableColor() + jsoncolor.DefaultTrueColor.DisableColor() + } +} + +func getJSONWriter(compact bool) func(v interface{}) ([]byte, error) { + var writers = [2]func(v interface{}) ([]byte, error){ + func(v interface{}) ([]byte, error) { return jsoncolor.MarshalIndent(v, "", " ") }, + jsoncolor.Marshal, + } + return writers[bool2int(compact)] +} + +func bool2int(b bool) int { + if b { + return 1 + } else { + return 0 + } +} From ad910492645ca4a2173e86ed9cb0d756d0cb4be7 Mon Sep 17 00:00:00 2001 From: Springcomp Date: Tue, 21 Mar 2023 11:30:27 +0100 Subject: [PATCH 3/6] Using simpler code. Signed-off-by: Springcomp --- jp.go | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/jp.go b/jp.go index 5bff538..84e86c7 100644 --- a/jp.go +++ b/jp.go @@ -137,8 +137,11 @@ func runMain(c *cli.Context) int { os.Stdout.WriteString(converted) } else { var toJSON []byte - var jsonWriter = getJSONWriter(c.Bool("compact")) - toJSON, err = jsonWriter(result) + if c.Bool("compact") { + toJSON, err = jsoncolor.Marshal(result) + } else { + toJSON, err = jsoncolor.MarshalIndent(result, "", " ") + } if err != nil { errMsg("Error marshalling result to JSON: %s\n", err) return 3 @@ -182,19 +185,3 @@ func EnableColor(enabled bool) { jsoncolor.DefaultTrueColor.DisableColor() } } - -func getJSONWriter(compact bool) func(v interface{}) ([]byte, error) { - var writers = [2]func(v interface{}) ([]byte, error){ - func(v interface{}) ([]byte, error) { return jsoncolor.MarshalIndent(v, "", " ") }, - jsoncolor.Marshal, - } - return writers[bool2int(compact)] -} - -func bool2int(b bool) int { - if b { - return 1 - } else { - return 0 - } -} From ea85366dba23c62359eeaaca906d2651f31b626d Mon Sep 17 00:00:00 2001 From: Springcomp Date: Tue, 21 Mar 2023 11:37:18 +0100 Subject: [PATCH 4/6] Made function private (really this time) Signed-off-by: Springcomp --- jp.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jp.go b/jp.go index 84e86c7..225ba31 100644 --- a/jp.go +++ b/jp.go @@ -84,11 +84,11 @@ func runMain(c *cli.Context) int { // colors. switch c.String("color") { case "always": - EnableColor(true) + enableColor(true) case "auto": // this is the default in the library case "never": - EnableColor(false) + enableColor(false) default: return errMsg("Invalid color specification. Must use always/auto/never") } @@ -152,7 +152,7 @@ func runMain(c *cli.Context) int { return 0 } -func EnableColor(enabled bool) { +func enableColor(enabled bool) { if enabled { jsoncolor.DefaultArrayColor.EnableColor() From 821337df1ebe185606711be4726342159104b7b6 Mon Sep 17 00:00:00 2001 From: Springcomp Date: Tue, 21 Mar 2023 11:44:08 +0100 Subject: [PATCH 5/6] Documented NO_COLOR environment variable. Signed-off-by: Springcomp --- jp.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/jp.go b/jp.go index 225ba31..220bdea 100644 --- a/jp.go +++ b/jp.go @@ -77,16 +77,15 @@ func runMain(c *cli.Context) int { } expression = c.Args()[0] } - // NoColor defines if the output is colorized or not. It's dynamically set to - // false or true based on the stdout's file descriptor referring to a terminal - // or not. It's also set to true if the NO_COLOR environment variable is - // set (regardless of its value). This is a global option and affects all - // colors. switch c.String("color") { case "always": enableColor(true) case "auto": - // this is the default in the library + // this requests the default behaviour in the jsoncolor library + // color output is enabled or disabled dynamically based on the + // stdout's file descriptor referring to a terminal or not. + // Additionally, if the NO_COLOR environment variable is set + // (regardless of its value) color output will be disabled. case "never": enableColor(false) default: From 6304c72d769124287eb41d1082d112a71b849f9f Mon Sep 17 00:00:00 2001 From: Springcomp Date: Sat, 25 Mar 2023 11:39:51 +0100 Subject: [PATCH 6/6] Using updated upstream jsoncolor. Signed-off-by: Springcomp --- go.mod | 2 +- go.sum | 4 ++-- jp.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index cb509ff..2b5072e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.18 require ( github.com/jmespath-community/go-jmespath v1.1.0 - github.com/springcomp/jsoncolor v0.3.2-rc2 github.com/urfave/cli v1.22.12 ) @@ -13,6 +12,7 @@ require ( github.com/fatih/color v1.15.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect + github.com/nwidger/jsoncolor v0.3.2 github.com/russross/blackfriday/v2 v2.1.0 // indirect golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect golang.org/x/sys v0.6.0 // indirect diff --git a/go.sum b/go.sum index de94d10..84612eb 100644 --- a/go.sum +++ b/go.sum @@ -17,12 +17,12 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/nwidger/jsoncolor v0.3.2 h1:rVJJlwAWDJShnbTYOQ5RM7yTA20INyKXlJ/fg4JMhHQ= +github.com/nwidger/jsoncolor v0.3.2/go.mod h1:Cs34umxLbJvgBMnVNVqhji9BhoT/N/KinHqZptQ7cf4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/springcomp/jsoncolor v0.3.2-rc2 h1:6iWfH3O8UW8NdyHg7wvH2R5Mwd7vrUXfBZdvQMyHnik= -github.com/springcomp/jsoncolor v0.3.2-rc2/go.mod h1:AsAsacerXZjz6F3kg/bhfPdIPY8Sz8Ny8+0J9niPYIM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/jp.go b/jp.go index 220bdea..80a9f07 100644 --- a/jp.go +++ b/jp.go @@ -7,7 +7,7 @@ import ( "os" "github.com/jmespath-community/go-jmespath" - "github.com/springcomp/jsoncolor" + "github.com/nwidger/jsoncolor" "github.com/urfave/cli" )