X Tutup
Skip to content

Commit 0635514

Browse files
authored
first stab
wip move to method param flushing out
1 parent c2a9c5a commit 0635514

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

internal/codespaces/api/api.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,60 @@ func (a *API) DeleteCodespace(ctx context.Context, codespaceName string) error {
686686
return nil
687687
}
688688

689+
type EditCodespaceParams struct {
690+
DisplayName string
691+
IdleTimeoutMinutes int
692+
Machine string
693+
}
694+
695+
type editRequest struct {
696+
DisplayName string `json:"display_name,omitempty"`
697+
IdleTimeoutMinutes int `json:"idle_timeout_minutes,omitempty"`
698+
Machine string `json:"machine"`
699+
}
700+
701+
func (a *API) EditCodespace(ctx context.Context, codespaceName string, params *EditCodespaceParams) (*Codespace, error) {
702+
requestBody, err := json.Marshal(editRequest{
703+
DisplayName: params.DisplayName,
704+
IdleTimeoutMinutes: params.IdleTimeoutMinutes,
705+
Machine: params.Machine,
706+
})
707+
fmt.Printf("requestBody: %s\n", requestBody)
708+
if err != nil {
709+
return nil, fmt.Errorf("error marshaling request: %w", err)
710+
}
711+
712+
req, err := http.NewRequest(http.MethodPatch, a.githubAPI+"/user/codespaces/"+codespaceName, bytes.NewBuffer(requestBody))
713+
if err != nil {
714+
return nil, fmt.Errorf("error creating request: %w", err)
715+
}
716+
717+
a.setHeaders(req)
718+
resp, err := a.do(ctx, req, "/user/codespaces")
719+
if err != nil {
720+
return nil, fmt.Errorf("error making request: %w", err)
721+
}
722+
defer resp.Body.Close()
723+
724+
if resp.StatusCode == http.StatusAccepted {
725+
return nil, errProvisioningInProgress // RPC finished before result of creation known
726+
} else if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
727+
return nil, api.HandleHTTPError(resp)
728+
}
729+
730+
b, err := ioutil.ReadAll(resp.Body)
731+
if err != nil {
732+
return nil, fmt.Errorf("error reading response body: %w", err)
733+
}
734+
735+
var response Codespace
736+
if err := json.Unmarshal(b, &response); err != nil {
737+
return nil, fmt.Errorf("error unmarshaling response: %w", err)
738+
}
739+
740+
return &response, nil
741+
}
742+
689743
type getCodespaceRepositoryContentsResponse struct {
690744
Content string `json:"content"`
691745
}

pkg/cmd/codespace/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type apiClient interface {
6262
StartCodespace(ctx context.Context, name string) error
6363
StopCodespace(ctx context.Context, name string) error
6464
CreateCodespace(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error)
65+
EditCodespace(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error)
6566
GetRepository(ctx context.Context, nwo string) (*api.Repository, error)
6667
AuthorizedKeys(ctx context.Context, user string) ([]byte, error)
6768
GetCodespaceRegionLocation(ctx context.Context) (string, error)

pkg/cmd/codespace/edit.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package codespace
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/cli/cli/v2/internal/codespaces/api"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
type editOptions struct {
13+
codespaceName string
14+
displayName string
15+
idleTimeout time.Duration
16+
machine string
17+
}
18+
19+
func newEditCmd(app *App) *cobra.Command {
20+
opts := editOptions{}
21+
22+
editCmd := &cobra.Command{
23+
Use: "edit",
24+
Short: "Edit a codespace",
25+
Args: noArgsConstraint,
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
return app.Edit(cmd.Context(), opts)
28+
},
29+
}
30+
31+
editCmd.Flags().StringVarP(&opts.codespaceName, "codespace", "c", "", "Name of the codespace")
32+
editCmd.Flags().StringVarP(&opts.displayName, "displayName", "d", "", "display name")
33+
editCmd.Flags().DurationVar(&opts.idleTimeout, "idle-timeout", 0, "allowed inactivity before codespace is stopped, e.g. \"10m\", \"1h\"")
34+
editCmd.Flags().StringVarP(&opts.machine, "machine", "m", "", "hardware specifications for the VM")
35+
36+
return editCmd
37+
}
38+
39+
// Edits a codespace
40+
func (a *App) Edit(ctx context.Context, opts editOptions) error {
41+
userInputs := struct {
42+
CodespaceName string
43+
DisplayName string
44+
IdleTimeout time.Duration
45+
SKU string
46+
}{
47+
CodespaceName: opts.codespaceName,
48+
DisplayName: opts.displayName,
49+
IdleTimeout: opts.idleTimeout,
50+
SKU: opts.machine,
51+
}
52+
a.StartProgressIndicatorWithLabel("Editing codespace")
53+
codespace, err := a.apiClient.EditCodespace(ctx, userInputs.CodespaceName, &api.EditCodespaceParams{
54+
DisplayName: userInputs.DisplayName,
55+
IdleTimeoutMinutes: int(userInputs.IdleTimeout.Minutes()),
56+
Machine: userInputs.SKU,
57+
})
58+
a.StopProgressIndicator()
59+
if err != nil {
60+
return fmt.Errorf("error editing codespace: %w", err)
61+
}
62+
63+
fmt.Fprintln(a.io.Out, codespace.Name)
64+
return nil
65+
}

pkg/cmd/codespace/mock_api.go

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/codespace/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func NewRootCmd(app *App) *cobra.Command {
1212

1313
root.AddCommand(newCodeCmd(app))
1414
root.AddCommand(newCreateCmd(app))
15+
root.AddCommand(newEditCmd(app))
1516
root.AddCommand(newDeleteCmd(app))
1617
root.AddCommand(newListCmd(app))
1718
root.AddCommand(newLogsCmd(app))

0 commit comments

Comments
 (0)
X Tutup