Skip to content

Commit

Permalink
wip 6
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Apr 17, 2023
1 parent 5fa32f8 commit 86f24cd
Show file tree
Hide file tree
Showing 4 changed files with 648 additions and 42 deletions.
22 changes: 5 additions & 17 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ type cli struct {
func (c *cli) setup(ctx context.Context) error {
cobra.EnableCommandSorting = false

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

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

c.configureRenderer()

tenant, err := c.ensureTenantAccessTokenIsUpdated(ctx)
Expand All @@ -82,22 +86,6 @@ 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

Expand Down
8 changes: 0 additions & 8 deletions internal/cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,6 @@ func RunLoginAsUser(ctx context.Context, cli *cli, additionalScopes []string) (c
return config.Tenant{}, fmt.Errorf("Failed to add the tenant to the config: %w", err)
}

if err := cli.Config.AssignInstallID(); err != nil {
return config.Tenant{}, fmt.Errorf("Failed to update the config: %w", err)
}

cli.tracker.TrackFirstLogin(cli.Config.InstallID)

if cli.Config.DefaultTenant != result.Domain {
Expand Down Expand Up @@ -298,10 +294,6 @@ func RunLoginAsMachine(ctx context.Context, inputs LoginInputs, cli *cli, cmd *c
cli.renderer.Infof("Successfully logged in.")
cli.renderer.Infof("Tenant: %s", inputs.Domain)

if err := cli.Config.AssignInstallID(); err != nil {
return fmt.Errorf("failed to update the config: %w", err)
}

cli.tracker.TrackFirstLogin(cli.Config.InstallID)

return nil
Expand Down
50 changes: 33 additions & 17 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,28 @@ func (c *Config) Initialize() error {
return c.initError
}

// Validate checks to see if the config is not corrupted,
// VerifyAuthentication 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
func (c *Config) VerifyAuthentication() error {
if err := c.Initialize(); err != nil {
return err
}

if c.DefaultTenant == "" {
for tenant := range c.Tenants {
c.DefaultTenant = tenant
break // Pick first tenant and exit.
}
if len(c.Tenants) == 0 {
return ErrNoAuthenticatedTenants
}

return c.saveToDisk()
}

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

c.InstallID = uuid.NewString()
for tenant := range c.Tenants {
c.DefaultTenant = tenant
break // Pick first tenant and exit.
}

return c.saveToDisk()
}
Expand All @@ -77,12 +72,16 @@ func (c *Config) IsLoggedInWithTenant(tenantName string) bool {
// be not logged in yet.
_ = c.Initialize()

if tenantName == "" {
tenantName = c.DefaultTenant
}

tenant, ok := c.Tenants[tenantName]
if !ok {
return false
}

token, err := jwt.ParseString(tenant.AccessToken)
token, err := jwt.ParseString(tenant.GetAccessToken())
if err != nil {
return false
}
Expand Down Expand Up @@ -119,6 +118,8 @@ func (c *Config) AddTenant(tenant Tenant) error {
// logging in the first time.
_ = c.Initialize()

c.ensureInstallIDAssigned()

if c.DefaultTenant == "" {
c.DefaultTenant = tenant.Domain
}
Expand Down Expand Up @@ -154,6 +155,8 @@ func (c *Config) RemoveTenant(tenant string) error {
delete(c.Tenants, tenant)

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

for otherTenant := range c.Tenants {
c.DefaultTenant = otherTenant
break // Pick first tenant and exit as we called delete above.
Expand All @@ -179,7 +182,12 @@ func (c *Config) ListAllTenants() ([]Tenant, error) {

// SaveNewDefaultTenant saves the new default tenant to the disk.
func (c *Config) SaveNewDefaultTenant(tenant string) error {
if err := c.Initialize(); err != nil {
return err
}

c.DefaultTenant = tenant

return c.saveToDisk()
}

Expand All @@ -196,6 +204,14 @@ func (c *Config) SaveNewDefaultAppIDForTenant(tenantName, appID string) error {
return c.saveToDisk()
}

func (c *Config) ensureInstallIDAssigned() {
if c.InstallID != "" {
return
}

c.InstallID = uuid.NewString()
}

func (c *Config) loadFromDisk() error {
if c.Path == "" {
c.Path = defaultPath()
Expand Down
Loading

0 comments on commit 86f24cd

Please sign in to comment.