From ada12524809f2a4be21340e37b5c47dd05f4cddb Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Thu, 29 Mar 2018 14:30:05 -0400 Subject: [PATCH] GH-3996: Add config-format flag to validate subcommand --- command/validate/validate.go | 7 ++- command/validate/validate_test.go | 76 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/command/validate/validate.go b/command/validate/validate.go index 1b3923239c..f4bf9053c8 100644 --- a/command/validate/validate.go +++ b/command/validate/validate.go @@ -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 diff --git a/command/validate/validate_test.go b/command/validate/validate_test.go index 517e02b620..a375b02fcb 100644 --- a/command/validate/validate_test.go +++ b/command/validate/validate_test.go @@ -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")