github.com/spirius/terraform@v0.10.0-beta2.0.20170714185654-87b2c0cf8fea/command/validate.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/config"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  // ValidateCommand is a Command implementation that validates the terraform files
    13  type ValidateCommand struct {
    14  	Meta
    15  }
    16  
    17  const defaultPath = "."
    18  
    19  func (c *ValidateCommand) Run(args []string) int {
    20  	args, err := c.Meta.process(args, false)
    21  	if err != nil {
    22  		return 1
    23  	}
    24  	var checkVars bool
    25  
    26  	cmdFlags := c.Meta.flagSet("validate")
    27  	cmdFlags.BoolVar(&checkVars, "check-variables", true, "check-variables")
    28  	cmdFlags.Usage = func() {
    29  		c.Ui.Error(c.Help())
    30  	}
    31  	if err := cmdFlags.Parse(args); err != nil {
    32  		return 1
    33  	}
    34  
    35  	args = cmdFlags.Args()
    36  
    37  	var dirPath string
    38  	if len(args) == 1 {
    39  		dirPath = args[0]
    40  	} else {
    41  		dirPath = "."
    42  	}
    43  	dir, err := filepath.Abs(dirPath)
    44  	if err != nil {
    45  		c.Ui.Error(fmt.Sprintf(
    46  			"Unable to locate directory %v\n", err.Error()))
    47  	}
    48  
    49  	rtnCode := c.validate(dir, checkVars)
    50  
    51  	return rtnCode
    52  }
    53  
    54  func (c *ValidateCommand) Synopsis() string {
    55  	return "Validates the Terraform files"
    56  }
    57  
    58  func (c *ValidateCommand) Help() string {
    59  	helpText := `
    60  Usage: terraform validate [options] [dir]
    61  
    62    Validate the terraform files in a directory. Validation includes a
    63    basic check of syntax as well as checking that all variables declared
    64    in the configuration are specified in one of the possible ways:
    65  
    66        -var foo=...
    67        -var-file=foo.vars
    68        TF_VAR_foo environment variable
    69        terraform.tfvars
    70        default value
    71  
    72    If dir is not specified, then the current directory will be used.
    73  
    74  Options:
    75  
    76    -check-variables=true If set to true (default), the command will check
    77                          whether all required variables have been specified.
    78  
    79    -no-color             If specified, output won't contain any color.
    80  
    81    -var 'foo=bar'        Set a variable in the Terraform configuration. This
    82                          flag can be set multiple times.
    83  
    84    -var-file=foo         Set variables in the Terraform configuration from
    85                          a file. If "terraform.tfvars" is present, it will be
    86                          automatically loaded if this flag is not specified.
    87  `
    88  	return strings.TrimSpace(helpText)
    89  }
    90  
    91  func (c *ValidateCommand) validate(dir string, checkVars bool) int {
    92  	cfg, err := config.LoadDir(dir)
    93  	if err != nil {
    94  		c.Ui.Error(fmt.Sprintf(
    95  			"Error loading files %v\n", err.Error()))
    96  		return 1
    97  	}
    98  	err = cfg.Validate()
    99  	if err != nil {
   100  		c.Ui.Error(fmt.Sprintf(
   101  			"Error validating: %v\n", err.Error()))
   102  		return 1
   103  	}
   104  
   105  	if checkVars {
   106  		mod, err := c.Module(dir)
   107  		if err != nil {
   108  			c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err))
   109  			return 1
   110  		}
   111  
   112  		opts := c.contextOpts()
   113  		opts.Module = mod
   114  
   115  		tfCtx, err := terraform.NewContext(opts)
   116  		if err != nil {
   117  			c.Ui.Error(fmt.Sprintf("Error: %v\n", err.Error()))
   118  			return 1
   119  		}
   120  
   121  		if !validateContext(tfCtx, c.Ui) {
   122  			return 1
   123  		}
   124  	}
   125  
   126  	return 0
   127  }