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
|
2018-03-29 19:06:48 +00:00
|
|
|
// configFormat forces all config files to be interpreted as this
|
2018-03-29 18:30:05 +00:00
|
|
|
// format independent of their extension.
|
|
|
|
configFormat string
|
2018-03-29 18:35:49 +00:00
|
|
|
quiet bool
|
|
|
|
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)
|
2018-03-29 18:30:05 +00:00
|
|
|
c.flags.StringVar(&c.configFormat, "config-format", "",
|
|
|
|
"Config files are in this format irrespective of their extension. Must be 'hcl' or 'json'")
|
2017-10-11 12:51:20 +00:00
|
|
|
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
|
|
|
|
}
|
2018-04-13 16:57:25 +00:00
|
|
|
|
2018-04-02 13:23:01 +00:00
|
|
|
if c.configFormat != "" && c.configFormat != "json" && c.configFormat != "hcl" {
|
|
|
|
c.UI.Error("-config-format must be either 'hcl' or 'json")
|
2018-04-13 16:57:25 +00:00
|
|
|
return 1
|
2018-04-02 13:23:01 +00:00
|
|
|
}
|
2018-04-13 16:57:25 +00:00
|
|
|
|
2020-06-17 19:14:40 +00:00
|
|
|
b, err := config.NewBuilder(config.BuilderOpts{ConfigFiles: configFiles, ConfigFormat: c.configFormat})
|
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 {
|
2020-06-17 19:14:40 +00:00
|
|
|
for _, w := range b.Warnings {
|
|
|
|
c.UI.Warn(w)
|
|
|
|
}
|
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.
|
|
|
|
`
|