forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop.go
More file actions
73 lines (63 loc) · 1.88 KB
/
stop.go
File metadata and controls
73 lines (63 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package codespace
import (
"context"
"errors"
"fmt"
"github.com/cli/cli/v2/internal/codespaces/api"
"github.com/spf13/cobra"
)
func newStopCmd(app *App) *cobra.Command {
var codespace string
stopCmd := &cobra.Command{
Use: "stop",
Short: "Stop a running codespace",
Args: noArgsConstraint,
RunE: func(cmd *cobra.Command, args []string) error {
return app.StopCodespace(cmd.Context(), codespace)
},
}
stopCmd.Flags().StringVarP(&codespace, "codespace", "c", "", "Name of the codespace")
return stopCmd
}
func (a *App) StopCodespace(ctx context.Context, codespaceName string) error {
if codespaceName == "" {
a.StartProgressIndicatorWithLabel("Fetching codespaces")
codespaces, err := a.apiClient.ListCodespaces(ctx, -1)
a.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to list codespaces: %w", err)
}
var runningCodespaces []*api.Codespace
for _, c := range codespaces {
cs := codespace{c}
if cs.running() {
runningCodespaces = append(runningCodespaces, c)
}
}
if len(runningCodespaces) == 0 {
return errors.New("no running codespaces")
}
codespace, err := chooseCodespaceFromList(ctx, runningCodespaces)
if err != nil {
return fmt.Errorf("failed to choose codespace: %w", err)
}
codespaceName = codespace.Name
} else {
a.StartProgressIndicatorWithLabel("Fetching codespace")
c, err := a.apiClient.GetCodespace(ctx, codespaceName, false)
a.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get codespace: %q: %w", codespaceName, err)
}
cs := codespace{c}
if !cs.running() {
return fmt.Errorf("codespace %q is not running", codespaceName)
}
}
a.StartProgressIndicatorWithLabel("Stopping codespace")
defer a.StopProgressIndicator()
if err := a.apiClient.StopCodespace(ctx, codespaceName); err != nil {
return fmt.Errorf("failed to stop codespace: %w", err)
}
return nil
}