diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 2cb4c5d1f..d220f93bb 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -2,6 +2,7 @@ package cli import ( "encoding/json" + "errors" "fmt" "io/ioutil" "os" @@ -31,6 +32,8 @@ type tenant struct { ExpiresAt time.Time `json:"expires_at"` } +var errUnauthenticated = errors.New("Not yet configured. Try `auth0 login`.") + // cli provides all the foundational things for all the commands in the CLI, // specifically: // @@ -84,7 +87,10 @@ func (c *cli) setup() error { return err } - if t.AccessToken != "" { + if t.AccessToken == "" { + return errUnauthenticated + + } else if t.AccessToken != "" { c.api, err = management.New(t.Domain, management.WithStaticToken(t.AccessToken), management.WithDebug(c.verbose)) @@ -177,7 +183,7 @@ func (c *cli) initContext() (err error) { } if _, err := os.Stat(c.path); os.IsNotExist(err) { - return fmt.Errorf("Not yet configured. Try `auth0 login`.") + return errUnauthenticated } var buf []byte @@ -190,7 +196,7 @@ func (c *cli) initContext() (err error) { } if c.tenant == "" && c.config.DefaultTenant == "" { - return fmt.Errorf("Not yet configured. Try `auth0 login`.") + return errUnauthenticated } if c.tenant == "" { diff --git a/internal/cli/root.go b/internal/cli/root.go index 9b80f6b57..585d3d491 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -16,7 +16,7 @@ func Execute() { // 2. global flag (e.g. --api-key) // 3. JSON file (e.g. api_key = "..." in ~/.config/auth0/config.json) cli := &cli{ - renderer: &display.Renderer{}, + renderer: display.NewRenderer(), } rootCmd := &cobra.Command{ diff --git a/internal/display/display.go b/internal/display/display.go index 92a14d7b1..ea352cd00 100644 --- a/internal/display/display.go +++ b/internal/display/display.go @@ -6,7 +6,6 @@ import ( "io" "os" "strings" - "sync" "time" "github.com/auth0/auth0-cli/internal/ansi" @@ -31,45 +30,31 @@ type Renderer struct { // Format indicates how the results are rendered. Default (empty) will write as table Format OutputFormat - - initOnce sync.Once } -func (r *Renderer) init() { - r.initOnce.Do(func() { - if r.MessageWriter == nil { - r.MessageWriter = os.Stderr - } - if r.ResultWriter == nil { - r.ResultWriter = os.Stdout - } - }) +func NewRenderer() *Renderer { + return &Renderer{ + MessageWriter: os.Stderr, + ResultWriter: os.Stdout, + } } func (r *Renderer) Infof(format string, a ...interface{}) { - r.init() - fmt.Fprint(r.MessageWriter, aurora.Green(" ▸ ")) fmt.Fprintf(r.MessageWriter, format+"\n", a...) } func (r *Renderer) Warnf(format string, a ...interface{}) { - r.init() - fmt.Fprint(r.MessageWriter, aurora.Yellow(" ▸ ")) fmt.Fprintf(r.MessageWriter, format+"\n", a...) } func (r *Renderer) Errorf(format string, a ...interface{}) { - r.init() - fmt.Fprint(r.MessageWriter, aurora.BrightRed(" ▸ ")) fmt.Fprintf(r.MessageWriter, format+"\n", a...) } func (r *Renderer) Heading(text ...string) { - r.init() - fmt.Fprintf(r.MessageWriter, "%s %s\n", ansi.Faint("==="), strings.Join(text, " ")) } @@ -79,8 +64,6 @@ type View interface { } func (r *Renderer) Results(data []View) { - r.init() - if len(data) > 0 { switch r.Format { case OutputFormatJSON: