mirror of
https://github.com/status-im/consul.git
synced 2025-02-15 07:07:18 +00:00
This patch refactors the commands that use the mitchellh/cli library to populate the command line flag set in both the Run() and the Help() method. Earlier versions of the mitchellh/cli library relied on the Run() method to populuate the flagset for generating the usage screen. This has changed in later versions and was previously solved with a small monkey patch to the library to restore the old behavior. However, this makes upgrading the library difficult since the patch has to be restored every time. This patch addresses this by moving the command line flags into an initFlags() method where appropriate and also moving all variables for the flags from the Run() method into the command itself. Fixes #3536
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/consul/agent/config"
|
|
)
|
|
|
|
// ValidateCommand is a Command implementation that is used to
|
|
// verify config files
|
|
type ValidateCommand struct {
|
|
BaseCommand
|
|
|
|
// flags
|
|
quiet bool
|
|
}
|
|
|
|
func (c *ValidateCommand) initFlags() {
|
|
c.InitFlagSet()
|
|
c.FlagSet.BoolVar(&c.quiet, "quiet", false,
|
|
"When given, a successful run will produce no output.")
|
|
}
|
|
|
|
func (c *ValidateCommand) Help() string {
|
|
c.initFlags()
|
|
return c.HelpCommand(`
|
|
Usage: consul validate [options] FILE_OR_DIRECTORY...
|
|
|
|
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.
|
|
|
|
`)
|
|
}
|
|
|
|
func (c *ValidateCommand) Run(args []string) int {
|
|
c.initFlags()
|
|
if err := c.FlagSet.Parse(args); err != nil {
|
|
c.UI.Error(err.Error())
|
|
return 1
|
|
}
|
|
|
|
configFiles := c.FlagSet.Args()
|
|
if len(configFiles) < 1 {
|
|
c.UI.Error("Must specify at least one config file or directory")
|
|
return 1
|
|
}
|
|
|
|
b, err := config.NewBuilder(config.Flags{ConfigFiles: configFiles})
|
|
if err != nil {
|
|
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
|
return 1
|
|
}
|
|
if _, err := b.BuildAndValidate(); err != nil {
|
|
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
|
return 1
|
|
}
|
|
|
|
if !c.quiet {
|
|
c.UI.Output("Configuration is valid!")
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (c *ValidateCommand) Synopsis() string {
|
|
return "Validate config files/directories"
|
|
}
|