Skip to content

Commit

Permalink
feat: add support for custom working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorstenHans committed Apr 9, 2021
1 parent 97c0adc commit 799adcf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions pkg/terraform/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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
}
24 changes: 21 additions & 3 deletions pkg/terraform/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,44 @@ 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)

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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config:
workingDirectory: /cnab/app/custom
install:
- terraform:
description: "noop"

0 comments on commit 799adcf

Please sign in to comment.