From e31f805a9c91efe8b1974a64cfcabc7f9d71cc4b Mon Sep 17 00:00:00 2001 From: Doug Hatcher Date: Fri, 29 Oct 2021 21:51:10 -0400 Subject: [PATCH] adds API_VERSION --- .vscode/launch.json | 4 ++-- cmd/build.go | 13 ++++++++++++- cmd/build_test.go | 8 ++++---- examples/images/example/API_VERSION | 1 + 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 examples/images/example/API_VERSION diff --git a/.vscode/launch.json b/.vscode/launch.json index 6ccab8b..3309d96 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,8 +9,8 @@ "type": "go", "request": "launch", "mode": "auto", - "program": "${workspaceFolder}/main.go", - "args": ["compose", "template", "up"] + "program": "${workspaceFolder}/cmd/build_test.go", + // "args": ["build"] } ] } \ No newline at end of file diff --git a/cmd/build.go b/cmd/build.go index ac9338d..dfc20ae 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -9,6 +9,7 @@ import ( "fmt" "html/template" "io" + "io/ioutil" "log" "os" "path/filepath" @@ -228,12 +229,22 @@ func generateDockerfileTemplate(wr io.Writer, filename string) { } +func getApiVersion(filename string) string { + + content, err := ioutil.ReadFile(filepath.Dir(filename) + "/API_VERSION") + if err != nil { + return "" + } else { + return string(content) + "-" + } +} + // getTag determines the string used for the docker image tag that gets referenced in the registry. // This is typically just the directory name the dockerfile resides in, but can be modified by either // using a variant in the filename (detailed below) or by using a git branch other than the default func getTag(filename string) string { - var tag string = filepath.Base(filepath.Dir(filename)) + getVariant(filename) + var tag string = getApiVersion(filename) + filepath.Base(filepath.Dir(filename)) + getVariant(filename) if DockerRegistry != "" { return DockerRegistry + ":" + tag diff --git a/cmd/build_test.go b/cmd/build_test.go index fb86ae2..70914c9 100644 --- a/cmd/build_test.go +++ b/cmd/build_test.go @@ -184,7 +184,7 @@ func Test_buildCmdOneArgWithImagesOutputOnlyWithTagReal(t *testing.T) { var actual = buildImage("../examples/images/example/Dockerfile") - if actual != "superterran/mach:example" { + if actual != "superterran/mach:v1-example" { assert.FailNowf(t, "mach tag returned as expected, %s", actual) } } @@ -196,7 +196,7 @@ func Test_buildCmdOneArgWithImagesOutputOnlyWithTagRealTemplate(t *testing.T) { var actual = buildImage("../examples/images/example/Dockerfile-template.tpl") - if actual != "superterran/mach:example-template" { + if actual != "superterran/mach:v1-example-template" { assert.FailNowf(t, "mach tag returned as expected, %s", actual) } } @@ -208,7 +208,7 @@ func Test_buildCmdOneArgWithImagesOutputOnlyWithTagRealOutputOnly(t *testing.T) var actual = buildImage("../examples/images/example/Dockerfile") - if actual != "superterran/mach:example" { + if actual != "superterran/mach:v1-example" { assert.FailNowf(t, "mach tag returned as expected, %s", actual) } } @@ -220,7 +220,7 @@ func Test_buildCmdOneArgWithImagesOutputOnlyWithTagRealTemplateOutputOnly(t *tes var actual = buildImage("../examples/images/example/Dockerfile-template.tpl") - if actual != "superterran/mach:example-template" { + if actual != "superterran/mach:v1-example-template" { assert.FailNowf(t, "mach tag returned as expected, %s", actual) } } diff --git a/examples/images/example/API_VERSION b/examples/images/example/API_VERSION new file mode 100644 index 0000000..28c218c --- /dev/null +++ b/examples/images/example/API_VERSION @@ -0,0 +1 @@ +v1 \ No newline at end of file