From f44108a846d91eb2296e4b0438f1105aa021a482 Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Mon, 8 Oct 2018 16:25:00 -0400 Subject: [PATCH] Refactor workspace switch to prompt if no ws uuid provided --- cmd/workspace.go | 10 ++++++-- workspace/workspace.go | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/cmd/workspace.go b/cmd/workspace.go index ccf792ca7..13d143ee4 100644 --- a/cmd/workspace.go +++ b/cmd/workspace.go @@ -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, } @@ -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) } diff --git a/workspace/workspace.go b/workspace/workspace.go index 2db73d40b..dee013e4a 100644 --- a/workspace/workspace.go +++ b/workspace/workspace.go @@ -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" ) @@ -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,