Skip to content

Commit

Permalink
Move templating functionality into its own package from Levant pkg
Browse files Browse the repository at this point in the history
Closes #171
  • Loading branch information
jrasell committed May 18, 2018
1 parent 32664bb commit 076fd63
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 25 deletions.
3 changes: 2 additions & 1 deletion command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/jrasell/levant/levant"
"github.com/jrasell/levant/levant/structs"
"github.com/jrasell/levant/logging"
"github.com/jrasell/levant/template"
)

// DeployCommand is the command implementation that allows users to deploy a
Expand Down Expand Up @@ -109,7 +110,7 @@ func (c *DeployCommand) Run(args []string) int {
return 1
}

config.Job, err = levant.RenderJob(config.TemplateFile, config.VaiableFile, &c.Meta.flagVars)
config.Job, err = template.RenderJob(config.TemplateFile, config.VaiableFile, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
6 changes: 3 additions & 3 deletions command/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package command
import (
"testing"

"github.com/jrasell/levant/levant"
"github.com/jrasell/levant/template"
)

func TestDeploy_checkCanaryAutoPromote(t *testing.T) {
Expand All @@ -30,7 +30,7 @@ func TestDeploy_checkCanaryAutoPromote(t *testing.T) {
}

for i, c := range cases {
job, err := levant.RenderJob(c.File, "", &fVars)
job, err := template.RenderJob(c.File, "", &fVars)
if err != nil {
t.Fatalf("case %d failed: %v", i, err)
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestDeploy_checkForceBatch(t *testing.T) {
}

for i, c := range cases {
job, err := levant.RenderJob(c.File, "", &fVars)
job, err := template.RenderJob(c.File, "", &fVars)
if err != nil {
t.Fatalf("case %d failed: %v", i, err)
}
Expand Down
4 changes: 2 additions & 2 deletions command/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/jrasell/levant/helper"
"github.com/jrasell/levant/levant"
"github.com/jrasell/levant/template"
)

// RenderCommand is the command implementation that allows users to render a
Expand Down Expand Up @@ -78,7 +78,7 @@ func (c *RenderCommand) Run(args []string) int {
return 1
}

tpl, err = levant.RenderTemplate(templateFile, variables, &c.Meta.flagVars)
tpl, err = template.RenderTemplate(templateFile, variables, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
24 changes: 6 additions & 18 deletions levant/templater.go → template/render.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package levant
package template

import (
"bytes"
"fmt"
"io/ioutil"
"path"
"text/template"

"github.com/jrasell/levant/helper"
"github.com/rs/zerolog/log"
Expand All @@ -16,12 +15,6 @@ import (
"github.com/hashicorp/terraform/config"
)

const (
terraformVarExtension = ".tf"
yamlVarExtension = ".yaml"
ymlVarExtension = ".yml"
)

// RenderJob takes in a template and variables performing a render of the
// template followed by Nomad jobspec parse.
func RenderJob(templateFile, variableFile string, flagVars *map[string]string) (job *nomad.Job, err error) {
Expand All @@ -39,17 +32,17 @@ func RenderJob(templateFile, variableFile string, flagVars *map[string]string) (
// passed variables file.
func RenderTemplate(templateFile, variableFile string, flagVars *map[string]string) (tpl *bytes.Buffer, err error) {
if variableFile == "" {
log.Debug().Msgf("levant/templater: no variable file passed, trying defaults")
log.Debug().Msgf("template/render: no variable file passed, trying defaults")
if variableFile = helper.GetDefaultVarFile(); variableFile != "" {
log.Debug().Msgf("levant/templater: found default variable file, using %s", variableFile)
log.Debug().Msgf("template/render: found default variable file, using %s", variableFile)
}
}

// Process the variable file extension and log DEBUG so the template can be
// correctly rendered.
var ext string
if ext = path.Ext(variableFile); ext != "" {
log.Debug().Msgf("levant/templater: variable file extension %s detected", ext)
log.Debug().Msgf("template/render: variable file extension %s detected", ext)
}

src, err := ioutil.ReadFile(templateFile)
Expand All @@ -60,7 +53,7 @@ func RenderTemplate(templateFile, variableFile string, flagVars *map[string]stri
// If no command line variables are passed; log this as DEBUG to provide much
// greater feedback.
if len(*flagVars) == 0 {
log.Debug().Msgf("levant/templater: no command line variables passed")
log.Debug().Msgf("template/render: no command line variables passed")
}

switch ext {
Expand All @@ -73,7 +66,7 @@ func RenderTemplate(templateFile, variableFile string, flagVars *map[string]stri

case "":
// No variables file passed; render using any passed CLI variables.
log.Debug().Msgf("levant/templater: variable file not passed")
log.Debug().Msgf("template/render: variable file not passed")
tpl, err = readJobFile(string(src), flagVars)

default:
Expand Down Expand Up @@ -166,8 +159,3 @@ func readJobFile(src string, flagVars *map[string]string) (tpl *bytes.Buffer, er

return tpl, nil
}

// newTemplate returns an empty template with default options set
func newTemplate() *template.Template {
return template.New("jobTemplate").Delims("[[", "]]").Option("missingkey=error")
}
2 changes: 1 addition & 1 deletion levant/templater_test.go → template/render_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package levant
package template

import (
"strings"
Expand Down
16 changes: 16 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package template

import (
"text/template"
)

const (
terraformVarExtension = ".tf"
yamlVarExtension = ".yaml"
ymlVarExtension = ".yml"
)

// newTemplate returns an empty template with default options set
func newTemplate() *template.Template {
return template.New("jobTemplate").Delims("[[", "]]").Option("missingkey=error")
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 076fd63

Please sign in to comment.