mirror of https://github.com/status-im/consul.git
GH-3996: Add config-format flag to validate subcommand
This commit is contained in:
parent
5a67109f75
commit
ada1252480
|
@ -18,12 +18,17 @@ func New(ui cli.Ui) *cmd {
|
|||
type cmd struct {
|
||||
UI cli.Ui
|
||||
flags *flag.FlagSet
|
||||
// ConfigFormat forces all config files to be interpreted as this
|
||||
// format independent of their extension.
|
||||
configFormat string
|
||||
quiet bool
|
||||
help string
|
||||
}
|
||||
|
||||
func (c *cmd) init() {
|
||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
c.flags.StringVar(&c.configFormat, "config-format", "",
|
||||
"Config files are in this format irrespective of their extension. Must be 'hcl' or 'json'")
|
||||
c.flags.BoolVar(&c.quiet, "quiet", false,
|
||||
"When given, a successful run will produce no output.")
|
||||
c.help = flags.Usage(help, c.flags)
|
||||
|
@ -40,7 +45,7 @@ func (c *cmd) Run(args []string) int {
|
|||
c.UI.Error("Must specify at least one config file or directory")
|
||||
return 1
|
||||
}
|
||||
b, err := config.NewBuilder(config.Flags{ConfigFiles: configFiles})
|
||||
b, err := config.NewBuilder(config.Flags{ConfigFiles: configFiles, ConfigFormat: &c.configFormat})
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
|
||||
return 1
|
||||
|
|
|
@ -50,6 +50,63 @@ func TestValidateCommand_SucceedOnMinimalConfigFile(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestValidateCommand_SucceedWithMinimalJSONConfigFormat(t * testing.T) {
|
||||
t.Parallel()
|
||||
td := testutil.TempDir(t, "consul")
|
||||
defer os.RemoveAll(td)
|
||||
|
||||
fp := filepath.Join(td, "json.conf")
|
||||
err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
cmd := New(cli.NewMockUi())
|
||||
args := []string{"--config-format", "json", fp}
|
||||
|
||||
if code := cmd.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCommand_SucceedWithMinimalHCLConfigFormat(t * testing.T) {
|
||||
t.Parallel()
|
||||
td := testutil.TempDir(t, "consul")
|
||||
defer os.RemoveAll(td)
|
||||
|
||||
fp := filepath.Join(td, "hcl.conf")
|
||||
err := ioutil.WriteFile(fp, []byte("bind_addr = \"10.0.0.1\"\ndata_dir = \""+td+"\""), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
cmd := New(cli.NewMockUi())
|
||||
args := []string{"--config-format", "hcl", fp}
|
||||
|
||||
if code := cmd.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCommand_SucceedWithJSONAsHCL(t * testing.T) {
|
||||
t.Parallel()
|
||||
td := testutil.TempDir(t, "consul")
|
||||
defer os.RemoveAll(td)
|
||||
|
||||
fp := filepath.Join(td, "json.conf")
|
||||
err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
cmd := New(cli.NewMockUi())
|
||||
args := []string{"--config-format", "hcl", fp}
|
||||
|
||||
if code := cmd.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCommand_SucceedOnMinimalConfigDir(t *testing.T) {
|
||||
t.Parallel()
|
||||
td := testutil.TempDir(t, "consul")
|
||||
|
@ -68,6 +125,25 @@ func TestValidateCommand_SucceedOnMinimalConfigDir(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestValidateCommand_FailForInvalidJSONConfigFormat(t * testing.T) {
|
||||
t.Parallel()
|
||||
td := testutil.TempDir(t, "consul")
|
||||
defer os.RemoveAll(td)
|
||||
|
||||
fp := filepath.Join(td, "hcl.conf")
|
||||
err := ioutil.WriteFile(fp, []byte(`bind_addr = "10.0.0.1"\ndata_dir = "`+td+`"`), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
cmd := New(cli.NewMockUi())
|
||||
args := []string{"--config-format", "json", fp}
|
||||
|
||||
if code := cmd.Run(args); code == 0 {
|
||||
t.Fatalf("bad: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCommand_Quiet(t *testing.T) {
|
||||
t.Parallel()
|
||||
td := testutil.TempDir(t, "consul")
|
||||
|
|
Loading…
Reference in New Issue