From 32abc03cc35cbc6085d9d965cb5991af02c90cda Mon Sep 17 00:00:00 2001 From: Daisuke Fujita Date: Thu, 26 Dec 2019 12:25:37 +0900 Subject: [PATCH] Refeactor template logic --- terraform/template.go | 126 ++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 86 deletions(-) diff --git a/terraform/template.go b/terraform/template.go index b542f50..9eaba92 100644 --- a/terraform/template.go +++ b/terraform/template.go @@ -195,42 +195,32 @@ func NewApplyTemplate(template string) *ApplyTemplate { } } -// Execute binds the execution result of terraform command into tepmlate -func (t *DefaultTemplate) Execute() (resp string, err error) { - data := map[string]interface{}{ - "Title": t.Title, - "Message": t.Message, - "Result": "", - "Body": t.Result, - "Link": t.Link, - } - +func generateOutput(kind, template string, data map[string]interface{}, useRawOutput bool) (string, error) { var b bytes.Buffer - if t.UseRawOutput { - tpl, err := texttemplate.New("default").Parse(t.Template) + if useRawOutput { + tpl, err := texttemplate.New(kind).Parse(template) if err != nil { - return resp, err + return "", err } if err := tpl.Execute(&b, data); err != nil { - return resp, err + return "", err } } else { - tpl, err := htmltemplate.New("default").Parse(t.Template) + tpl, err := htmltemplate.New(kind).Parse(template) if err != nil { - return resp, err + return "", err } if err := tpl.Execute(&b, data); err != nil { - return resp, err + return "", err } } - resp = b.String() - return resp, err + return b.String(), nil } -// Execute binds the execution result of terraform fmt into tepmlate -func (t *FmtTemplate) Execute() (resp string, err error) { +// Execute binds the execution result of terraform command into tepmlate +func (t *DefaultTemplate) Execute() (string, error) { data := map[string]interface{}{ "Title": t.Title, "Message": t.Message, @@ -239,34 +229,34 @@ func (t *FmtTemplate) Execute() (resp string, err error) { "Link": t.Link, } - var b bytes.Buffer + resp, err := generateOutput("default", t.Template, data, t.UseRawOutput) + if err != nil { + return "", err + } - if t.UseRawOutput { - tpl, err := texttemplate.New("fmt").Parse(t.Template) - if err != nil { - return resp, err - } + return resp, nil +} - if err := tpl.Execute(&b, data); err != nil { - return resp, err - } - } else { - tpl, err := htmltemplate.New("fmt").Parse(t.Template) - if err != nil { - return resp, err - } +// Execute binds the execution result of terraform fmt into tepmlate +func (t *FmtTemplate) Execute() (string, error) { + data := map[string]interface{}{ + "Title": t.Title, + "Message": t.Message, + "Result": "", + "Body": t.Result, + "Link": t.Link, + } - if err := tpl.Execute(&b, data); err != nil { - return resp, err - } + resp, err := generateOutput("fmt", t.Template, data, t.UseRawOutput) + if err != nil { + return "", err } - resp = b.String() - return resp, err + return resp, nil } // Execute binds the execution result of terraform plan into tepmlate -func (t *PlanTemplate) Execute() (resp string, err error) { +func (t *PlanTemplate) Execute() (string, error) { data := map[string]interface{}{ "Title": t.Title, "Message": t.Message, @@ -275,30 +265,12 @@ func (t *PlanTemplate) Execute() (resp string, err error) { "Link": t.Link, } - var b bytes.Buffer - - if t.UseRawOutput { - 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 { - return resp, err - } - - if err := tpl.Execute(&b, data); err != nil { - return resp, err - } + resp, err := generateOutput("plan", t.Template, data, t.UseRawOutput) + if err != nil { + return "", err } - resp = b.String() - return resp, err + return resp, nil } // Execute binds the execution result of terraform plan into template @@ -322,7 +294,7 @@ func (t *DestroyWarningTemplate) Execute() (resp string, err error) { } // Execute binds the execution result of terraform apply into tepmlate -func (t *ApplyTemplate) Execute() (resp string, err error) { +func (t *ApplyTemplate) Execute() (string, error) { data := map[string]interface{}{ "Title": t.Title, "Message": t.Message, @@ -331,30 +303,12 @@ func (t *ApplyTemplate) Execute() (resp string, err error) { "Link": t.Link, } - var b bytes.Buffer - - if t.UseRawOutput { - 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 { - return resp, err - } - - if err := tpl.Execute(&b, data); err != nil { - return resp, err - } + resp, err := generateOutput("apply", t.Template, data, t.UseRawOutput) + if err != nil { + return "", err } - resp = b.String() - return resp, err + return resp, nil } // SetValue sets template entities to CommonTemplate