From 783f316df122c675292dfde458eba84bd68bf3b1 Mon Sep 17 00:00:00 2001 From: Oleg Solomka Date: Wed, 23 Mar 2022 00:26:50 +0000 Subject: [PATCH] [codespaces]: add hidden `select` command --- pkg/cmd/codespace/root.go | 1 + pkg/cmd/codespace/select.go | 33 +++++++++++++++ pkg/cmd/codespace/select_test.go | 69 ++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 pkg/cmd/codespace/select.go create mode 100644 pkg/cmd/codespace/select_test.go diff --git a/pkg/cmd/codespace/root.go b/pkg/cmd/codespace/root.go index 0a04d2fdd..662f4c2de 100644 --- a/pkg/cmd/codespace/root.go +++ b/pkg/cmd/codespace/root.go @@ -20,6 +20,7 @@ func NewRootCmd(app *App) *cobra.Command { root.AddCommand(newSSHCmd(app)) root.AddCommand(newCpCmd(app)) root.AddCommand(newStopCmd(app)) + root.AddCommand(newSelectCmd(app)) return root } diff --git a/pkg/cmd/codespace/select.go b/pkg/cmd/codespace/select.go new file mode 100644 index 000000000..88c01e211 --- /dev/null +++ b/pkg/cmd/codespace/select.go @@ -0,0 +1,33 @@ +package codespace + +import ( + "context" + "fmt" + + "github.com/spf13/cobra" +) + +func newSelectCmd(app *App) *cobra.Command { + codeCmd := &cobra.Command{ + Use: "select", + Short: "Select a codespace", + Hidden: true, + Args: noArgsConstraint, + RunE: func(cmd *cobra.Command, args []string) error { + return app.Select(cmd.Context(), "") + }, + } + + return codeCmd +} + +func (a *App) Select(ctx context.Context, name string) error { + codespace, err := getOrChooseCodespace(ctx, a.apiClient, name) + if err != nil { + return err + } + + a.io.Out.Write([]byte(fmt.Sprintf("%s\n", codespace.Name))); + + return nil +} diff --git a/pkg/cmd/codespace/select_test.go b/pkg/cmd/codespace/select_test.go new file mode 100644 index 000000000..09c162096 --- /dev/null +++ b/pkg/cmd/codespace/select_test.go @@ -0,0 +1,69 @@ +package codespace + +import ( + "context" + "errors" + "fmt" + "testing" + + "github.com/cli/cli/v2/internal/codespaces/api" + "github.com/cli/cli/v2/pkg/iostreams" +) + +const CODESPACE_NAME = "monalisa-cli-cli-abcdef" + +func TestApp_Select(t *testing.T) { + tests := []struct { + name string + arg string + wantErr bool + wantStdout string + wantStderr string + }{ + { + name: "Select a codespace", + arg: CODESPACE_NAME, + wantErr: false, + wantStdout: fmt.Sprintf("%s\n", CODESPACE_NAME), + }, + { + name: "Select a codespace error", + arg: "non-existent-codespace-name", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + io, _, stdout, stderr := iostreams.Test() + io.SetStdinTTY(true) + io.SetStdoutTTY(true) + a := NewApp(io, nil, testSelectApiMock(), nil) + + if err := a.Select(context.Background(), tt.arg); (err != nil) != tt.wantErr { + t.Errorf("App.Select() error = %v, wantErr %v", err, tt.wantErr) + } + + if out := stdout.String(); out != tt.wantStdout { + t.Errorf("stdout = %q, want %q", out, tt.wantStdout) + } + if out := sortLines(stderr.String()); out != tt.wantStderr { + t.Errorf("stderr = %q, want %q", out, tt.wantStderr) + } + }) + } +} + +func testSelectApiMock() *apiClientMock { + testingCodespace := &api.Codespace{ + Name: CODESPACE_NAME, + } + return &apiClientMock{ + GetCodespaceFunc: func(_ context.Context, name string, includeConnection bool) (*api.Codespace, error) { + if name == CODESPACE_NAME { + return testingCodespace, nil + } + + return nil, errors.New("Cannot find codespace.") + }, + } +}