Skip to content

Commit

Permalink
Refeactor template logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dtan4 committed Dec 26, 2019
1 parent 966795a commit 32abc03
Showing 1 changed file with 40 additions and 86 deletions.
126 changes: 40 additions & 86 deletions terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 32abc03

Please sign in to comment.