2015-05-04 18:41:19 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/command/agent"
|
2017-02-08 00:16:33 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2015-05-04 18:41:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigTestCommand is a Command implementation that is used to
|
|
|
|
// verify config files
|
|
|
|
type ConfigTestCommand struct {
|
2017-02-08 00:16:33 +00:00
|
|
|
base.Command
|
2015-05-04 18:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigTestCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: consul configtest [options]
|
|
|
|
|
2015-05-11 16:42:26 +00:00
|
|
|
Performs a basic sanity test on Consul configuration files. For each file
|
|
|
|
or directory given, the configtest 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.
|
2015-05-04 18:41:19 +00:00
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
` + c.Command.Help()
|
2015-05-04 18:41:19 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigTestCommand) Run(args []string) int {
|
2017-02-08 00:16:33 +00:00
|
|
|
var configFiles []string
|
|
|
|
|
|
|
|
f := c.Command.NewFlagSet(c)
|
|
|
|
f.Var((*agent.AppendSliceValue)(&configFiles), "config-file",
|
2017-02-07 01:50:51 +00:00
|
|
|
"Path to a JSON file to read configuration from. This can be specified multiple times.")
|
2017-02-08 00:16:33 +00:00
|
|
|
f.Var((*agent.AppendSliceValue)(&configFiles), "config-dir",
|
|
|
|
"Path to a directory to read configuration files from. This will read every file ending in "+
|
2017-02-07 01:50:51 +00:00
|
|
|
".json as configuration in this directory in alphabetical order.")
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
if err := c.Command.Parse(args); err != nil {
|
2015-05-04 18:41:19 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
if len(configFiles) <= 0 {
|
2015-05-04 18:41:19 +00:00
|
|
|
c.Ui.Error("Must specify config using -config-file or -config-dir")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
_, err := agent.ReadConfigPaths(configFiles)
|
2015-05-04 18:41:19 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigTestCommand) Synopsis() string {
|
|
|
|
return "Validate config file"
|
|
|
|
}
|