Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix workspace switch workflow #188

Merged
merged 4 commits into from Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this TRUE only when you current workspace is "" and you have only 1 workspace in cluster.
len(workspaces) == 1 && len(c.Workspace) == 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

near line 162, we are do same mistake only when you current workspace is ""

if len(workspaces) != 1 && len(c.Workspace) == 0 {
		fmt.Printf(messages.CLI_SET_WORKSPACE_EXAMPLE)
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest, we need to show message or switch every time when you relogin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✗ go run main.go auth login localhost
 CLUSTER                             WORKSPACE
 localhost                           cjsp2qct2000x0838cmbll6t3

 Switched cluster
Default "Andrii Soldatenko's Workspace" (cjsp2qct2000x0838cmbll6t3) workspace found, setting default workspace.

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