github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/command/validate.go (about) 1 package command 2 3 import ( 4 "flag" 5 "fmt" 6 "path/filepath" 7 "strings" 8 9 "github.com/hashicorp/terraform/config" 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 = c.Meta.process(args, false) 21 var dirPath string 22 23 cmdFlags := flag.NewFlagSet("validate", flag.ContinueOnError) 24 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 25 if err := cmdFlags.Parse(args); err != nil { 26 return 1 27 } 28 29 if len(args) == 1 { 30 dirPath = args[0] 31 } else { 32 dirPath = "." 33 } 34 dir, err := filepath.Abs(dirPath) 35 if err != nil { 36 c.Ui.Error(fmt.Sprintf( 37 "Unable to locate directory %v\n", err.Error())) 38 } 39 40 rtnCode := c.validate(dir) 41 42 return rtnCode 43 } 44 45 func (c *ValidateCommand) Synopsis() string { 46 return "Validates the Terraform files" 47 } 48 49 func (c *ValidateCommand) Help() string { 50 helpText := ` 51 Usage: terraform validate [options] [path] 52 53 Reads the Terraform files in the given path (directory) and 54 validates their syntax and basic semantics. 55 56 This is not a full validation that is normally done with 57 a plan or apply operation, but can be used to verify the basic 58 syntax and usage of Terraform configurations is correct. 59 60 Options: 61 62 -no-color If specified, output won't contain any color. 63 64 ` 65 return strings.TrimSpace(helpText) 66 } 67 68 func (c *ValidateCommand) validate(dir string) int { 69 cfg, err := config.LoadDir(dir) 70 if err != nil { 71 c.Ui.Error(fmt.Sprintf( 72 "Error loading files %v\n", err.Error())) 73 return 1 74 } 75 err = cfg.Validate() 76 if err != nil { 77 c.Ui.Error(fmt.Sprintf( 78 "Error validating: %v\n", err.Error())) 79 return 1 80 } 81 return 0 82 }