From 4fc11707df04531d6675df2fcc657dc5315fb1d7 Mon Sep 17 00:00:00 2001 From: Alberto Rojas Date: Wed, 7 Dec 2022 13:04:10 -0700 Subject: [PATCH 1/3] feat: Support body from fmt command --- terraform/parser.go | 2 +- terraform/template.go | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/terraform/parser.go b/terraform/parser.go index 938afc4..9dace47 100644 --- a/terraform/parser.go +++ b/terraform/parser.go @@ -90,7 +90,7 @@ func (p *DefaultParser) Parse(body string) ParseResult { func (p *FmtParser) Parse(body string) ParseResult { result := ParseResult{} if p.Fail.MatchString(body) { - result.Result = "There is diff in your .tf file (need to be formatted)" + result.Result = body result.ExitCode = ExitFail } return result diff --git a/terraform/template.go b/terraform/template.go index 0126acb..5387471 100644 --- a/terraform/template.go +++ b/terraform/template.go @@ -39,11 +39,10 @@ const ( DefaultFmtTemplate = ` {{ .Title }} -{{ .Message }} - -{{ .Result }} +
Details (Click me) -{{ .Body }} +
{{ .Body }}
+
` // DefaultPlanTemplate is a default template for terraform plan From 23531a2deb78ae945288399206730a8466bbd51b Mon Sep 17 00:00:00 2001 From: Alberto Rojas Date: Fri, 9 Dec 2022 06:25:09 -0700 Subject: [PATCH 2/3] Fix FmtTemplate Execute data map values --- terraform/parser.go | 2 +- terraform/template.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/terraform/parser.go b/terraform/parser.go index 9dace47..938afc4 100644 --- a/terraform/parser.go +++ b/terraform/parser.go @@ -90,7 +90,7 @@ func (p *DefaultParser) Parse(body string) ParseResult { func (p *FmtParser) Parse(body string) ParseResult { result := ParseResult{} if p.Fail.MatchString(body) { - result.Result = body + result.Result = "There is diff in your .tf file (need to be formatted)" result.ExitCode = ExitFail } return result diff --git a/terraform/template.go b/terraform/template.go index 5387471..848e6dd 100644 --- a/terraform/template.go +++ b/terraform/template.go @@ -241,8 +241,8 @@ func (t *FmtTemplate) Execute() (string, error) { data := map[string]interface{}{ "Title": t.Title, "Message": t.Message, - "Result": "", - "Body": t.Result, + "Result": t.Result, + "Body": t.Body, "Link": t.Link, } From e9192df1e9adb8ae9388505b5450bc3044f9b87d Mon Sep 17 00:00:00 2001 From: Alberto Rojas Date: Fri, 9 Dec 2022 07:06:42 -0700 Subject: [PATCH 3/3] Fix DefaultFmtTemplate and TestPlanTemplateExecute --- terraform/template.go | 9 +++++++- terraform/template_test.go | 46 ++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/terraform/template.go b/terraform/template.go index 848e6dd..408888d 100644 --- a/terraform/template.go +++ b/terraform/template.go @@ -39,10 +39,17 @@ const ( DefaultFmtTemplate = ` {{ .Title }} +{{ .Message }} + +{{if .Result}} +
{{ .Result }}
+
+{{end}} +
Details (Click me)
{{ .Body }}
-
+ ` // DefaultPlanTemplate is a default template for terraform plan diff --git a/terraform/template_test.go b/terraform/template_test.go index c947061..8cccc6c 100644 --- a/terraform/template_test.go +++ b/terraform/template_test.go @@ -66,7 +66,6 @@ b `, }, - { template: "", value: CommonTemplate{ @@ -193,7 +192,10 @@ func TestFmtTemplateExecute(t *testing.T) { +
Details (Click me) +

+
`, }, { @@ -208,7 +210,10 @@ message +
Details (Click me) +

+
`, }, { @@ -225,11 +230,16 @@ a b +
c
+
+ -c +
Details (Click me) + +
d
+
`, }, - { template: "", value: CommonTemplate{ @@ -244,8 +254,14 @@ a b +
c
+
+ + +
Details (Click me) -c +
d
+
`, }, { @@ -262,8 +278,14 @@ a b +
This is a "result".
+
+ + +
Details (Click me) -This is a "result". +
d
+
`, }, { @@ -281,8 +303,14 @@ a b +
This is a "result".
+
+ -This is a "result". +
Details (Click me) + +
d
+
`, }, { @@ -290,10 +318,10 @@ This is a "result". value: CommonTemplate{ Title: "a", Message: "b", - Result: "should be used as body", - Body: "should be empty", + Result: "c", + Body: "d", }, - resp: `a-b--should be used as body`, + resp: `a-b-c-d`, }, } for _, testCase := range testCases {