command/validate: respond to --help

Fixes #5072

This is very simple: we need to process CLI flags on validate in order
to catch and respond to `--help`.
This commit is contained in:
Mitchell Hashimoto 2016-10-27 13:42:49 -04:00
parent 2b6fd7e968
commit b2950bc205
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
1 changed files with 27 additions and 4 deletions

View File

@ -1,8 +1,10 @@
package command
import (
"flag"
"fmt"
"path/filepath"
"strings"
"github.com/hashicorp/terraform/config"
)
@ -14,14 +16,16 @@ type ValidateCommand struct {
const defaultPath = "."
func (c *ValidateCommand) Help() string {
return ""
}
func (c *ValidateCommand) Run(args []string) int {
args = c.Meta.process(args, false)
var dirPath string
cmdFlags := flag.NewFlagSet("validate", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
if len(args) == 1 {
dirPath = args[0]
} else {
@ -42,6 +46,25 @@ func (c *ValidateCommand) Synopsis() string {
return "Validates the Terraform files"
}
func (c *ValidateCommand) Help() string {
helpText := `
Usage: terraform validate [options] [path]
Reads the Terraform files in the given path (directory) and
validates their syntax and basic semantics.
This is not a full validation that is normally done with
a plan or apply operation, but can be used to verify the basic
syntax and usage of Terraform configurations is correct.
Options:
-no-color If specified, output won't contain any color.
`
return strings.TrimSpace(helpText)
}
func (c *ValidateCommand) validate(dir string) int {
cfg, err := config.LoadDir(dir)
if err != nil {