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-17 13:44:20 +00:00
|
|
|
help 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-17 22:00:01 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
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-10-17 13:44:20 +00:00
|
|
|
return synopsis
|
2017-02-11 00:21:51 +00:00
|
|
|
}
|
2017-10-11 12:51:20 +00:00
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return c.help
|
2017-10-11 22:32:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
const synopsis = "Validate config files/directories"
|
|
|
|
const help = `
|
|
|
|
Usage: consul validate [options] FILE_OR_DIRECTORY...
|
2017-10-11 12:51:20 +00:00
|
|
|
|
2017-10-20 01:59:05 +00:00
|
|
|
Performs a thorough 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.
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
This is useful to do a test of the configuration only, without actually
|
2017-10-20 01:59:05 +00:00
|
|
|
starting the agent. This performs all of the validation the agent would, so
|
|
|
|
this should be given the complete set of configuration files that are going
|
|
|
|
to be loaded by the agent. This command cannot operate on partial
|
|
|
|
configuration fragments since those won't pass the full agent validation.
|
2017-10-11 12:51:20 +00:00
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
Returns 0 if the configuration is valid, or 1 if there are problems.
|
|
|
|
`
|