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 Feb 5, 2016
1 parent 57836e0 commit e7ed5de
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ website/node_modules
.*.swp
.idea
*.test
*.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
20 changes: 20 additions & 0 deletions website/source/docs/commands/verify.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
layout: "docs"
page_title: "Command: verify"
sidebar_current: "docs-commands-verify"
description: |-
The `terraform verify` command is used to validate the format and structure of the terraform files.
---

# Command: verify

The `terraform verify` command is used to validate the format and structure of the terraform files.
Terraform performas an analysis of the terraform files, and will display information about missing
variables and errors in the structure of the files.

## Usage

Usage: `terraform verify [dir]`

By default, `verify` requires no flags and looks in the current directory
for the configurations.

0 comments on commit e7ed5de

Please sign in to comment.