Skip to content

Commit

Permalink
Finish?
Browse files Browse the repository at this point in the history
  • Loading branch information
mtojek committed Apr 22, 2024
1 parent 34c4a00 commit 6cde3b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
45 changes: 35 additions & 10 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,11 @@ func Run(ctx context.Context, options Options) error {
if options.DockerfilePath == "" {
// Only look for a devcontainer if a Dockerfile wasn't specified.
// devcontainer is a standard, so it's reasonable to be the default.
devcontainerPath, devcontainerDir := findDevcontainerJSON(options)
_, err := options.Filesystem.Stat(devcontainerPath)
if err == nil {
devcontainerPath, devcontainerDir, err := findDevcontainerJSON(options)
if err != nil {
logf(codersdk.LogLevelError, "Failed to locate devcontainer.json: %s", err.Error())
logf(codersdk.LogLevelError, "Falling back to the default image...")
} else {
// We know a devcontainer exists.
// Let's parse it and use it!
file, err := options.Filesystem.Open(devcontainerPath)
Expand Down Expand Up @@ -1194,7 +1196,7 @@ func (fs *osfsWithChmod) Chmod(name string, mode os.FileMode) error {
return os.Chmod(name, mode)
}

func findDevcontainerJSON(options Options) (string, string) {
func findDevcontainerJSON(options Options) (string, string, error) {
// 0. Check provided options.
if options.DevcontainerDir != "" || options.DevcontainerJSONPath != "" {
// Locate .devcontainer directory.
Expand All @@ -1212,7 +1214,7 @@ func findDevcontainerJSON(options Options) (string, string) {

// An absolute location always takes a precedence.
if filepath.IsAbs(devcontainerPath) {
return options.DevcontainerJSONPath, devcontainerDir
return options.DevcontainerJSONPath, devcontainerDir, nil
}

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

// 1. Check `options.WorkspaceFolder`/.devcontainer/devcontainer.json.
location := filepath.Join(options.WorkspaceFolder, ".devcontainer", "devcontainer.json")
_, err := options.Filesystem.Stat(location)
if err == nil {
return location, filepath.Dir(location)
return location, filepath.Dir(location), nil
}

// 2. Check `options.WorkspaceFolder`/devcontainer.json.
location = filepath.Join(options.WorkspaceFolder, "devcontainer.json")
_, err = options.Filesystem.Stat(location)
if err == nil {
return location, filepath.Dir(location)
return location, filepath.Dir(location), nil
}

// 3. Check every folder: `options.WorkspaceFolder`/<folder>/devcontainer.json.
// 3. Check every folder: `options.WorkspaceFolder`/.devcontainer/<folder>/devcontainer.json.
devcontainerDir := filepath.Join(options.WorkspaceFolder, ".devcontainer")

fileInfos, err := options.Filesystem.ReadDir(devcontainerDir)
if err != nil {
return "", "", err
}

logf := options.Logger
for _, fileInfo := range fileInfos {
if !fileInfo.IsDir() {
logf(codersdk.LogLevelDebug, `%s is a file`, fileInfo.Name())
continue
}

location := filepath.Join(devcontainerDir, fileInfo.Name(), "devcontainer.json")
_, err := options.Filesystem.Stat(location)
if err != nil {
logf(codersdk.LogLevelDebug, `stat %s failed: %s`, location, err.Error())
continue
}

return location, filepath.Dir(location), nil
}

panic("not implemented yet")
return "", "", errors.New("can't find devcontainer.json, is it a correct spec?")
}
4 changes: 2 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,13 @@ func TestBuildFromDevcontainerInSubfolder(t *testing.T) {
// Ensures that a Git repository with a devcontainer.json is cloned and built.
url := createGitServer(t, gitServerOptions{
files: map[string]string{
"./devcontainer/subfolder/devcontainer.json": `{
".devcontainer/subfolder/devcontainer.json": `{
"name": "Test",
"build": {
"dockerfile": "Dockerfile"
},
}`,
"Dockerfile": "FROM ubuntu",
".devcontainer/subfolder/Dockerfile": "FROM ubuntu",
},
})
ctr, err := runEnvbuilder(t, options{env: []string{
Expand Down

0 comments on commit 6cde3b2

Please sign in to comment.