Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes so callers don't have to init #15

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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