hcl/printer: multiline strings don't indent lines 2+

Fixes https://github.com/hashicorp/terraform/issues/9859

For strings that have more than one line, we should not indent the
follow-up lines.
This commit is contained in:
Mitchell Hashimoto 2016-11-03 18:40:17 -07:00
parent 6e968a3fcd
commit 9235fed8ea
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 23 additions and 1 deletions

View File

@ -195,7 +195,8 @@ func (p *printer) output(n interface{}) []byte {
func (p *printer) literalType(lit *ast.LiteralType) []byte {
result := []byte(lit.Token.Text)
if lit.Token.Type == token.HEREDOC {
switch lit.Token.Type {
case token.HEREDOC:
// Clear the trailing newline from heredocs
if result[len(result)-1] == '\n' {
result = result[:len(result)-1]
@ -203,6 +204,12 @@ func (p *printer) literalType(lit *ast.LiteralType) []byte {
// Poison lines 2+ so that we don't indent them
result = p.heredocIndent(result)
case token.STRING:
// If this is a multiline string, poison lines 2+ so we don't
// indent them.
if bytes.ContainsRune(result, '\n') {
result = p.heredocIndent(result)
}
}
return result

View File

@ -40,6 +40,7 @@ var data = []entry{
{"comment_standalone.input", "comment_standalone.golden"},
{"empty_block.input", "empty_block.golden"},
{"list_of_objects.input", "list_of_objects.golden"},
{"multiline_string.input", "multiline_string.golden"},
}
func TestFiles(t *testing.T) {

View File

@ -0,0 +1,7 @@
resource "null_resource" "some_command" {
provisioner "local-exec" {
command = "echo '
some newlines
and additonal output'"
}
}

View File

@ -0,0 +1,7 @@
resource "null_resource" "some_command" {
provisioner "local-exec" {
command = "echo '
some newlines
and additonal output'"
}
}