Skip to content

Commit 715cbc3

Browse files
authored
Merge pull request #77 from stuartleeks/stuart/non-folder-dev-container
Support .devcontainer.json
2 parents 774487c + 3a65db8 commit 715cbc3

File tree

8 files changed

+40
-5
lines changed

8 files changed

+40
-5
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
44
#-------------------------------------------------------------------------------------------------------------
55

6-
FROM golang:1.17-stretch
6+
FROM golang:1.17-bullseye
77

88
# Avoid warnings by switching to noninteractive
99
ENV DEBIAN_FRONTEND=noninteractive

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ require (
66
github.com/blang/semver v3.5.1+incompatible
77
github.com/bradford-hamilton/dora v0.1.1
88
github.com/kyoh86/richgo v0.3.12 // indirect
9-
github.com/mattn/go-isatty v0.0.18 // indirect
9+
github.com/mattn/go-isatty v0.0.19 // indirect
1010
github.com/rhysd/go-github-selfupdate v1.2.2
1111
github.com/spf13/cobra v1.0.0
1212
github.com/spf13/viper v1.4.0
1313
github.com/stretchr/testify v1.8.2
14-
golang.org/x/sys v0.8.0 // indirect
14+
golang.org/x/sys v0.10.0 // indirect
1515
gopkg.in/yaml.v2 v2.4.0 // indirect
1616
)
1717

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPn
8282
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8383
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
8484
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
85+
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
86+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
8587
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
8688
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
8789
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
@@ -194,6 +196,8 @@ golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
194196
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
195197
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
196198
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
199+
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
200+
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
197201
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
198202
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
199203
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package devcontainers
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
func getDevContainerJsonPath(folderPath string) (string, error) {
11+
pathsToTest := []string{".devcontainer/devcontainer.json", ".devcontainer.json"}
12+
13+
for _, path := range pathsToTest {
14+
devcontainerJsonPath := filepath.Join(folderPath, path)
15+
devContainerJsonInfo, err := os.Stat(devcontainerJsonPath)
16+
if err == nil && !devContainerJsonInfo.IsDir() {
17+
return devcontainerJsonPath, nil
18+
}
19+
}
20+
21+
return "", fmt.Errorf("devcontainer.json not found. Looked for %s", strings.Join(pathsToTest, ","))
22+
}

internal/pkg/devcontainers/dockerutils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ func ExecInDevContainer(containerID string, workDir string, args []string) error
234234
localPath := sourceInfo.DevcontainerFolder
235235

236236
statusWriter.Printf("Getting user name")
237-
devcontainerJSONPath := path.Join(localPath, ".devcontainer/devcontainer.json")
237+
devcontainerJSONPath, err := getDevContainerJsonPath(localPath)
238+
if err != nil {
239+
return err
240+
}
238241
userName, err := GetDevContainerUserName(devcontainerJSONPath)
239242
if err != nil {
240243
return err

internal/pkg/devcontainers/remoteuri.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ func GetWorkspaceMountPath(folderPath string) (string, error) {
5757
}
5858
}
5959

60-
devcontainerDefinitionPath := filepath.Join(folderPath, ".devcontainer/devcontainer.json")
60+
devcontainerDefinitionPath, err := getDevContainerJsonPath(folderPath)
61+
if err != nil {
62+
return "", fmt.Errorf("Error getting devcontainer definition path: %s", err)
63+
}
6164
buf, err := ioutil.ReadFile(devcontainerDefinitionPath)
6265
if err != nil {
6366
return "", fmt.Errorf("Error loading devcontainer definition: %s", err)

internal/pkg/devcontainers/snippet.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func insertDockerfileSnippet(projectFolder string, dockerfileFilename string, sn
320320
}
321321

322322
content := newContent.String()
323+
// TODO - decide whether to support .devcontainer.json or just remove snippet support
323324
values, err := getSubstitutionValuesFromFile(filepath.Join(projectFolder, ".devcontainer/devcontainer.json"))
324325
if err != nil {
325326
return fmt.Errorf("failed to get dev container values: %s", err)
@@ -358,6 +359,7 @@ func mergeJSON(projectFolder string, snippet *DevcontainerSnippet, relativeMerge
358359
return err
359360
}
360361

362+
// TODO - decide whether to support .devcontainer.json or just remove snippet support
361363
values, err := getSubstitutionValuesFromFile(filepath.Join(projectFolder, ".devcontainer/devcontainer.json"))
362364
if err != nil {
363365
return fmt.Errorf("failed to get dev container values: %s", err)

internal/pkg/devcontainers/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func getTemplatesFromFolder(folder string) ([]DevcontainerTemplate, error) {
7676
if !fi.IsDir() {
7777
return false
7878
}
79+
// TOODO - add support for templates with .devcontainer.json rather than .devcontainer folder
7980
devcontainerJsonPath := filepath.Join(parentPath, fi.Name(), ".devcontainer/devcontainer.json")
8081
devContainerJsonInfo, err := os.Stat(devcontainerJsonPath)
8182
return err == nil && !devContainerJsonInfo.IsDir()

0 commit comments

Comments
 (0)