gruntwork-io / boilerplate

A tool for generating files and folders ("boilerplate") from a set of templates
https://www.gruntwork.io
Mozilla Public License 2.0
157 stars 12 forks source link

Cannot produce quoted JSON #168

Closed yngvark closed 3 months ago

yngvark commented 3 months ago

Describe the bug

The template

{
  "foo": "{{ .Foo }}"
}

renders as an exact copy, without "Foo" being replaced by any input variable.

To Reproduce

cd $(mktemp -d)
mkdir -p bug/template
cd bug/template

cat <<EOF >boilerplate.yml
variables:
  - name: Foo
    type: string
EOF

cat <<EOF >example1.json
{
  "foo": "{{ .Foo }}"
}
EOF

cat <<EOF >example2.json
{
  "foo: "{{ .Foo }}"
}
EOF

boilerplate --template-url . --output-folder ../output --non-interactive --var Foo=Hello

cat ../output/example1.json
cat ../output/example2.json

Expected behavior

I expect the file ../output/example1.json to contain

{
  "foo": "Hello"
}

but it contains:

{
  "foo": "{{ .Foo }}"
}

The template in example1.json works fine here: https://gotemplate.io.

Nice to have

image

Additional context

boilerplate version v0.5.11

brikis98 commented 3 months ago

Huh, very weird. I think there's something happening where the { and } at the beginning/ending of the file are causing issues. It seems like it should work, so I'm not sure why that's the case, but while we investigate, the workaround is to "escape" those curly braces:

{{"{"}}
  "foo" : "{{ .Foo }}"
{{"}"}}
brikis98 commented 3 months ago

I can also confirm that with totally standalone Go template, this rendering should work:

package main

import (
    "os"
    "text/template"
)

const json = `{
  "foo": "{{ .Foo }}"
}`

func main() {
    tmpl, err := template.New("test").Parse(json)
    if err != nil {
        panic(err)
    }
    err = tmpl.Execute(os.Stdout, map[string]string{"Foo": "bar"})
    if err != nil {
        panic(err)
    }
}

The output from running this is:

{
  "foo": "bar"
}
brikis98 commented 3 months ago

@denis256 When you have a chance, could you take a look?

denis256 commented 3 months ago

Will take a look soon

denis256 commented 3 months ago

Fix included in https://github.com/gruntwork-io/boilerplate/releases/tag/v0.5.12