Merge branch 'trunk' of https://github.com/meiji163/cli into pin-ext

This commit is contained in:
meiji163 2022-03-07 16:53:53 -08:00
commit 95fa7f97cd
10 changed files with 353 additions and 7 deletions

View file

@ -68,6 +68,7 @@ type apiClient interface {
StartCodespace(ctx context.Context, name string) error
StopCodespace(ctx context.Context, name string) error
CreateCodespace(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error)
EditCodespace(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error)
GetRepository(ctx context.Context, nwo string) (*api.Repository, error)
AuthorizedKeys(ctx context.Context, user string) ([]byte, error)
GetCodespaceRegionLocation(ctx context.Context) (string, error)

64
pkg/cmd/codespace/edit.go Normal file
View file

@ -0,0 +1,64 @@
package codespace
import (
"context"
"fmt"
"time"
"github.com/cli/cli/v2/internal/codespaces/api"
"github.com/spf13/cobra"
)
type editOptions struct {
codespaceName string
displayName string
idleTimeout time.Duration
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 {
return app.Edit(cmd.Context(), opts)
},
}
editCmd.Flags().StringVarP(&opts.codespaceName, "codespace", "c", "", "Name of the codespace")
editCmd.Flags().StringVarP(&opts.displayName, "displayName", "d", "", "display name")
editCmd.Flags().DurationVar(&opts.idleTimeout, "idle-timeout", 0, "allowed inactivity before codespace is stopped, e.g. \"10m\", \"1h\"")
editCmd.Flags().StringVarP(&opts.machine, "machine", "m", "", "hardware specifications for the VM")
return editCmd
}
// Edits a codespace
func (a *App) Edit(ctx context.Context, opts editOptions) error {
userInputs := struct {
CodespaceName string
DisplayName string
IdleTimeout time.Duration
SKU string
}{
CodespaceName: opts.codespaceName,
DisplayName: opts.displayName,
IdleTimeout: opts.idleTimeout,
SKU: opts.machine,
}
a.StartProgressIndicatorWithLabel("Editing codespace")
_, err := a.apiClient.EditCodespace(ctx, userInputs.CodespaceName, &api.EditCodespaceParams{
DisplayName: userInputs.DisplayName,
IdleTimeoutMinutes: int(userInputs.IdleTimeout.Minutes()),
Machine: userInputs.SKU,
})
a.StopProgressIndicator()
if err != nil {
return fmt.Errorf("error editing codespace: %w", err)
}
return nil
}

View file

@ -25,6 +25,9 @@ import (
// DeleteCodespaceFunc: func(ctx context.Context, name string) error {
// panic("mock out the DeleteCodespace method")
// },
// EditCodespaceFunc: func(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error) {
// panic("mock out the EditCodespace method")
// },
// GetCodespaceFunc: func(ctx context.Context, name string, includeConnection bool) (*api.Codespace, error) {
// panic("mock out the GetCodespace method")
// },
@ -71,6 +74,9 @@ type apiClientMock struct {
// DeleteCodespaceFunc mocks the DeleteCodespace method.
DeleteCodespaceFunc func(ctx context.Context, name string) error
// EditCodespaceFunc mocks the EditCodespace method.
EditCodespaceFunc func(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error)
// GetCodespaceFunc mocks the GetCodespace method.
GetCodespaceFunc func(ctx context.Context, name string, includeConnection bool) (*api.Codespace, error)
@ -124,6 +130,15 @@ type apiClientMock struct {
// Name is the name argument value.
Name string
}
// EditCodespace holds details about calls to the EditCodespace method.
EditCodespace []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// CodespaceName is the codespaceName argument value.
CodespaceName string
// Params is the params argument value.
Params *api.EditCodespaceParams
}
// GetCodespace holds details about calls to the GetCodespace method.
GetCodespace []struct {
// Ctx is the ctx argument value.
@ -204,6 +219,7 @@ type apiClientMock struct {
lockAuthorizedKeys sync.RWMutex
lockCreateCodespace sync.RWMutex
lockDeleteCodespace sync.RWMutex
lockEditCodespace sync.RWMutex
lockGetCodespace sync.RWMutex
lockGetCodespaceRegionLocation sync.RWMutex
lockGetCodespaceRepoSuggestions sync.RWMutex
@ -321,6 +337,45 @@ func (mock *apiClientMock) DeleteCodespaceCalls() []struct {
return calls
}
// EditCodespace calls EditCodespaceFunc.
func (mock *apiClientMock) EditCodespace(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error) {
if mock.EditCodespaceFunc == nil {
panic("apiClientMock.EditCodespaceFunc: method is nil but apiClient.EditCodespace was just called")
}
callInfo := struct {
Ctx context.Context
CodespaceName string
Params *api.EditCodespaceParams
}{
Ctx: ctx,
CodespaceName: codespaceName,
Params: params,
}
mock.lockEditCodespace.Lock()
mock.calls.EditCodespace = append(mock.calls.EditCodespace, callInfo)
mock.lockEditCodespace.Unlock()
return mock.EditCodespaceFunc(ctx, codespaceName, params)
}
// EditCodespaceCalls gets all the calls that were made to EditCodespace.
// Check the length with:
// len(mockedapiClient.EditCodespaceCalls())
func (mock *apiClientMock) EditCodespaceCalls() []struct {
Ctx context.Context
CodespaceName string
Params *api.EditCodespaceParams
} {
var calls []struct {
Ctx context.Context
CodespaceName string
Params *api.EditCodespaceParams
}
mock.lockEditCodespace.RLock()
calls = mock.calls.EditCodespace
mock.lockEditCodespace.RUnlock()
return calls
}
// GetCodespace calls GetCodespaceFunc.
func (mock *apiClientMock) GetCodespace(ctx context.Context, name string, includeConnection bool) (*api.Codespace, error) {
if mock.GetCodespaceFunc == nil {

View file

@ -12,6 +12,7 @@ func NewRootCmd(app *App) *cobra.Command {
root.AddCommand(newCodeCmd(app))
root.AddCommand(newCreateCmd(app))
root.AddCommand(newEditCmd(app))
root.AddCommand(newDeleteCmd(app))
root.AddCommand(newListCmd(app))
root.AddCommand(newLogsCmd(app))