Skip to content

Commit ea0d0a5

Browse files
committed
Initial StopCodespace implementation
- API - Command
1 parent 7abf682 commit ea0d0a5

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

internal/codespaces/api/api.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,30 @@ func (a *API) StartCodespace(ctx context.Context, codespaceName string) error {
347347
return nil
348348
}
349349

350+
func (a *API) StopCodespace(ctx context.Context, codespaceName string) error {
351+
req, err := http.NewRequest(
352+
http.MethodPost,
353+
a.githubAPI+"/user/codespaces/"+codespaceName+"/stop",
354+
nil,
355+
)
356+
if err != nil {
357+
return fmt.Errorf("error creating request: %w", err)
358+
}
359+
360+
a.setHeaders(req)
361+
resp, err := a.do(ctx, req, "/user/codespaces/*/stop")
362+
if err != nil {
363+
return fmt.Errorf("error making request: %w", err)
364+
}
365+
defer resp.Body.Close()
366+
367+
if resp.StatusCode != http.StatusOK {
368+
return api.HandleHTTPError(resp)
369+
}
370+
371+
return nil
372+
}
373+
350374
type getCodespaceRegionLocationResponse struct {
351375
Current string `json:"current"`
352376
}

pkg/cmd/codespace/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type apiClient interface {
3838
ListCodespaces(ctx context.Context, limit int) ([]*api.Codespace, error)
3939
DeleteCodespace(ctx context.Context, name string) error
4040
StartCodespace(ctx context.Context, name string) error
41+
StopCodespace(ctx context.Context, name string) error
4142
CreateCodespace(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error)
4243
GetRepository(ctx context.Context, nwo string) (*api.Repository, error)
4344
AuthorizedKeys(ctx context.Context, user string) ([]byte, error)
@@ -251,3 +252,7 @@ func (c codespace) branchWithGitStatus() string {
251252
func (c codespace) hasUnsavedChanges() bool {
252253
return c.Environment.GitStatus.HasUncommitedChanges || c.Environment.GitStatus.HasUnpushedChanges
253254
}
255+
256+
func (c codespace) isRunning() bool {
257+
return c.Environment.State == api.CodespaceEnvironmentStateAvailable
258+
}

pkg/cmd/codespace/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ token to access the GitHub API with.`,
2525
root.AddCommand(newLogsCmd(app))
2626
root.AddCommand(newPortsCmd(app))
2727
root.AddCommand(newSSHCmd(app))
28+
root.AddCommand(newStopCmd(app))
2829

2930
return root
3031
}

pkg/cmd/codespace/stop.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package codespace
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/cli/cli/v2/internal/codespaces/api"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func newStopCmd(app *App) *cobra.Command {
13+
var codespace string
14+
15+
stopCmd := &cobra.Command{
16+
Use: "stop",
17+
Short: "Stop a running codespace",
18+
Args: noArgsConstraint,
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
return app.StopCodespace(cmd.Context(), codespace)
21+
},
22+
}
23+
stopCmd.Flags().StringVarP(&codespace, "codespace", "c", "", "Name of the codespace")
24+
25+
return stopCmd
26+
}
27+
28+
func (a *App) StopCodespace(ctx context.Context, codespaceName string) error {
29+
if codespaceName == "" {
30+
codespaces, err := a.apiClient.ListCodespaces(ctx, -1)
31+
if err != nil {
32+
return fmt.Errorf("failed to list codespace: %w", err)
33+
}
34+
35+
var runningCodespaces []*api.Codespace
36+
for _, c := range codespaces {
37+
cs := codespace{c}
38+
if cs.isRunning() {
39+
runningCodespaces = append(runningCodespaces, c)
40+
}
41+
}
42+
if len(runningCodespaces) == 0 {
43+
return errors.New("no running codespaces")
44+
}
45+
46+
codespace, err := chooseCodespaceFromList(ctx, runningCodespaces)
47+
if err != nil {
48+
return fmt.Errorf("failed to choose codespace: %w", err)
49+
}
50+
codespaceName = codespace.Name
51+
} else {
52+
c, err := a.apiClient.GetCodespace(ctx, codespaceName, false)
53+
if err != nil {
54+
return fmt.Errorf("failed to get codespace: %w", err)
55+
}
56+
cs := codespace{c}
57+
if !cs.isRunning() {
58+
return fmt.Errorf("codespace %q is not running", codespaceName)
59+
}
60+
}
61+
62+
if err := a.apiClient.StopCodespace(ctx, codespaceName); err != nil {
63+
return fmt.Errorf("failed to stop codespace: %w", err)
64+
}
65+
a.logger.Println("Codespace stopped")
66+
67+
return nil
68+
}

0 commit comments

Comments
 (0)