Skip to content

Commit

Permalink
round 2 of autoloading
Browse files Browse the repository at this point in the history
  * If no TEMPLATE is given as positional arg we scan the working
    directory for *.nomad files. If only 1 match is found we use that to
    proceed.
  * If no -var-file is given we check the working directory for files
    (in this order) named:
    - levant.yaml
    - levant.yml
    - levant.tf
    The first match will be used.
  • Loading branch information
Yorick Gersie committed Dec 12, 2017
1 parent 67e17fe commit 282b74e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
12 changes: 6 additions & 6 deletions command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package command

import (
"fmt"
"os"
"strings"

nomad "github.com/hashicorp/nomad/api"

"github.com/jrasell/levant/helper"
"github.com/jrasell/levant/levant"
"github.com/jrasell/levant/logging"
)
Expand All @@ -27,7 +27,8 @@ Usage: levant deploy [options] [TEMPLATE]
Arguments:
TEMPLATE nomad job template [default: levant.nomad]
TEMPLATE nomad job template
If no argument is given we look for a single *.nomad file
General Options:
Expand Down Expand Up @@ -83,11 +84,12 @@ func (c *DeployCommand) Run(args []string) int {

args = flags.Args()

logging.SetLevel(log)

if len(args) == 1 {
templateFile = args[0]
} else if len(args) == 0 {
templateFile = "levant.nomad"
if _, err := os.Stat(templateFile); os.IsNotExist(err) {
if templateFile = helper.GetDefaultTmplFile(); templateFile == "" {
c.UI.Error(c.Help())
return 1
}
Expand All @@ -96,8 +98,6 @@ func (c *DeployCommand) Run(args []string) int {
return 1
}

logging.SetLevel(log)

job, err = levant.RenderJob(templateFile, variables, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
Expand Down
7 changes: 4 additions & 3 deletions command/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

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

Expand All @@ -25,7 +26,8 @@ Usage: levant render [options] [TEMPLATE]
Arguments:
TEMPLATE nomad job template [default: levant.nomad]
TEMPLATE nomad job template
If no argument is given we look for a single *.nomad file
General Options:
Expand Down Expand Up @@ -67,8 +69,7 @@ func (c *RenderCommand) Run(args []string) int {
if len(args) == 1 {
templateFile = args[0]
} else if len(args) == 0 {
templateFile = "levant.nomad"
if _, err := os.Stat(templateFile); os.IsNotExist(err) {
if templateFile = helper.GetDefaultTmplFile(); templateFile == "" {
c.UI.Error(c.Help())
return 1
}
Expand Down
40 changes: 40 additions & 0 deletions helper/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package helper

import (
"os"
"path/filepath"

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

// GetDefaultTmplFile checks the current working directory for *.nomad files.
// If only 1 is found we return the match.
func GetDefaultTmplFile() (templateFile string) {
if matches, _ := filepath.Glob("*.nomad"); matches != nil {
if len(matches) == 1 {
templateFile = matches[0]
logging.Debug("helper/files: using templatefile `%v`", templateFile)
return templateFile
}
}
return ""
}

// GetDefaultVarFile checks the current working directory for levant.(yaml|yml|tf) files.
// The first match is returned.
func GetDefaultVarFile() (varFile string) {
if _, err := os.Stat("levant.yaml"); !os.IsNotExist(err) {
logging.Debug("helper/files: using default var-file `levant.yaml`")
return "levant.yaml"
}
if _, err := os.Stat("levant.yml"); !os.IsNotExist(err) {
logging.Debug("helper/files: using default var-file `levant.yml`")
return "levant.yml"
}
if _, err := os.Stat("levant.tf"); !os.IsNotExist(err) {
logging.Debug("helper/files: using default var-file `levant.tf`")
return "levant.tf"
}
logging.Debug("helper/files: no default var-file found")
return ""
}
10 changes: 7 additions & 3 deletions levant/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

const (
terraformVarExtention = ".tf"
terraformVarExtension = ".tf"
yamlVarExtension = ".yaml"
ymlVarExtension = ".yml"
)
Expand All @@ -38,6 +38,10 @@ 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) {
if variableFile == "" {
variableFile = helper.GetDefaultVarFile()
}

// Process the variable file extension and log DEBUG so the template can be
// correctly rendered.
ext := path.Ext(variableFile)
Expand All @@ -57,8 +61,8 @@ func RenderTemplate(templateFile, variableFile string, flagVars *map[string]stri
}

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

case yamlVarExtension, ymlVarExtension:
Expand Down

0 comments on commit 282b74e

Please sign in to comment.