Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoloading of files #37

Merged
merged 7 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

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

"github.com/jrasell/levant/helper"
"github.com/jrasell/levant/levant"
"github.com/jrasell/levant/logging"
)
Expand All @@ -20,10 +21,15 @@ type DeployCommand struct {
// Help provides the help information for the deploy command.
func (c *DeployCommand) Help() string {
helpText := `
Usage: levant deploy [options] TEMPLATE
Usage: levant deploy [options] [TEMPLATE]

Deploy a Nomad job based on input templates and variable files.

Arguments:

TEMPLATE nomad job template
If no argument is given we look for a single *.nomad file

General Options:

-address=<http_address>
Expand All @@ -40,7 +46,7 @@ General Options:

-var-file=<file>
Used in conjunction with the -job-file will deploy a templated job to your
Nomad cluster.
Nomad cluster. [default: levant.(yaml|yml|tf)]

-force-count
Use the taskgroup count from the Nomad jobfile instead of the count that
Expand All @@ -57,7 +63,7 @@ func (c *DeployCommand) Synopsis() string {
// Run triggers a run of the Levant template and deploy functions.
func (c *DeployCommand) Run(args []string) int {

var variables, addr, log string
var variables, addr, log, templateFile string
var err error
var job *nomad.Job
var canary int
Expand All @@ -78,14 +84,22 @@ func (c *DeployCommand) Run(args []string) int {

args = flags.Args()

if len(args) != 1 {
logging.SetLevel(log)

if len(args) == 1 {
templateFile = args[0]
} else if len(args) == 0 {
if templateFile = helper.GetDefaultTmplFile(); templateFile == "" {
c.UI.Error(c.Help())
c.UI.Error("\nERROR: Template arg missing and no default template found")
return 1
}
} else {
c.UI.Error(c.Help())
return 1
}

logging.SetLevel(log)

job, err = levant.RenderJob(args[0], variables, &c.Meta.flagVars)
job, err = levant.RenderJob(templateFile, variables, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
24 changes: 19 additions & 5 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 @@ -19,10 +20,15 @@ type RenderCommand struct {
// Help provides the help information for the template command.
func (c *RenderCommand) Help() string {
helpText := `
Usage: levant render [options] TEMPLATE
Usage: levant render [options] [TEMPLATE]

Render a Nomad job template, useful for debugging.

Arguments:

TEMPLATE nomad job template
If no argument is given we look for a single *.nomad file

General Options:

-out=<file>
Expand All @@ -31,7 +37,7 @@ General Options:
rendered to stdout if this is not set.

-var-file=<file>
The variables file to render the template with.
The variables file to render the template with. [default: levant.(yaml|yml|tf)]
`
return strings.TrimSpace(helpText)
}
Expand All @@ -44,7 +50,7 @@ func (c *RenderCommand) Synopsis() string {
// Run triggers a run of the Levant template functions.
func (c *RenderCommand) Run(args []string) int {

var variables, outPath string
var variables, outPath, templateFile string
var err error
var tpl *bytes.Buffer

Expand All @@ -60,12 +66,20 @@ func (c *RenderCommand) Run(args []string) int {

args = flags.Args()

if len(args) != 1 {
if len(args) == 1 {
templateFile = args[0]
} else if len(args) == 0 {
if templateFile = helper.GetDefaultTmplFile(); templateFile == "" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we find more than one template file or no template file, it would be ideal if we returned a detailed error message to the user rather than just dumping them back to the usage information and expecting them to guess what went wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this?

c.UI.Error(c.Help())
c.UI.Error("\nERROR: Template arg missing and no default template found")
return 1
}
} else {
c.UI.Error(c.Help())
return 1
}

tpl, err = levant.RenderTemplate(args[0], variables, &c.Meta.flagVars)
tpl, err = levant.RenderTemplate(templateFile, variables, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
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 ""
}
19 changes: 12 additions & 7 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,11 +38,17 @@ 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 == "" {
logging.Debug("levant/templater: no variable file passed, trying defaults")
if variableFile = helper.GetDefaultVarFile(); variableFile != "" {
logging.Debug("levant/templater: found default variable file, using %s", variableFile)
}
}

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

Expand All @@ -58,16 +64,15 @@ 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:
tpl, err = renderTFTemplte(string(src), variableFile, flagVars)

case yamlVarExtension, ymlVarExtension:
// Run the render using a YAML varaible file.
// Run the render using a YAML variable file.
tpl, err = renderYAMLVarsTemplate(string(src), variableFile, flagVars)

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

Expand Down