2017-10-11 12:51:20 +00:00
|
|
|
package validate
|
2017-02-11 00:21:51 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-11 12:51:20 +00:00
|
|
|
"flag"
|
2017-02-11 00:21:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2017-10-11 12:51:20 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/mitchellh/cli"
|
2017-02-11 00:21:51 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
2017-10-11 22:32:35 +00:00
|
|
|
c.init()
|
2017-10-11 12:51:20 +00:00
|
|
|
return c
|
|
|
|
}
|
2017-10-11 12:51:18 +00:00
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
2017-10-11 12:51:18 +00:00
|
|
|
quiet bool
|
2017-10-11 22:32:35 +00:00
|
|
|
usage string
|
2017-10-11 12:51:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 22:32:35 +00:00
|
|
|
func (c *cmd) init() {
|
2017-10-11 12:51:20 +00:00
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.flags.BoolVar(&c.quiet, "quiet", false,
|
2017-10-11 12:51:18 +00:00
|
|
|
"When given, a successful run will produce no output.")
|
2017-10-11 22:32:35 +00:00
|
|
|
c.usage = flags.Usage(usage, c.flags, nil, nil)
|
2017-02-11 00:21:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2017-09-25 18:40:42 +00:00
|
|
|
c.UI.Error(err.Error())
|
2017-02-11 00:21:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
configFiles := c.flags.Args()
|
2017-02-11 00:21:51 +00:00
|
|
|
if len(configFiles) < 1 {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Must specify at least one config file or directory")
|
2017-02-11 00:21:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
b, err := config.NewBuilder(config.Flags{ConfigFiles: configFiles})
|
2017-02-11 00:21:51 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
2017-02-11 00:21:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
if _, err := b.BuildAndValidate(); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
|
|
|
return 1
|
|
|
|
}
|
2017-10-11 12:51:18 +00:00
|
|
|
if !c.quiet {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Output("Configuration is valid!")
|
2017-02-11 01:14:22 +00:00
|
|
|
}
|
2017-02-11 00:21:51 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
2017-02-11 00:21:51 +00:00
|
|
|
return "Validate config files/directories"
|
|
|
|
}
|
2017-10-11 12:51:20 +00:00
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
2017-10-11 22:32:35 +00:00
|
|
|
return c.usage
|
|
|
|
}
|
|
|
|
|
|
|
|
const usage = `Usage: consul validate [options] FILE_OR_DIRECTORY...
|
2017-10-11 12:51:20 +00:00
|
|
|
|
|
|
|
Performs a basic sanity test on Consul configuration files. For each file
|
|
|
|
or directory given, the validate command will attempt to parse the
|
|
|
|
contents just as the "consul agent" command would, and catch any errors.
|
|
|
|
This is useful to do a test of the configuration only, without actually
|
|
|
|
starting the agent.
|
|
|
|
|
|
|
|
Returns 0 if the configuration is valid, or 1 if there are problems.`
|