-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
executable file
·71 lines (59 loc) · 1.92 KB
/
edit.go
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
package codespace
import (
"context"
"fmt"
"github.com/ungtb10d/cli/v2/internal/codespaces/api"
"github.com/ungtb10d/cli/v2/pkg/cmdutil"
"github.com/spf13/cobra"
)
type editOptions struct {
codespaceName string
displayName string
machine string
}
func newEditCmd(app *App) *cobra.Command {
opts := editOptions{}
editCmd := &cobra.Command{
Use: "edit",
Short: "Edit a codespace",
Args: noArgsConstraint,
RunE: func(cmd *cobra.Command, args []string) error {
if opts.displayName == "" && opts.machine == "" {
return cmdutil.FlagErrorf("must provide `--display-name` or `--machine`")
}
return app.Edit(cmd.Context(), opts)
},
}
editCmd.Flags().StringVarP(&opts.codespaceName, "codespace", "c", "", "Name of the codespace")
editCmd.Flags().StringVarP(&opts.displayName, "display-name", "d", "", "Set the display name")
editCmd.Flags().StringVar(&opts.displayName, "displayName", "", "display name")
if err := editCmd.Flags().MarkDeprecated("displayName", "use `--display-name` instead"); err != nil {
fmt.Fprintf(app.io.ErrOut, "error marking flag as deprecated: %v\n", err)
}
editCmd.Flags().StringVarP(&opts.machine, "machine", "m", "", "Set hardware specifications for the VM")
return editCmd
}
// Edits a codespace
func (a *App) Edit(ctx context.Context, opts editOptions) error {
codespaceName := opts.codespaceName
if codespaceName == "" {
selectedCodespace, err := chooseCodespace(ctx, a.apiClient)
if err != nil {
if err == errNoCodespaces {
return err
}
return fmt.Errorf("error choosing codespace: %w", err)
}
codespaceName = selectedCodespace.Name
}
a.StartProgressIndicatorWithLabel("Editing codespace")
_, err := a.apiClient.EditCodespace(ctx, codespaceName, &api.EditCodespaceParams{
DisplayName: opts.displayName,
Machine: opts.machine,
})
a.StopProgressIndicator()
if err != nil {
return fmt.Errorf("error editing codespace: %w", err)
}
return nil
}