mirror of https://github.com/status-im/consul.git
commands: move validate command to separate pkg
This commit is contained in:
parent
cef6a80ae6
commit
e0551b80a6
|
@ -8,6 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/command/validate"
|
||||||
"github.com/hashicorp/consul/version"
|
"github.com/hashicorp/consul/version"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
@ -349,12 +350,7 @@ func init() {
|
||||||
},
|
},
|
||||||
|
|
||||||
"validate": func() (cli.Command, error) {
|
"validate": func() (cli.Command, error) {
|
||||||
return &ValidateCommand{
|
return validate.New(ui), nil
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
Flags: FlagSetNone,
|
|
||||||
UI: ui,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"version": func() (cli.Command, error) {
|
"version": func() (cli.Command, error) {
|
||||||
|
|
|
@ -1,55 +1,43 @@
|
||||||
package command
|
package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent/config"
|
"github.com/hashicorp/consul/agent/config"
|
||||||
|
"github.com/hashicorp/consul/command/flags"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ValidateCommand is a Command implementation that is used to
|
func New(ui cli.Ui) *cmd {
|
||||||
// verify config files
|
c := &cmd{UI: ui}
|
||||||
type ValidateCommand struct {
|
c.initFlags()
|
||||||
BaseCommand
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
// flags
|
type cmd struct {
|
||||||
|
UI cli.Ui
|
||||||
|
flags *flag.FlagSet
|
||||||
quiet bool
|
quiet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ValidateCommand) initFlags() {
|
func (c *cmd) initFlags() {
|
||||||
c.InitFlagSet()
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
c.FlagSet.BoolVar(&c.quiet, "quiet", false,
|
c.flags.BoolVar(&c.quiet, "quiet", false,
|
||||||
"When given, a successful run will produce no output.")
|
"When given, a successful run will produce no output.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ValidateCommand) Help() string {
|
func (c *cmd) Run(args []string) int {
|
||||||
c.initFlags()
|
if err := c.flags.Parse(args); err != nil {
|
||||||
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())
|
c.UI.Error(err.Error())
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
configFiles := c.FlagSet.Args()
|
configFiles := c.flags.Args()
|
||||||
if len(configFiles) < 1 {
|
if len(configFiles) < 1 {
|
||||||
c.UI.Error("Must specify at least one config file or directory")
|
c.UI.Error("Must specify at least one config file or directory")
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := config.NewBuilder(config.Flags{ConfigFiles: configFiles})
|
b, err := config.NewBuilder(config.Flags{ConfigFiles: configFiles})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
||||||
|
@ -59,13 +47,26 @@ func (c *ValidateCommand) Run(args []string) int {
|
||||||
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.quiet {
|
if !c.quiet {
|
||||||
c.UI.Output("Configuration is valid!")
|
c.UI.Output("Configuration is valid!")
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ValidateCommand) Synopsis() string {
|
func (c *cmd) Synopsis() string {
|
||||||
return "Validate config files/directories"
|
return "Validate config files/directories"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Help() string {
|
||||||
|
s := `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.`
|
||||||
|
|
||||||
|
return flags.Usage(s, c.flags, nil, nil)
|
||||||
|
}
|
|
@ -1,37 +1,28 @@
|
||||||
package command
|
package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/testutil"
|
"github.com/hashicorp/consul/testutil"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testValidateCommand(t *testing.T) (*cli.MockUi, *ValidateCommand) {
|
func TestValidateCommand_noTabs(t *testing.T) {
|
||||||
ui := cli.NewMockUi()
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
||||||
return ui, &ValidateCommand{
|
t.Fatal("usage has tabs")
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
UI: ui,
|
|
||||||
Flags: FlagSetNone,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateCommand_implements(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
var _ cli.Command = &ValidateCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidateCommandFailOnEmptyFile(t *testing.T) {
|
func TestValidateCommandFailOnEmptyFile(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
tmpFile := testutil.TempFile(t, "consul")
|
tmpFile := testutil.TempFile(t, "consul")
|
||||||
defer os.RemoveAll(tmpFile.Name())
|
defer os.RemoveAll(tmpFile.Name())
|
||||||
|
|
||||||
_, cmd := testValidateCommand(t)
|
cmd := New(cli.NewMockUi())
|
||||||
|
|
||||||
args := []string{tmpFile.Name()}
|
args := []string{tmpFile.Name()}
|
||||||
|
|
||||||
if code := cmd.Run(args); code == 0 {
|
if code := cmd.Run(args); code == 0 {
|
||||||
|
@ -49,8 +40,7 @@ func TestValidateCommandSucceedOnMinimalConfigFile(t *testing.T) {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, cmd := testValidateCommand(t)
|
cmd := New(cli.NewMockUi())
|
||||||
|
|
||||||
args := []string{fp}
|
args := []string{fp}
|
||||||
|
|
||||||
if code := cmd.Run(args); code != 0 {
|
if code := cmd.Run(args); code != 0 {
|
||||||
|
@ -67,8 +57,7 @@ func TestValidateCommandSucceedOnMinimalConfigDir(t *testing.T) {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, cmd := testValidateCommand(t)
|
cmd := New(cli.NewMockUi())
|
||||||
|
|
||||||
args := []string{td}
|
args := []string{td}
|
||||||
|
|
||||||
if code := cmd.Run(args); code != 0 {
|
if code := cmd.Run(args); code != 0 {
|
||||||
|
@ -87,8 +76,8 @@ func TestValidateCommandQuiet(t *testing.T) {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ui, cmd := testValidateCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
cmd := New(ui)
|
||||||
args := []string{"-quiet", td}
|
args := []string{"-quiet", td}
|
||||||
|
|
||||||
if code := cmd.Run(args); code != 0 {
|
if code := cmd.Run(args); code != 0 {
|
Loading…
Reference in New Issue