Skip to content

Commit

Permalink
Attempt to use the last used workspace, unless it's been deleted, the…
Browse files Browse the repository at this point in the history
…n show the menu to select.
  • Loading branch information
andriisoldatenko committed Mar 11, 2019
1 parent 4b1be78 commit d7244d7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
30 changes: 28 additions & 2 deletions auth/auth.go
Expand Up @@ -2,12 +2,13 @@ package auth

import (
"fmt"

"github.com/astronomer/astro-cli/cluster"
"github.com/astronomer/astro-cli/config"
"github.com/astronomer/astro-cli/docker"
"github.com/astronomer/astro-cli/houston"
"github.com/astronomer/astro-cli/messages"
"github.com/astronomer/astro-cli/pkg/input"
"github.com/astronomer/astro-cli/workspace"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -49,6 +50,20 @@ func getWorkspaceByLabel(label string) *houston.Workspace {
return nil
}

func switchToLastUsedWorkspace(c config.Context, workspaces []houston.Workspace) bool {
fmt.Printf("lastUsedWorkspaceID %v", c.LastUsedWorkspace)
if c.LastUsedWorkspace != "" {
for _, w := range workspaces {
if c.LastUsedWorkspace == w.Id {
fmt.Println(w.Id)
c.SetContextKey("workspace", w.Id)
return true
}
}
}
return false
}

// oAuth handles oAuth with houston api
func oAuth(oAuthUrl string) string {
fmt.Println("\n" + messages.HOUSTON_OAUTH_REDIRECT)
Expand Down Expand Up @@ -155,11 +170,22 @@ func Login(domain string, oAuthOnly bool) error {
if len(workspaces) == 1 {
w := workspaces[0]
c.SetContextKey("workspace", w.Id)
// update last used workspace ID
c.SetContextKey("last_used_workspace", w.Id)
fmt.Printf(messages.CONFIG_SET_DEFAULT_WORKSPACE, w.Label, w.Id)
}

if len(workspaces) > 1 {
fmt.Printf(messages.CLI_SET_WORKSPACE_EXAMPLE)
// try to switch to last used workspace in cluster
isSwitched := switchToLastUsedWorkspace(c, workspaces)

if !isSwitched {
// show switch menu with available workspace IDs
err := workspace.Switch("")
if err != nil {
fmt.Printf(messages.CLI_SET_WORKSPACE_EXAMPLE)
}
}
}

err = registryAuth()
Expand Down
10 changes: 6 additions & 4 deletions config/context.go
Expand Up @@ -22,12 +22,13 @@ type Contexts struct {

// Context represents a single cluster context
type Context struct {
Domain string `mapstructure:"domain"`
Workspace string `mapstructure:"workspace"`
Token string `mapstructure:"token"`
Domain string `mapstructure:"domain"`
Workspace string `mapstructure:"workspace"`
LastUsedWorkspace string `mapstructure:"last_used_workspace"`
Token string `mapstructure:"token"`
}

// GetCurrentContext looks up current context and gets cooresponding Context struct
// GetCurrentContext looks up current context and gets corresponding Context struct
func GetCurrentContext() (Context, error) {
c := Context{}

Expand Down Expand Up @@ -142,6 +143,7 @@ func (c Context) SetContext() error {
"token": c.Token,
"domain": c.Domain,
"workspace": c.Workspace,
"last_used_workspace": c.Workspace,
}

viperHome.Set("contexts"+"."+key, context)
Expand Down

0 comments on commit d7244d7

Please sign in to comment.