Skip to content

Commit

Permalink
Merge pull request #15 from auth0/change-to-new-renderer
Browse files Browse the repository at this point in the history
Changes so callers don't have to init
  • Loading branch information
cyx authored Jan 25, 2021
2 parents 70b0842 + 33ac735 commit 3d2e26f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
12 changes: 9 additions & 3 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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:
//
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
27 changes: 5 additions & 22 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"strings"
"sync"
"time"

"github.com/auth0/auth0-cli/internal/ansi"
Expand All @@ -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, " "))
}

Expand All @@ -79,8 +64,6 @@ type View interface {
}

func (r *Renderer) Results(data []View) {
r.init()

if len(data) > 0 {
switch r.Format {
case OutputFormatJSON:
Expand Down

0 comments on commit 3d2e26f

Please sign in to comment.