X Tutup
Skip to content

Commit 9554e52

Browse files
committed
Change the way we parse list-devcontainers response
1 parent 03b8b16 commit 9554e52

File tree

7 files changed

+51
-28
lines changed

7 files changed

+51
-28
lines changed

internal/codespaces/api/api.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,14 @@ func (a *API) DeleteCodespace(ctx context.Context, codespaceName string) error {
762762
return nil
763763
}
764764

765+
type DevContainerEntry struct {
766+
Path string `json:"path"`
767+
Name string `json:"name,omitempty"`
768+
}
769+
765770
// ListDevContainers returns a list of valid devcontainer.json files for the repo. Pass a negative limit to request all pages from
766771
// the API until all devcontainer.json files have been fetched.
767-
func (a *API) ListDevContainers(ctx context.Context, repoID int, branch string, limit int) (devcontainers []string, err error) {
772+
func (a *API) ListDevContainers(ctx context.Context, repoID int, branch string, limit int) (devcontainers []DevContainerEntry, err error) {
768773
perPage := 100
769774
if limit > 0 && limit < 100 {
770775
perPage = limit
@@ -792,8 +797,9 @@ func (a *API) ListDevContainers(ctx context.Context, repoID int, branch string,
792797
}
793798

794799
var response struct {
795-
Devcontainers []string `json:"devcontainers"`
800+
Devcontainers []DevContainerEntry `json:"devcontainers"`
796801
}
802+
797803
dec := json.NewDecoder(resp.Body)
798804
if err := dec.Decode(&response); err != nil {
799805
return nil, fmt.Errorf("error unmarshaling response: %w", err)

pkg/cmd/codespace/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type apiClient interface {
7474
GetCodespaceRegionLocation(ctx context.Context) (string, error)
7575
GetCodespacesMachines(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error)
7676
GetCodespaceRepositoryContents(ctx context.Context, codespace *api.Codespace, path string) ([]byte, error)
77-
ListDevContainers(ctx context.Context, repoID int, branch string, limit int) (devcontainers []string, err error)
77+
ListDevContainers(ctx context.Context, repoID int, branch string, limit int) (devcontainers []api.DevContainerEntry, err error)
7878
GetCodespaceRepoSuggestions(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error)
7979
}
8080

pkg/cmd/codespace/create.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
5858

5959
locationCh := getLocation(ctx, vscsLocation, a.apiClient)
6060

61+
DEFAULT_DEVCONTAINER_DEFINITIONS := []string{".devcontainer.json", ".devcontainer/devcontainer.json"}
62+
6163
userInputs := struct {
6264
Repository string
6365
Branch string
@@ -113,23 +115,35 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
113115
// now that we have repo+branch, we can list available devcontainer.json files (if any)
114116
if len(opts.devContainerPath) < 1 {
115117
a.StartProgressIndicatorWithLabel("Fetching devcontainer.json files")
116-
devContainerPaths, err := a.apiClient.ListDevContainers(ctx, repository.ID, branch, 100)
118+
devcontainers, err := a.apiClient.ListDevContainers(ctx, repository.ID, branch, 100)
117119
if err != nil {
118120
return fmt.Errorf("error getting devcontainer.json paths: %w", err)
119121
}
120122
a.StopProgressIndicator()
121123

122-
if len(devContainerPaths) > 0 {
123-
devContainerPathQuestion := &survey.Question{
124-
Name: "devContainerPath",
125-
Prompt: &survey.Select{
126-
Message: "Devcontainer definition file:",
127-
Options: append([]string{"default"}, devContainerPaths...),
128-
},
129-
}
124+
if len(devcontainers) > 0 {
130125

131-
if err := ask([]*survey.Question{devContainerPathQuestion}, &devContainerPath); err != nil {
132-
return fmt.Errorf("failed to prompt: %w", err)
126+
// if there is only one devcontainer.json file and it is one of the default paths we can auto-select it
127+
if len(devcontainers) == 1 && utils.StringInSlice(devcontainers[0].Path, DEFAULT_DEVCONTAINER_DEFINITIONS) {
128+
devContainerPath = devcontainers[0].Path
129+
} else {
130+
promptOptions := []string{"default"}
131+
132+
for _, devcontainer := range devcontainers {
133+
promptOptions = append(promptOptions, devcontainer.Path)
134+
}
135+
136+
devContainerPathQuestion := &survey.Question{
137+
Name: "devContainerPath",
138+
Prompt: &survey.Select{
139+
Message: "Devcontainer definition file:",
140+
Options: promptOptions,
141+
},
142+
}
143+
144+
if err := ask([]*survey.Question{devContainerPathQuestion}, &devContainerPath); err != nil {
145+
return fmt.Errorf("failed to prompt: %w", err)
146+
}
133147
}
134148
}
135149

pkg/cmd/codespace/create_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func TestApp_Create(t *testing.T) {
8686
DefaultBranch: "main",
8787
}, nil
8888
},
89-
ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]string, error) {
90-
return []string{}, nil
89+
ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) {
90+
return []api.DevContainerEntry{}, nil
9191
},
9292
GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error) {
9393
return []*api.Machine{
@@ -139,7 +139,7 @@ func TestApp_Create(t *testing.T) {
139139
DefaultBranch: "main",
140140
}, nil
141141
},
142-
ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]string, error) {
142+
ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) {
143143
return nil, fmt.Errorf("some error")
144144
},
145145
},
@@ -167,8 +167,8 @@ func TestApp_Create(t *testing.T) {
167167
DefaultBranch: "main",
168168
}, nil
169169
},
170-
ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]string, error) {
171-
return []string{}, nil
170+
ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) {
171+
return []api.DevContainerEntry{}, nil
172172
},
173173
GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error) {
174174
return []*api.Machine{

pkg/cmd/codespace/mock_api.go

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

testdevcontainer

Lines changed: 0 additions & 6 deletions
This file was deleted.

utils/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,12 @@ func DisplayURL(urlStr string) string {
8383
func ValidURL(urlStr string) bool {
8484
return len(urlStr) < 8192
8585
}
86+
87+
func StringInSlice(a string, slice []string) bool {
88+
for _, b := range slice {
89+
if b == a {
90+
return true
91+
}
92+
}
93+
return false
94+
}

0 commit comments

Comments
 (0)
X Tutup