Skip to content

Commit

Permalink
Generate output without HTML escape
Browse files Browse the repository at this point in the history
  • Loading branch information
dtan4 committed Dec 26, 2019
1 parent 8fc65c3 commit 966795a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 4 deletions.
36 changes: 32 additions & 4 deletions terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraform
import (
"bytes"
htmltemplate "html/template"
texttemplate "text/template"
)

const (
Expand Down Expand Up @@ -207,7 +208,13 @@ func (t *DefaultTemplate) Execute() (resp string, err error) {
var b bytes.Buffer

if t.UseRawOutput {
// do nothing
tpl, err := texttemplate.New("default").Parse(t.Template)
if err != nil {
return resp, err
}
if err := tpl.Execute(&b, data); err != nil {
return resp, err
}
} else {
tpl, err := htmltemplate.New("default").Parse(t.Template)
if err != nil {
Expand Down Expand Up @@ -235,7 +242,14 @@ func (t *FmtTemplate) Execute() (resp string, err error) {
var b bytes.Buffer

if t.UseRawOutput {
// do nothing
tpl, err := texttemplate.New("fmt").Parse(t.Template)
if err != nil {
return resp, err
}

if err := tpl.Execute(&b, data); err != nil {
return resp, err
}
} else {
tpl, err := htmltemplate.New("fmt").Parse(t.Template)
if err != nil {
Expand Down Expand Up @@ -264,7 +278,14 @@ func (t *PlanTemplate) Execute() (resp string, err error) {
var b bytes.Buffer

if t.UseRawOutput {
// do nothing
tpl, err := texttemplate.New("plan").Parse(t.Template)
if err != nil {
return resp, err
}

if err := tpl.Execute(&b, data); err != nil {
return resp, err
}
} else {
tpl, err := htmltemplate.New("plan").Parse(t.Template)
if err != nil {
Expand Down Expand Up @@ -313,7 +334,14 @@ func (t *ApplyTemplate) Execute() (resp string, err error) {
var b bytes.Buffer

if t.UseRawOutput {
// do nothing
tpl, err := texttemplate.New("apply").Parse(t.Template)
if err != nil {
return resp, err
}

if err := tpl.Execute(&b, data); err != nil {
return resp, err
}
} else {
tpl, err := htmltemplate.New("apply").Parse(t.Template)
if err != nil {
Expand Down
107 changes: 107 additions & 0 deletions terraform/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,50 @@ b
<pre><code>This is a &#34;result&#34;.
</code></pre></details>
`,
},
{
template: "",
value: CommonTemplate{
Title: "a",
Message: "b",
Result: `This is a "result".`,
Body: "d",
UseRawOutput: true,
},
resp: `
a
b
<details><summary>Details (Click me)</summary>
<pre><code>This is a "result".
</code></pre></details>
`,
},
{
template: "",
value: CommonTemplate{
Title: "a",
Message: "b",
Result: `This is a "result".`,
Body: "d",
UseRawOutput: true,
},
resp: `
a
b
<details><summary>Details (Click me)</summary>
<pre><code>This is a "result".
</code></pre></details>
`,
},
{
Expand Down Expand Up @@ -220,6 +264,25 @@ b
This is a &#34;result&#34;.
`,
},
{
template: "",
value: CommonTemplate{
Title: "a",
Message: "b",
Result: `This is a "result".`,
Body: "d",
UseRawOutput: true,
},
resp: `
a
b
This is a "result".
`,
},
{
Expand Down Expand Up @@ -332,6 +395,28 @@ message
<pre><code>This is a &#34;body&#34;.
</code></pre></details>
`,
},
{
template: DefaultPlanTemplate,
value: CommonTemplate{
Title: "title",
Message: "message",
Result: "",
Body: `This is a "body".`,
UseRawOutput: true,
},
resp: `
title
message
<details><summary>Details (Click me)</summary>
<pre><code>This is a "body".
</code></pre></details>
`,
},
{
Expand Down Expand Up @@ -572,6 +657,28 @@ message
<pre><code>This is a &#34;body&#34;.
</code></pre></details>
`,
},
{
template: "",
value: CommonTemplate{
Title: "title",
Message: "message",
Result: "",
Body: `This is a "body".`,
UseRawOutput: true,
},
resp: `
title
message
<details><summary>Details (Click me)</summary>
<pre><code>This is a "body".
</code></pre></details>
`,
},
{
Expand Down

0 comments on commit 966795a

Please sign in to comment.