Skip to content

Commit

Permalink
Make --no-color global (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Apr 26, 2021
1 parent 7263e5a commit 4b8fcb5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 33 deletions.
12 changes: 12 additions & 0 deletions internal/ansi/ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func Red(text string) string {
return color.Sprintf(color.Red(text))
}

// BrightRed returns text colored bright red
func BrightRed(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.BrightRed(text))
}

// Green returns text colored green
func Green(text string) string {
color := Color(os.Stdout)
Expand All @@ -100,6 +106,12 @@ func Yellow(text string) string {
return color.Sprintf(color.Yellow(text))
}

// BrightYellow returns text colored bright yellow
func BrightYellow(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.BrightYellow(text))
}

// Blue returns text colored blue
func Blue(text string) string {
color := Color(os.Stdout)
Expand Down
1 change: 1 addition & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type cli struct {
format string
force bool
noInput bool
noColor bool

// config state management.
initOnce sync.Once
Expand Down
14 changes: 2 additions & 12 deletions internal/cli/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ var (
ShortForm: "n",
Help: "Number of log entries to show.",
}

logsNoColor = Flag{
Name: "Disable Color",
LongForm: "no-color",
Help: "Disable colored log output.",
}
)

func logsCmd(cli *cli) *cobra.Command {
Expand All @@ -50,7 +44,6 @@ func listLogsCmd(cli *cli) *cobra.Command {
var inputs struct {
ClientID string
Num int
NoColor bool
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -88,22 +81,20 @@ auth0 logs ls -n 100`,
cli.api.ActionExecution, time.Second,
)

cli.renderer.LogList(list, logsCh, actionExecutionAPI, inputs.NoColor, !cli.debug)
cli.renderer.LogList(list, logsCh, actionExecutionAPI, !cli.debug)
return nil
},
}

logsClientID.RegisterString(cmd, &inputs.ClientID, "")
logsNum.RegisterInt(cmd, &inputs.Num, 100)
logsNoColor.RegisterBool(cmd, &inputs.NoColor, false)
return cmd
}

func tailLogsCmd(cli *cli) *cobra.Command {
var inputs struct {
ClientID string
Num int
NoColor bool
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -181,14 +172,13 @@ auth0 logs tail -n 100`,
cli.api.ActionExecution, time.Second,
)

cli.renderer.LogList(list, logsCh, actionExecutionAPI, inputs.NoColor, !cli.debug)
cli.renderer.LogList(list, logsCh, actionExecutionAPI, !cli.debug)
return nil
},
}

logsClientID.RegisterString(cmd, &inputs.ClientID, "")
logsNum.RegisterInt(cmd, &inputs.Num, 100)
logsNoColor.RegisterBool(cmd, &inputs.NoColor, false)
return cmd
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func Execute() {
return nil
}

ansi.DisableColors = cli.noColor

// Initialize everything once. Later callers can then
// freely assume that config is fully primed and ready
// to go.
Expand All @@ -82,6 +84,8 @@ func Execute() {
rootCmd.PersistentFlags().BoolVar(&cli.noInput,
"no-input", false, "Disable interactivity.")

rootCmd.PersistentFlags().BoolVar(&cli.noColor,
"no-color", false, "Disable colors.")
// order of the comamnds here matters
// so add new commands in a place that reflect its relevance or relation with other commands:
rootCmd.AddCommand(loginCmd(cli))
Expand Down
7 changes: 3 additions & 4 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/charmbracelet/glamour"
"github.com/logrusorgru/aurora"
"github.com/olekukonko/tablewriter"
)

Expand Down Expand Up @@ -45,17 +44,17 @@ func (r *Renderer) Newline() {
}

func (r *Renderer) Infof(format string, a ...interface{}) {
fmt.Fprint(r.MessageWriter, aurora.Green(" ▸ "))
fmt.Fprint(r.MessageWriter, ansi.Green(" ▸ "))
fmt.Fprintf(r.MessageWriter, format+"\n", a...)
}

func (r *Renderer) Warnf(format string, a ...interface{}) {
fmt.Fprint(r.MessageWriter, aurora.Yellow(" ▸ "))
fmt.Fprint(r.MessageWriter, ansi.Yellow(" ▸ "))
fmt.Fprintf(r.MessageWriter, format+"\n", a...)
}

func (r *Renderer) Errorf(format string, a ...interface{}) {
fmt.Fprint(r.MessageWriter, aurora.BrightRed(" ▸ "))
fmt.Fprint(r.MessageWriter, ansi.BrightRed(" ▸ "))
fmt.Fprintf(r.MessageWriter, format+"\n", a...)
}

Expand Down
30 changes: 13 additions & 17 deletions internal/display/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/logrusorgru/aurora"

"gopkg.in/auth0.v5/management"
"gopkg.in/yaml.v2"
Expand All @@ -26,8 +25,7 @@ type logCategory int
var _ View = &logView{}

type logView struct {
silent bool
noColor bool
silent bool
*management.Log

ActionExecutionAPI auth0.ActionExecutionAPI
Expand Down Expand Up @@ -130,23 +128,21 @@ func (v *logView) typeDesc() (typ, desc string) {

desc = fmt.Sprintf("%s %s", desc, auth0.StringValue(v.Description))

if !v.noColor {
switch v.category() {
case logCategorySuccess:
typ = aurora.Green(typ).String()
case logCategoryFailure:
typ = aurora.BrightRed(typ).String()
case logCategoryWarning:
typ = aurora.BrightYellow(typ).String()
default:
typ = ansi.Faint(typ)
}
switch v.category() {
case logCategorySuccess:
typ = ansi.Green(typ)
case logCategoryFailure:
typ = ansi.BrightRed(typ)
case logCategoryWarning:
typ = ansi.BrightYellow(typ)
default:
typ = ansi.Faint(typ)
}

return typ, desc
}

func (r *Renderer) LogList(logs []*management.Log, ch <-chan []*management.Log, api auth0.ActionExecutionAPI, noColor, silent bool) {
func (r *Renderer) LogList(logs []*management.Log, ch <-chan []*management.Log, api auth0.ActionExecutionAPI, silent bool) {
resource := "logs"

r.Heading(resource)
Expand All @@ -159,7 +155,7 @@ func (r *Renderer) LogList(logs []*management.Log, ch <-chan []*management.Log,

var res []View
for _, l := range logs {
res = append(res, &logView{Log: l, ActionExecutionAPI: api, silent: silent, noColor: noColor})
res = append(res, &logView{Log: l, ActionExecutionAPI: api, silent: silent})
}

var viewChan chan View
Expand All @@ -172,7 +168,7 @@ func (r *Renderer) LogList(logs []*management.Log, ch <-chan []*management.Log,

for list := range ch {
for _, l := range list {
viewChan <- &logView{Log: l, ActionExecutionAPI: api, silent: silent, noColor: noColor}
viewChan <- &logView{Log: l, ActionExecutionAPI: api, silent: silent}
}
}
}()
Expand Down

0 comments on commit 4b8fcb5

Please sign in to comment.