Skip to content

Commit

Permalink
Add support for colorized JSON output
Browse files Browse the repository at this point in the history
Add support for colorized JSON output via the
github.com/nwidger/jsoncolor package.  Colorized output can be
disabled via a new --monochrome flag.
  • Loading branch information
nwidger committed Feb 8, 2020
1 parent 7c1947b commit e6d4819
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ import (
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/nwidger/jsoncolor"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cube2222/jql/jql/app"
)

var cfgFile string
var (
cfgFile string
monochrome bool
)

type encoder interface {
Encode(v interface{}) error
SetIndent(prefix, indent string)
}

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -26,7 +35,12 @@ var rootCmd = &cobra.Command{
input := json.NewDecoder(bufio.NewReaderSize(os.Stdin, 4096*16))
w := bufio.NewWriterSize(os.Stdout, 4096*16)
defer w.Flush()
output := json.NewEncoder(w)
var output encoder
if monochrome {
output = json.NewEncoder(w)
} else {
output = jsoncolor.NewEncoder(w)
}
output.SetIndent("", " ")

app := app.NewApp(args[0], input, output)
Expand Down Expand Up @@ -54,6 +68,7 @@ func init() {
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.jql.yaml)")
rootCmd.PersistentFlags().BoolVar(&monochrome, "monochrome", false, "monochrome (don't colorize output)")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/cube2222/jql
go 1.13

require (
github.com/fatih/color v1.9.0 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/nwidger/jsoncolor v0.2.1
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.1
github.com/stretchr/testify v1.4.0
Expand Down

0 comments on commit e6d4819

Please sign in to comment.