Skip to content

Commit

Permalink
Add isLoggedIn / errOnce concept
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Jan 23, 2021
1 parent d8758f5 commit 1e56c8b
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,28 @@ type cli struct {

// config state management.
initOnce sync.Once
errOnce error
path string
config config
}

// isLoggedIn encodes the domain logic for determining whether or not we're
// logged in. This might check our config storage, or just in memory.
func (c *cli) isLoggedIn() bool {
c.init()

return c.tenant != ""
}

// setup will try to initialize the config context, as well as figure out if
// there's a readily available tenant. A management API SDK instance is initialized IFF:
//
// 1. A tenant is found.
// 2. The tenant has an access token.
func (c *cli) setup() error {
c.init()
if err := c.init(); err != nil {
return err
}

t, err := c.getTenant()
if err != nil {
Expand Down Expand Up @@ -133,25 +144,26 @@ func (c *cli) setTenant(ten tenant) error {
}

func (c *cli) init() error {
var err error
c.initOnce.Do(func() {
// Initialize the context -- e.g. the configuration
// information, tenants, etc.
if err = c.initContext(); err != nil {
if c.errOnce = c.initContext(); c.errOnce != nil {
return
}
c.renderer.Tenant = c.tenant

// Determine what the desired output format is.
format := strings.ToLower(c.format)
if format != "" && format != string(display.OutputFormatJSON) {
err = fmt.Errorf("Invalid format. Use `--format=json` or omit this option to use the default format.")
c.errOnce = fmt.Errorf("Invalid format. Use `--format=json` or omit this option to use the default format.")
return
}
c.renderer.Format = display.OutputFormat(format)
})

return err
// Once initialized, we'll keep returning the same err that was
// originally encountered.
return c.errOnce
}

func (c *cli) initContext() (err error) {
Expand Down

0 comments on commit 1e56c8b

Please sign in to comment.