Skip to content

Commit

Permalink
wip3
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Apr 17, 2023
1 parent fcd4f91 commit 8758798
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
57 changes: 27 additions & 30 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ type cli struct {
// 1. A tenant is found.
// 2. The tenant has an access token.
func (c *cli) setup(ctx context.Context) error {
if err := c.init(); err != nil {
if err := c.initializeAndValidateConfig(); err != nil {
return err
}

c.configureRenderer()

tenant, err := c.ensureTenantAccessTokenIsUpdated(ctx)
if err != nil {
return err
Expand All @@ -78,6 +80,30 @@ func (c *cli) setup(ctx context.Context) error {
return nil
}

func (c *cli) initializeAndValidateConfig() error {
if err := c.Config.Initialize(); err != nil {
return err
}

if err := c.Config.Validate(); err != nil {
return err
}

if c.tenant == "" {
c.tenant = c.Config.DefaultTenant
}

return nil
}

func (c *cli) configureRenderer() {
c.renderer.Tenant = c.tenant

if c.json {
c.renderer.Format = display.OutputFormatJSON
}
}

// ensureTenantAccessTokenIsUpdated loads the tenant, refreshing its token if necessary.
// The tenant access token needs a refresh if:
// 1. The tenant scopes are different than the currently required scopes.
Expand Down Expand Up @@ -122,35 +148,6 @@ func (c *cli) ensureTenantAccessTokenIsUpdated(ctx context.Context) (config.Tena
return t, nil
}

func (c *cli) init() error {

if err := c.Config.Initialize(); err != nil {
return err
}

if c.Config.DefaultTenant == "" && len(c.Config.Tenants) == 0 {
return nil // Nothing to remove.
}

if c.Config.DefaultTenant != "" && len(c.Config.Tenants) == 0 {
return nil // Nothing to remove.
}

if c.tenant == "" {
c.tenant = c.Config.DefaultTenant
}

c.renderer.Tenant = c.tenant

if c.json {
c.renderer.Format = display.OutputFormatJSON
}

c.renderer.Tenant = c.tenant

return nil
}

func canPrompt(cmd *cobra.Command) bool {
noInput, err := cmd.Root().Flags().GetBool("no-input")
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
// ErrConfigFileMissing is thrown when the config.json file is missing.
var ErrConfigFileMissing = errors.New("config.json file is missing")

// ErrNoAuthenticatedTenants is thrown when the config file has no authenticated tenants.
var ErrNoAuthenticatedTenants = errors.New("Not logged in. Try `auth0 login`.")

// Config holds cli configuration settings.
type Config struct {
onlyOnce sync.Once
Expand All @@ -37,6 +40,26 @@ func (c *Config) Initialize() error {
return c.initError
}

// Validate checks to see if the config is not corrupted,
// and we have an authenticated tenant saved.
// If we have at least one tenant saved but the DefaultTenant
// is empty, it will attempt to set the first available
// tenant as the DefaultTenant and save to disk.
func (c *Config) Validate() error {
if len(c.Tenants) == 0 {
return ErrNoAuthenticatedTenants
}

if c.DefaultTenant == "" {
for tenant := range c.Tenants {
c.DefaultTenant = tenant
break // Pick first tenant and exit.
}
}

return c.saveToDisk()
}

// AssignInstallID assigns and saves the installation ID to the config.
func (c *Config) AssignInstallID() error {
if c.InstallID != "" {
Expand Down

0 comments on commit 8758798

Please sign in to comment.