Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed defect with multiline tfvars not being escaped #631

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/variable/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ func WriteTerraformVars(dir string, vars []*Variable) error {
b.WriteString(" = ")
if v.HCL {
b.WriteString(v.Value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are using EOT as the base delimiter for the heredoc, and optionally adding extra Ts to it if the delimiter is already contained by the value.

} else if strings.Contains(v.Value, "\n") {
delimiter := "EOT"
for strings.Contains(v.Value, delimiter) {
delimiter = delimiter + "T"
}

b.WriteString("<<" + delimiter + "\n")
b.WriteString(v.Value)
b.WriteString("\n" + delimiter)
} else {
b.WriteString(`"`)
b.WriteString(v.Value)
Expand Down
27 changes: 26 additions & 1 deletion internal/variable/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestWriteTerraformVariables(t *testing.T) {
Category: VariableCategoryPtr(CategoryTerraform),
})
require.NoError(t, err)

v2, err := newVariable(nil, CreateVariableOptions{
Key: internal.String("images"),
Value: internal.String(`{
Expand All @@ -138,7 +139,21 @@ func TestWriteTerraformVariables(t *testing.T) {
})
require.NoError(t, err)

err = WriteTerraformVars(dir, []*Variable{v1, v2})
v3, err := newVariable(nil, CreateVariableOptions{
Key: internal.String("multiline-foo"),
Value: internal.String("foo\nbar\nbaz"),
Category: VariableCategoryPtr(CategoryTerraform),
})
require.NoError(t, err)

v4, err := newVariable(nil, CreateVariableOptions{
Key: internal.String("multiline-foo-with-delimiter"),
Value: internal.String("EOTfoo\nbar\nbaz"),
Category: VariableCategoryPtr(CategoryTerraform),
})
require.NoError(t, err)

err = WriteTerraformVars(dir, []*Variable{v1, v2, v3, v4})
require.NoError(t, err)

tfvars := path.Join(dir, "terraform.tfvars")
Expand All @@ -151,6 +166,16 @@ images = {
us-east-1 = "image-1234"
us-west-2 = "image-4567"
}
multiline-foo = <<EOT
foo
bar
baz
EOT
multiline-foo-with-delimiter = <<EOTT
EOTfoo
bar
baz
EOTT
`
assert.Equal(t, want, string(got))
}
Expand Down