Skip to content

Commit

Permalink
Added verify command
Browse files Browse the repository at this point in the history
  • Loading branch information
Soren Mathiasen committed Nov 6, 2015
1 parent bf11be8 commit 01fe2a1
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ website/node_modules
*~
.*.swp
.idea
*.iml
9 changes: 9 additions & 0 deletions command/test-fixtures/verify-invalid/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "test_instance" "foo" {
ami = "bar"

# This is here because at some point it caused a test failure
network_interface {
device_index = 0
description = "Main network interface ${var.this_is_an_error}"
}
}
9 changes: 9 additions & 0 deletions command/test-fixtures/verify-valid/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "test_instance" "foo" {
ami = "bar"

# This is here because at some point it caused a test failure
network_interface {
device_index = 0
description = "Main network interface"
}
}
58 changes: 58 additions & 0 deletions command/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package command

import (
"fmt"
"github.com/hashicorp/terraform/config"
"path/filepath"
)

// VerifyCommand is a Command implementation that verifies the terraform files
type VerifyCommand struct {
Meta
}

const defaultPath = "."

func (c *VerifyCommand) Help() string {
return ""
}

func (c *VerifyCommand) Run(args []string) int {
args = c.Meta.process(args, false)
var dirPath string

if len(args) == 1 {
dirPath = args[0]
} else {
dirPath = "."
}
dir, err := filepath.Abs(dirPath)
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Unable to locate directory %v\n", err.Error()))
}

rtnCode := c.validate(dir)

return rtnCode
}

func (c *VerifyCommand) Synopsis() string {
return "Validates the Terraform files"
}

func (c *VerifyCommand) validate(dir string) int {
cfg, err := config.LoadDir(dir)
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error loading files %v\n", err.Error()))
return 1
}
err = cfg.Validate()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error validating: %v\n", err.Error()))
return 1
}
return 0
}
40 changes: 40 additions & 0 deletions command/verify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package command

import (
"github.com/mitchellh/cli"
"testing"
)

func TestVerifyCommand(t *testing.T) {
ui := new(cli.MockUi)
c := &VerifyCommand{
Meta: Meta{
Ui: ui,
},
}

args := []string{
testFixturePath("verify-valid"),
}

if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}

func TestVerifyFailingCommand(t *testing.T) {
ui := new(cli.MockUi)
c := &VerifyCommand{
Meta: Meta{
Ui: ui,
},
}

args := []string{
testFixturePath("verify-invalid"),
}

if code := c.Run(args); code == 0 {
t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
6 changes: 6 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func init() {
}, nil
},

"verify": func() (cli.Command, error) {
return &command.VerifyCommand{
Meta: meta,
}, nil
},

"version": func() (cli.Command, error) {
return &command.VersionCommand{
Meta: meta,
Expand Down

0 comments on commit 01fe2a1

Please sign in to comment.