Skip to content

Commit 8bf0cb8

Browse files
committed
Refactor the getOrChooseCodespace to always check for pending ops
1 parent 10e43b5 commit 8bf0cb8

File tree

8 files changed

+13
-47
lines changed

8 files changed

+13
-47
lines changed

pkg/cmd/codespace/code.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ func (a *App) VSCode(ctx context.Context, codespaceName string, useInsiders bool
3636
return fmt.Errorf("get or choose codespace: %w", err)
3737
}
3838

39-
if codespace.PendingOperation {
40-
return fmt.Errorf(
41-
"codespace is disabled while it has a pending operation: %s",
42-
codespace.PendingOperationDisabledReason,
43-
)
44-
}
45-
4639
url := vscodeProtocolURL(codespace.Name, useInsiders)
4740
if err := a.browser.Browse(url); err != nil {
4841
return fmt.Errorf("error opening Visual Studio Code: %w", err)

pkg/cmd/codespace/code_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestPendingOperationDisallowsCode(t *testing.T) {
5858
app := testingLogsApp()
5959

6060
if err := app.VSCode(context.Background(), "disabledCodespace", false); err != nil {
61-
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
61+
if err.Error() != "get or choose codespace: codespace is disabled while it has a pending operation: Some pending operation" {
6262
t.Errorf("expected pending operation error, but got: %v", err)
6363
}
6464
} else {

pkg/cmd/codespace/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ func getOrChooseCodespace(ctx context.Context, apiClient apiClient, codespaceNam
183183
}
184184
}
185185

186+
if codespace.PendingOperation {
187+
return nil, fmt.Errorf(
188+
"codespace is disabled while it has a pending operation: %s",
189+
codespace.PendingOperationDisabledReason,
190+
)
191+
}
192+
186193
return codespace, nil
187194
}
188195

pkg/cmd/codespace/logs.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ func (a *App) Logs(ctx context.Context, codespaceName string, follow bool) (err
4141
return fmt.Errorf("get or choose codespace: %w", err)
4242
}
4343

44-
if codespace.PendingOperation {
45-
return fmt.Errorf(
46-
"codespace is disabled while it has a pending operation: %s",
47-
codespace.PendingOperationDisabledReason,
48-
)
49-
}
50-
5144
authkeys := make(chan error, 1)
5245
go func() {
5346
authkeys <- checkAuthorizedKeys(ctx, a.apiClient)

pkg/cmd/codespace/logs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestPendingOperationDisallowsLogs(t *testing.T) {
1212
app := testingLogsApp()
1313

1414
if err := app.Logs(context.Background(), "disabledCodespace", false); err != nil {
15-
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
15+
if err.Error() != "get or choose codespace: codespace is disabled while it has a pending operation: Some pending operation" {
1616
t.Errorf("expected pending operation error, but got: %v", err)
1717
}
1818
} else {

pkg/cmd/codespace/ports.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func newPortsCmd(app *App) *cobra.Command {
4646

4747
// ListPorts lists known ports in a codespace.
4848
func (a *App) ListPorts(ctx context.Context, codespaceName string, exporter cmdutil.Exporter) (err error) {
49-
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
49+
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
5050
if err != nil {
5151
return err
5252
}
@@ -231,7 +231,7 @@ func (a *App) UpdatePortVisibility(ctx context.Context, codespaceName string, ar
231231
return fmt.Errorf("error parsing port arguments: %w", err)
232232
}
233233

234-
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
234+
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
235235
if err != nil {
236236
return err
237237
}
@@ -304,7 +304,7 @@ func (a *App) ForwardPorts(ctx context.Context, codespaceName string, ports []st
304304
return fmt.Errorf("get port pairs: %w", err)
305305
}
306306

307-
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
307+
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
308308
if err != nil {
309309
return err
310310
}
@@ -370,23 +370,3 @@ func normalizeJSON(j []byte) []byte {
370370
// remove trailing commas
371371
return bytes.ReplaceAll(j, []byte("},}"), []byte("}}"))
372372
}
373-
374-
func getCodespaceForPorts(ctx context.Context, apiClient apiClient, codespaceName string) (*api.Codespace, error) {
375-
codespace, err := getOrChooseCodespace(ctx, apiClient, codespaceName)
376-
if err != nil {
377-
// TODO(josebalius): remove special handling of this error here and it other places
378-
if err == errNoCodespaces {
379-
return nil, err
380-
}
381-
return nil, fmt.Errorf("error choosing codespace: %w", err)
382-
}
383-
384-
if codespace.PendingOperation {
385-
return nil, fmt.Errorf(
386-
"codespace is disabled while it has a pending operation: %s",
387-
codespace.PendingOperationDisabledReason,
388-
)
389-
}
390-
391-
return codespace, nil
392-
}

pkg/cmd/codespace/ssh.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,6 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
128128
return fmt.Errorf("get or choose codespace: %w", err)
129129
}
130130

131-
if codespace.PendingOperation {
132-
return fmt.Errorf(
133-
"codespace is disabled while it has a pending operation: %s",
134-
codespace.PendingOperationDisabledReason,
135-
)
136-
}
137-
138131
liveshareLogger := noopLogger()
139132
if opts.debug {
140133
debugLogger, err := newFileLogger(opts.debugFile)

pkg/cmd/codespace/ssh_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestPendingOperationDisallowsSSH(t *testing.T) {
1212
app := testingSSHApp()
1313

1414
if err := app.SSH(context.Background(), []string{}, sshOptions{codespace: "disabledCodespace"}); err != nil {
15-
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
15+
if err.Error() != "get or choose codespace: codespace is disabled while it has a pending operation: Some pending operation" {
1616
t.Errorf("expected pending operation error, but got: %v", err)
1717
}
1818
} else {

0 commit comments

Comments
 (0)