Skip to content

Commit 6cde3b2

Browse files
committed
Finish?
1 parent 34c4a00 commit 6cde3b2

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

envbuilder.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,11 @@ func Run(ctx context.Context, options Options) error {
427427
if options.DockerfilePath == "" {
428428
// Only look for a devcontainer if a Dockerfile wasn't specified.
429429
// devcontainer is a standard, so it's reasonable to be the default.
430-
devcontainerPath, devcontainerDir := findDevcontainerJSON(options)
431-
_, err := options.Filesystem.Stat(devcontainerPath)
432-
if err == nil {
430+
devcontainerPath, devcontainerDir, err := findDevcontainerJSON(options)
431+
if err != nil {
432+
logf(codersdk.LogLevelError, "Failed to locate devcontainer.json: %s", err.Error())
433+
logf(codersdk.LogLevelError, "Falling back to the default image...")
434+
} else {
433435
// We know a devcontainer exists.
434436
// Let's parse it and use it!
435437
file, err := options.Filesystem.Open(devcontainerPath)
@@ -1194,7 +1196,7 @@ func (fs *osfsWithChmod) Chmod(name string, mode os.FileMode) error {
11941196
return os.Chmod(name, mode)
11951197
}
11961198

1197-
func findDevcontainerJSON(options Options) (string, string) {
1199+
func findDevcontainerJSON(options Options) (string, string, error) {
11981200
// 0. Check provided options.
11991201
if options.DevcontainerDir != "" || options.DevcontainerJSONPath != "" {
12001202
// Locate .devcontainer directory.
@@ -1212,7 +1214,7 @@ func findDevcontainerJSON(options Options) (string, string) {
12121214

12131215
// An absolute location always takes a precedence.
12141216
if filepath.IsAbs(devcontainerPath) {
1215-
return options.DevcontainerJSONPath, devcontainerDir
1217+
return options.DevcontainerJSONPath, devcontainerDir, nil
12161218
}
12171219

12181220
// If an override is not provided, assume it is just `devcontainer.json`.
@@ -1223,24 +1225,47 @@ func findDevcontainerJSON(options Options) (string, string) {
12231225
if !filepath.IsAbs(devcontainerPath) {
12241226
devcontainerPath = filepath.Join(devcontainerDir, devcontainerPath)
12251227
}
1226-
return devcontainerPath, devcontainerDir
1228+
return devcontainerPath, devcontainerDir, nil
12271229
}
12281230

12291231
// 1. Check `options.WorkspaceFolder`/.devcontainer/devcontainer.json.
12301232
location := filepath.Join(options.WorkspaceFolder, ".devcontainer", "devcontainer.json")
12311233
_, err := options.Filesystem.Stat(location)
12321234
if err == nil {
1233-
return location, filepath.Dir(location)
1235+
return location, filepath.Dir(location), nil
12341236
}
12351237

12361238
// 2. Check `options.WorkspaceFolder`/devcontainer.json.
12371239
location = filepath.Join(options.WorkspaceFolder, "devcontainer.json")
12381240
_, err = options.Filesystem.Stat(location)
12391241
if err == nil {
1240-
return location, filepath.Dir(location)
1242+
return location, filepath.Dir(location), nil
12411243
}
12421244

1243-
// 3. Check every folder: `options.WorkspaceFolder`/<folder>/devcontainer.json.
1245+
// 3. Check every folder: `options.WorkspaceFolder`/.devcontainer/<folder>/devcontainer.json.
1246+
devcontainerDir := filepath.Join(options.WorkspaceFolder, ".devcontainer")
1247+
1248+
fileInfos, err := options.Filesystem.ReadDir(devcontainerDir)
1249+
if err != nil {
1250+
return "", "", err
1251+
}
1252+
1253+
logf := options.Logger
1254+
for _, fileInfo := range fileInfos {
1255+
if !fileInfo.IsDir() {
1256+
logf(codersdk.LogLevelDebug, `%s is a file`, fileInfo.Name())
1257+
continue
1258+
}
1259+
1260+
location := filepath.Join(devcontainerDir, fileInfo.Name(), "devcontainer.json")
1261+
_, err := options.Filesystem.Stat(location)
1262+
if err != nil {
1263+
logf(codersdk.LogLevelDebug, `stat %s failed: %s`, location, err.Error())
1264+
continue
1265+
}
1266+
1267+
return location, filepath.Dir(location), nil
1268+
}
12441269

1245-
panic("not implemented yet")
1270+
return "", "", errors.New("can't find devcontainer.json, is it a correct spec?")
12461271
}

integration/integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,13 @@ func TestBuildFromDevcontainerInSubfolder(t *testing.T) {
279279
// Ensures that a Git repository with a devcontainer.json is cloned and built.
280280
url := createGitServer(t, gitServerOptions{
281281
files: map[string]string{
282-
"./devcontainer/subfolder/devcontainer.json": `{
282+
".devcontainer/subfolder/devcontainer.json": `{
283283
"name": "Test",
284284
"build": {
285285
"dockerfile": "Dockerfile"
286286
},
287287
}`,
288-
"Dockerfile": "FROM ubuntu",
288+
".devcontainer/subfolder/Dockerfile": "FROM ubuntu",
289289
},
290290
})
291291
ctr, err := runEnvbuilder(t, options{env: []string{

0 commit comments

Comments
 (0)