Skip to content

Commit

Permalink
Merge pull request #62 from jrasell/f_61
Browse files Browse the repository at this point in the history
Update logging around passed variables and overridden variables.
  • Loading branch information
jrasell committed Dec 10, 2017
2 parents f01d4e2 + 6a83d6c commit 247dc53
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 6 additions & 0 deletions helper/variable.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package helper

import "github.com/jrasell/levant/logging"

// VariableMerge merges the passed file variables with the flag variabes to
// provide a single set of variables. The flagVars will always prevale over file
// variables.
Expand All @@ -8,13 +10,17 @@ func VariableMerge(fileVars *map[string]interface{}, flagVars *map[string]string
out := make(map[string]interface{})

for k, v := range *flagVars {
logging.Info("helper/variable: using command line variable with key %s and value %s", k, v)
out[k] = v
}

for k, v := range *fileVars {
if _, ok := out[k]; ok {
logging.Debug("helper/variable: variable from file with key %s and value %s overridden by CLI var",
k, v)
continue
}
logging.Info("helper/variable: using variable with key %s and value %v from file", k, v)
out[k] = v
}

Expand Down
3 changes: 2 additions & 1 deletion levant/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func (c *nomadClient) Deploy(job *nomad.Job, autoPromote int, forceCount bool) (

// If job.Type isn't set we can't continue
if job.Type == nil {
logging.Error("levant/deploy: Nomad job `type` is not set, should be set to `service`")
logging.Error("levant/deploy: Nomad job `type` is not set; should be set to `%s`, `%s` or `%s`",
nomadStructs.JobTypeBatch, nomadStructs.JobTypeSystem, nomadStructs.JobTypeService)
return
}

Expand Down
26 changes: 19 additions & 7 deletions levant/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,39 @@ func RenderJob(templateFile, variableFile string, flagVars *map[string]string) (
// RenderTemplate is the main entry point to render the template based on the
// passed variables file.
func RenderTemplate(templateFile, variableFile string, flagVars *map[string]string) (tpl *bytes.Buffer, err error) {

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

src, err := ioutil.ReadFile(templateFile)
if err != nil {
return
}

// If no command line variables are passed; log this as DEBUG to provide much
// greater feedback.
if len(*flagVars) == 0 {
logging.Debug("levant/templater: no command line variables passed")
}

switch ext {
case terraformVarExtention:
logging.Debug("levant/templater: detected .tf variable file extension")
// Run the render using variables formatted in Terraforms .tf extension.
tpl, err = renderTFTemplte(string(src), variableFile, flagVars)

case yamlVarExtension, ymlVarExtension:
logging.Debug("levant/templater: detected .yaml or .yml variable file extension")
// Run the render using a YAML varaible file.
tpl, err = renderYAMLVarsTemplate(string(src), variableFile, flagVars)
case "":
if len(*flagVars) == 0 {
tpl = bytes.NewBuffer(src)
}

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

default:
err = fmt.Errorf("variables file extension %v not supported", ext)
}
Expand Down

0 comments on commit 247dc53

Please sign in to comment.