github.com/wtlangford/terraform@v0.7.0-rc2.0.20160709103016-0e07a2776833/command/validate.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/hashicorp/terraform/config"
     8  )
     9  
    10  // ValidateCommand is a Command implementation that validates the terraform files
    11  type ValidateCommand struct {
    12  	Meta
    13  }
    14  
    15  const defaultPath = "."
    16  
    17  func (c *ValidateCommand) Help() string {
    18  	return ""
    19  }
    20  
    21  func (c *ValidateCommand) Run(args []string) int {
    22  	args = c.Meta.process(args, false)
    23  	var dirPath string
    24  
    25  	if len(args) == 1 {
    26  		dirPath = args[0]
    27  	} else {
    28  		dirPath = "."
    29  	}
    30  	dir, err := filepath.Abs(dirPath)
    31  	if err != nil {
    32  		c.Ui.Error(fmt.Sprintf(
    33  			"Unable to locate directory %v\n", err.Error()))
    34  	}
    35  
    36  	rtnCode := c.validate(dir)
    37  
    38  	return rtnCode
    39  }
    40  
    41  func (c *ValidateCommand) Synopsis() string {
    42  	return "Validates the Terraform files"
    43  }
    44  
    45  func (c *ValidateCommand) validate(dir string) int {
    46  	cfg, err := config.LoadDir(dir)
    47  	if err != nil {
    48  		c.Ui.Error(fmt.Sprintf(
    49  			"Error loading files %v\n", err.Error()))
    50  		return 1
    51  	}
    52  	err = cfg.Validate()
    53  	if err != nil {
    54  		c.Ui.Error(fmt.Sprintf(
    55  			"Error validating: %v\n", err.Error()))
    56  		return 1
    57  	}
    58  	return 0
    59  }