Skip to content

Commit

Permalink
Merge pull request #55 from lacework/afiune/windows/fix-colors
Browse files Browse the repository at this point in the history
fix(cli): support colors for windows
  • Loading branch information
afiune authored Apr 20, 2020
2 parents 3bdce00 + ea48379 commit 26ad81c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cli/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -101,7 +102,7 @@ func runApiCommand(_ *cobra.Command, args []string) error {
return errors.Wrap(err, "unable to format json response")
}

fmt.Println(string(pretty))
fmt.Fprintln(color.Output, string(pretty))
return nil
}

Expand Down
11 changes: 10 additions & 1 deletion cli/cmd/cli_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package cmd
import (
"fmt"

"github.com/fatih/color"
prettyjson "github.com/hokaccha/go-prettyjson"
"github.com/pkg/errors"
"github.com/spf13/viper"
Expand All @@ -46,7 +47,15 @@ type cliState struct {
func NewDefaultState() cliState {
return cliState{
Profile: "default",
JsonF: prettyjson.NewFormatter(),
JsonF: &prettyjson.Formatter{
KeyColor: color.New(color.FgCyan, color.Bold),
StringColor: color.New(color.FgGreen, color.Bold),
BoolColor: color.New(color.FgYellow, color.Bold),
NumberColor: color.New(color.FgRed, color.Bold),
NullColor: color.New(color.FgWhite, color.Bold),
Indent: 2,
Newline: "\n",
},
}
}

Expand Down
8 changes: 8 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func init() {
rootCmd.PersistentFlags().Bool("debug", false,
"turn on debug logging",
)
rootCmd.PersistentFlags().Bool("nocolor", false,
"turn off colors",
)
rootCmd.PersistentFlags().StringP("profile", "p", "",
"switch between profiles configured at ~/.lacework.toml",
)
Expand All @@ -93,6 +96,7 @@ func init() {
)

errcheckWARN(viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug")))
errcheckWARN(viper.BindPFlag("nocolor", rootCmd.PersistentFlags().Lookup("nocolor")))
errcheckWARN(viper.BindPFlag("profile", rootCmd.PersistentFlags().Lookup("profile")))
errcheckWARN(viper.BindPFlag("account", rootCmd.PersistentFlags().Lookup("account")))
errcheckWARN(viper.BindPFlag("api_key", rootCmd.PersistentFlags().Lookup("api_key")))
Expand All @@ -117,6 +121,10 @@ func initConfig() {
cli.LogLevel = "DEBUG"
}

if viper.GetBool("nocolor") {
cli.JsonF.DisabledColor = true
}

// by default the cli logs are going to be visualized in
// a console format unless the user wants the opposite
if os.Getenv("LW_LOG_FORMAT") == "" {
Expand Down

0 comments on commit 26ad81c

Please sign in to comment.