Skip to content

Commit

Permalink
Refactor workspace switch to prompt if no ws uuid provided
Browse files Browse the repository at this point in the history
  • Loading branch information
andscoop committed Oct 8, 2018
1 parent 8476898 commit f44108a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/workspace.go
Expand Up @@ -48,7 +48,7 @@ var (
Aliases: []string{"sw"},
Short: "Switch to a different astronomer workspace",
Long: "Switch to a different astronomer workspace",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
RunE: workspaceSwitch,
}

Expand Down Expand Up @@ -187,5 +187,11 @@ func workspaceSwitch(cmd *cobra.Command, args []string) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true

return workspace.Switch(args[0])
uuid := ""

if len(args) == 1 {
uuid = args[0]
}

return workspace.Switch(uuid)
}
52 changes: 52 additions & 0 deletions workspace/workspace.go
Expand Up @@ -2,11 +2,13 @@ package workspace

import (
"fmt"
"strconv"

"github.com/pkg/errors"

"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/pkg/input"
"github.com/astronomerio/astro-cli/pkg/printutil"
)

Expand Down Expand Up @@ -105,8 +107,58 @@ func GetCurrentWorkspace() (string, error) {
return c.Workspace, nil
}

func getWorkspaceSelection() (string, error) {
tab.GetUserInput = true

req := houston.Request{
Query: houston.WorkspacesGetRequest,
}

r, err := req.Do()
if err != nil {
return "", err
}

ws := r.Data.GetWorkspaces

c, err := config.GetCurrentContext()
for _, w := range ws {
name := w.Label
workspace := w.Uuid

if c.Workspace == w.Uuid {
tab.AddRow([]string{name, workspace}, true)
} else {
tab.AddRow([]string{name, workspace}, false)
}
}

tab.Print()

in := input.InputText("\n> ")
i, err := strconv.ParseInt(
in,
10,
64,
)

if err != nil {
return "", errors.Wrapf(err, "cannot parse %s to int", in)
}

return ws[i-1].Uuid, nil
}

// Switch switches workspaces
func Switch(uuid string) error {
if len(uuid) == 0 {
_uuid, err := getWorkspaceSelection()
if err != nil {
return err
}

uuid = _uuid
}
// validate workspace
req := houston.Request{
Query: houston.WorkspacesGetRequest,
Expand Down

0 comments on commit f44108a

Please sign in to comment.