Skip to content

Commit

Permalink
Fix workspace switch workflow (#188)
Browse files Browse the repository at this point in the history
* fix typo

* Added possible fix

* print set workspace if more than 1 workspace

* Attempt to use the last used workspace, unless it's been deleted, then show the menu to select.
  • Loading branch information
andriisoldatenko authored and schnie committed Mar 11, 2019
1 parent 0556496 commit f5cdab8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
33 changes: 29 additions & 4 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,19 @@ func getWorkspaceByLabel(label string) *houston.Workspace {
return nil
}

func switchToLastUsedWorkspace(c config.Context, workspaces []houston.Workspace) bool {
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 @@ -152,14 +166,25 @@ func Login(domain string, oAuthOnly bool) error {

workspaces := wsResp.Data.GetWorkspaces

if len(workspaces) == 1 && len(c.Workspace) == 0 {
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 && len(c.Workspace) == 0 {
fmt.Printf(messages.CLI_SET_WORKSPACE_EXAMPLE)
if len(workspaces) > 1 {
// 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
2 changes: 1 addition & 1 deletion cmd/auth.go
Expand Up @@ -12,7 +12,7 @@ var (

authRootCmd = &cobra.Command{
Use: "auth",
Short: "Mangage astronomer identity",
Short: "Manage astronomer identity",
Long: "Handles authentication to the Astronomer Platform",
}

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 f5cdab8

Please sign in to comment.