Refactor the getOrChooseCodespace to always check for pending ops

This commit is contained in:
Charlie Andrews 2022-03-16 08:51:33 -04:00
parent 10e43b5136
commit 8bf0cb8f13
8 changed files with 13 additions and 47 deletions

View file

@ -36,13 +36,6 @@ func (a *App) VSCode(ctx context.Context, codespaceName string, useInsiders bool
return fmt.Errorf("get or choose codespace: %w", err)
}
if codespace.PendingOperation {
return fmt.Errorf(
"codespace is disabled while it has a pending operation: %s",
codespace.PendingOperationDisabledReason,
)
}
url := vscodeProtocolURL(codespace.Name, useInsiders)
if err := a.browser.Browse(url); err != nil {
return fmt.Errorf("error opening Visual Studio Code: %w", err)

View file

@ -58,7 +58,7 @@ func TestPendingOperationDisallowsCode(t *testing.T) {
app := testingLogsApp()
if err := app.VSCode(context.Background(), "disabledCodespace", false); err != nil {
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
if err.Error() != "get or choose codespace: codespace is disabled while it has a pending operation: Some pending operation" {
t.Errorf("expected pending operation error, but got: %v", err)
}
} else {

View file

@ -183,6 +183,13 @@ func getOrChooseCodespace(ctx context.Context, apiClient apiClient, codespaceNam
}
}
if codespace.PendingOperation {
return nil, fmt.Errorf(
"codespace is disabled while it has a pending operation: %s",
codespace.PendingOperationDisabledReason,
)
}
return codespace, nil
}

View file

@ -41,13 +41,6 @@ func (a *App) Logs(ctx context.Context, codespaceName string, follow bool) (err
return fmt.Errorf("get or choose codespace: %w", err)
}
if codespace.PendingOperation {
return fmt.Errorf(
"codespace is disabled while it has a pending operation: %s",
codespace.PendingOperationDisabledReason,
)
}
authkeys := make(chan error, 1)
go func() {
authkeys <- checkAuthorizedKeys(ctx, a.apiClient)

View file

@ -12,7 +12,7 @@ func TestPendingOperationDisallowsLogs(t *testing.T) {
app := testingLogsApp()
if err := app.Logs(context.Background(), "disabledCodespace", false); err != nil {
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
if err.Error() != "get or choose codespace: codespace is disabled while it has a pending operation: Some pending operation" {
t.Errorf("expected pending operation error, but got: %v", err)
}
} else {

View file

@ -46,7 +46,7 @@ func newPortsCmd(app *App) *cobra.Command {
// ListPorts lists known ports in a codespace.
func (a *App) ListPorts(ctx context.Context, codespaceName string, exporter cmdutil.Exporter) (err error) {
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
if err != nil {
return err
}
@ -231,7 +231,7 @@ func (a *App) UpdatePortVisibility(ctx context.Context, codespaceName string, ar
return fmt.Errorf("error parsing port arguments: %w", err)
}
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
if err != nil {
return err
}
@ -304,7 +304,7 @@ func (a *App) ForwardPorts(ctx context.Context, codespaceName string, ports []st
return fmt.Errorf("get port pairs: %w", err)
}
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
if err != nil {
return err
}
@ -370,23 +370,3 @@ func normalizeJSON(j []byte) []byte {
// remove trailing commas
return bytes.ReplaceAll(j, []byte("},}"), []byte("}}"))
}
func getCodespaceForPorts(ctx context.Context, apiClient apiClient, codespaceName string) (*api.Codespace, error) {
codespace, err := getOrChooseCodespace(ctx, apiClient, codespaceName)
if err != nil {
// TODO(josebalius): remove special handling of this error here and it other places
if err == errNoCodespaces {
return nil, err
}
return nil, fmt.Errorf("error choosing codespace: %w", err)
}
if codespace.PendingOperation {
return nil, fmt.Errorf(
"codespace is disabled while it has a pending operation: %s",
codespace.PendingOperationDisabledReason,
)
}
return codespace, nil
}

View file

@ -128,13 +128,6 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
return fmt.Errorf("get or choose codespace: %w", err)
}
if codespace.PendingOperation {
return fmt.Errorf(
"codespace is disabled while it has a pending operation: %s",
codespace.PendingOperationDisabledReason,
)
}
liveshareLogger := noopLogger()
if opts.debug {
debugLogger, err := newFileLogger(opts.debugFile)

View file

@ -12,7 +12,7 @@ func TestPendingOperationDisallowsSSH(t *testing.T) {
app := testingSSHApp()
if err := app.SSH(context.Background(), []string{}, sshOptions{codespace: "disabledCodespace"}); err != nil {
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
if err.Error() != "get or choose codespace: codespace is disabled while it has a pending operation: Some pending operation" {
t.Errorf("expected pending operation error, but got: %v", err)
}
} else {