Skip to content

Commit 0314274

Browse files
authored
Merge pull request #51 from stuartleeks/sl/dockerfile-file-snippet
Add support for reading dockerfileSnippet from file
2 parents 0c29907 + 4202548 commit 0314274

File tree

2 files changed

+98
-14
lines changed

2 files changed

+98
-14
lines changed

internal/pkg/devcontainers/snippet.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import (
2020
dora_parser "github.com/bradford-hamilton/dora/pkg/parser"
2121
)
2222

23+
type SubstitutionValues struct {
24+
Name string
25+
UserName string
26+
HomeFolder string
27+
}
28+
2329
type DevcontainerSnippetType string
2430

2531
const (
@@ -45,10 +51,11 @@ const (
4551
)
4652

4753
type FolderSnippetAction struct {
48-
Type FolderSnippetActionType `json:"type"`
49-
SourcePath string `json:"source"` // for mergeJSON this is snippet-relative path to JSON. for copyAndRun this is the script filename
50-
TargetPath string `json:"target"` // for mergeJSON this is project-relative path to JSON
51-
Content string `json:"content"` // for dockerfileSnippet this is the content to include
54+
Type FolderSnippetActionType `json:"type"`
55+
SourcePath string `json:"source"` // for mergeJSON this is snippet-relative path to JSON. for copyAndRun this is the script filename
56+
TargetPath string `json:"target"` // for mergeJSON this is project-relative path to JSON
57+
Content string `json:"content"` // for dockerfileSnippet this is the content to include
58+
ContentPath string `json:"contentPath"` // for dockerfileSnippet this is the path to content to include
5259
}
5360

5461
// FolderSnippet maps to the content of the snippet.json file for folder-based snippets
@@ -220,11 +227,23 @@ func addFolderSnippetToDevContainer(projectFolder string, snippet *DevcontainerS
220227
return err
221228
}
222229
case FolderSnippetActionDockerfileSnippet:
223-
if action.Content == "" {
224-
return fmt.Errorf("content must be set for %s actions", action.Type)
230+
var content string
231+
if action.Content != "" {
232+
if action.ContentPath != "" {
233+
return fmt.Errorf("can only set one of content and contentPath")
234+
}
235+
content = action.Content + "\n"
236+
} else if action.ContentPath != "" {
237+
buf, err = ioutil.ReadFile(filepath.Join(snippet.Path, action.ContentPath))
238+
if err != nil {
239+
return err
240+
}
241+
content = string(buf)
242+
} else {
243+
return fmt.Errorf("one of content and contentPath must be set for %s actions", action.Type)
225244
}
226245
dockerfileFilename := filepath.Join(projectFolder, ".devcontainer", "Dockerfile")
227-
err = insertDockerfileSnippet(projectFolder, dockerfileFilename, action.Content+"\n")
246+
err = insertDockerfileSnippet(projectFolder, dockerfileFilename, content)
228247
if err != nil {
229248
return err
230249
}
@@ -368,12 +387,6 @@ func loadJSONDocument(path string) (*dora_ast.RootNode, error) {
368387
return &baseDocument, nil
369388
}
370389

371-
type SubstitutionValues struct {
372-
Name string
373-
UserName string
374-
HomeFolder string
375-
}
376-
377390
func getSubstitutionValuesFromFile(devContainerJsonPath string) (*SubstitutionValues, error) {
378391
// This doesn't use standard `json` pkg as devcontainer.json permits comments (and the default templates include them!)
379392

internal/pkg/devcontainers/snippet_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ RUN echo hi2
472472
`, string(buf))
473473
}
474474

475-
func TestFolderAddSnippet_InsertsSnippetsInDockerfile(t *testing.T) {
475+
func TestFolderAddSnippet_InsertsTextSnippetsInDockerfile(t *testing.T) {
476476

477477
root, _ := ioutil.TempDir("", "devcontainer*")
478478
defer os.RemoveAll(root)
@@ -539,6 +539,77 @@ RUN echo hi2
539539
`, string(buf))
540540
}
541541

542+
func TestFolderAddSnippet_InsertsFileSnippetInDockerfile(t *testing.T) {
543+
544+
root, _ := ioutil.TempDir("", "devcontainer*")
545+
defer os.RemoveAll(root)
546+
547+
// set up snippet
548+
snippetFolder := filepath.Join(root, "snippets/test1")
549+
_ = os.MkdirAll(snippetFolder, 0755)
550+
snippetJSONFilename := filepath.Join(snippetFolder, "snippet.json")
551+
_ = ioutil.WriteFile(snippetJSONFilename, []byte(`{
552+
"actions": [
553+
{
554+
"type": "dockerfileSnippet",
555+
"content": "ENV FOO=BAR"
556+
},
557+
{
558+
"type": "dockerfileSnippet",
559+
"contentPath": "Dockerfile"
560+
}
561+
]
562+
}`), 0755)
563+
snippetDockerfileFilename := filepath.Join(snippetFolder, "Dockerfile")
564+
_ = ioutil.WriteFile(snippetDockerfileFilename, []byte(`# from snippet file
565+
ENV WIBBLE BIBBLE
566+
`), 0755)
567+
568+
// set up devcontainer
569+
targetFolder := filepath.Join(root, "target")
570+
devcontainerFolder := filepath.Join(targetFolder, ".devcontainer")
571+
_ = os.MkdirAll(devcontainerFolder, 0755)
572+
573+
_ = ioutil.WriteFile(filepath.Join(devcontainerFolder, "Dockerfile"), []byte(`FROM foo
574+
RUN echo hi
575+
576+
# __DEVCONTAINER_SNIPPET_INSERT__
577+
578+
RUN echo hi2
579+
`), 0755)
580+
_ = ioutil.WriteFile(filepath.Join(devcontainerFolder, "devcontainer.json"), []byte(`{
581+
"name" : "testname"
582+
}`), 0755)
583+
584+
// Add snippet
585+
snippet := DevcontainerSnippet{
586+
Name: "test",
587+
Path: snippetFolder,
588+
Type: DevcontainerSnippetTypeFolder,
589+
}
590+
err := addSnippetToDevcontainer(targetFolder, &snippet)
591+
if !assert.NoError(t, err) {
592+
return
593+
}
594+
595+
buf, err := ioutil.ReadFile(filepath.Join(devcontainerFolder, "Dockerfile"))
596+
if !assert.NoError(t, err) {
597+
return
598+
}
599+
assert.Equal(t, `FROM foo
600+
RUN echo hi
601+
602+
ENV FOO=BAR
603+
604+
# from snippet file
605+
ENV WIBBLE BIBBLE
606+
607+
# __DEVCONTAINER_SNIPPET_INSERT__
608+
609+
RUN echo hi2
610+
`, string(buf))
611+
}
612+
542613
func TestFolderAddSnippet_PerformsSubstitutionWithoutUserName(t *testing.T) {
543614

544615
root, _ := ioutil.TempDir("", "devcontainer*")

0 commit comments

Comments
 (0)