From 799adcfb2b98aadfbdc34bcae8e132372b787158 Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Fri, 9 Apr 2021 15:56:34 +0200 Subject: [PATCH] feat: add support for custom working directory --- README.md | 3 ++- pkg/terraform/build.go | 11 +++++++-- pkg/terraform/build_test.go | 24 ++++++++++++++++--- .../build-input-with-working-directory.yaml | 5 ++++ 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 pkg/terraform/testdata/build-input-with-working-directory.yaml diff --git a/README.md b/README.md index 4937a55..f76dd78 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,12 @@ Then, to install the resulting mixin into PORTER_HOME, execute ## Mixin Configuration -The Terraform client version can be specified via the `clientVersion` configuration when declaring this mixin. +When declaring this mixin, the Terraform client version and the working directory can be specified using the `clientVersion` and `workingDirectory` configuration properties: ```yaml - terraform: clientVersion: 0.12.17 + workingDirectory: /cnab/app/terraform ``` ## Terraform state diff --git a/pkg/terraform/build.go b/pkg/terraform/build.go index ba7c3f9..eb7bca3 100644 --- a/pkg/terraform/build.go +++ b/pkg/terraform/build.go @@ -8,11 +8,12 @@ import ( ) const dockerfileLines = `ENV TERRAFORM_VERSION=%s +ENV TERRAFORM_WORKING_DIRECTORY=%s RUN apt-get update && apt-get install -y wget unzip && \ wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip && \ unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/bin COPY . $BUNDLE_DIR -RUN cd %s && terraform init -backend=false +RUN cd ${TERRAFORM_WORKING_DIRECTORY} && terraform init -backend=false ` // BuildInput represents stdin passed to the mixin for the build command. @@ -24,8 +25,10 @@ type BuildInput struct { // mixins: // - terraform: // version: 0.12.17 +// workingDir: terraform type MixinConfig struct { - ClientVersion string `yaml:"clientVersion,omitempty"` + ClientVersion string `yaml:"clientVersion,omitempty"` + WorkingDirectory string `yaml:"workingDir,omitempty"` } func (m *Mixin) Build() error { @@ -42,6 +45,10 @@ func (m *Mixin) Build() error { m.TerraformVersion = input.Config.ClientVersion } + if input.Config.WorkingDirectory != "" { + m.WorkingDir = input.Config.WorkingDirectory + } + fmt.Fprintf(m.Out, dockerfileLines, m.TerraformVersion, m.WorkingDir) return nil } diff --git a/pkg/terraform/build_test.go b/pkg/terraform/build_test.go index 6c8aece..78d20a8 100644 --- a/pkg/terraform/build_test.go +++ b/pkg/terraform/build_test.go @@ -11,13 +11,16 @@ import ( ) const buildOutputTemplate = `ENV TERRAFORM_VERSION=%s +ENV TERRAFORM_WORKING_DIRECTORY=%s RUN apt-get update && apt-get install -y wget unzip && \ wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip && \ unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/bin COPY . $BUNDLE_DIR -RUN cd /cnab/app/terraform && terraform init -backend=false +RUN cd ${TERRAFORM_WORKING_DIRECTORY} && terraform init -backend=false ` +const defaultWorkingDirectory = "/cnab/app/terraform" + func TestMixin_Build(t *testing.T) { t.Run("build with the default Terraform version", func(t *testing.T) { m := NewTestMixin(t) @@ -25,12 +28,27 @@ func TestMixin_Build(t *testing.T) { err := m.Build() require.NoError(t, err) - wantOutput := fmt.Sprintf(buildOutputTemplate, "0.12.17") + wantOutput := fmt.Sprintf(buildOutputTemplate, "0.12.17", defaultWorkingDirectory) gotOutput := m.TestContext.GetOutput() assert.Equal(t, wantOutput, gotOutput) }) + t.Run("build with custom working directory", func(t *testing.T) { + b, err := ioutil.ReadFile("testdata/build-input-with-working-directory.yaml") + require.NoError(t, err) + + m := NewTestMixin(t) + m.In = bytes.NewReader(b) + err = m.Build() + require.NoError(t, err) + + expected := fmt.Sprintf(buildOutputTemplate, "0.12.17", "/cnab/app/custom") + actual := m.TestContext.GetOutput() + + assert.Equal(t, expected, actual) + }) + t.Run("build with custom Terrafrom version", func(t *testing.T) { b, err := ioutil.ReadFile("testdata/build-input-with-version.yaml") require.NoError(t, err) @@ -40,7 +58,7 @@ func TestMixin_Build(t *testing.T) { err = m.Build() require.NoError(t, err) - wantOutput := fmt.Sprintf(buildOutputTemplate, "0.13.0-rc1") + wantOutput := fmt.Sprintf(buildOutputTemplate, "0.13.0-rc1", defaultWorkingDirectory) gotOutput := m.TestContext.GetOutput() assert.Equal(t, wantOutput, gotOutput) diff --git a/pkg/terraform/testdata/build-input-with-working-directory.yaml b/pkg/terraform/testdata/build-input-with-working-directory.yaml new file mode 100644 index 0000000..41df8b1 --- /dev/null +++ b/pkg/terraform/testdata/build-input-with-working-directory.yaml @@ -0,0 +1,5 @@ +config: + workingDirectory: /cnab/app/custom + install: + - terraform: + description: "noop"