Enforce json or hcl extension to Consul config files, updated unit tests
This commit is contained in:
parent
b0549da664
commit
1e8385df2c
|
@ -174,6 +174,9 @@ func (b *Builder) ReadPath(path string) error {
|
||||||
// ReadFile parses a JSON or HCL config file and appends it to the list of
|
// ReadFile parses a JSON or HCL config file and appends it to the list of
|
||||||
// config sources.
|
// config sources.
|
||||||
func (b *Builder) ReadFile(path string) error {
|
func (b *Builder) ReadFile(path string) error {
|
||||||
|
if !strings.HasSuffix(path, ".json") && !strings.HasSuffix(path, ".hcl") {
|
||||||
|
return fmt.Errorf(`Missing or invalid file extension for %q. Please use ".json" or ".hcl".`, path)
|
||||||
|
}
|
||||||
data, err := ioutil.ReadFile(path)
|
data, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("config: ReadFile failed on %s: %s", path, err)
|
return fmt.Errorf("config: ReadFile failed on %s: %s", path, err)
|
||||||
|
|
|
@ -229,11 +229,17 @@ func TestProtectDataDir(t *testing.T) {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfgFile := testutil.TempFile(t, "consul")
|
cfgDir := testutil.TempDir(t, "consul-config")
|
||||||
defer os.Remove(cfgFile.Name())
|
defer os.RemoveAll(cfgDir)
|
||||||
|
|
||||||
|
cfgFilePath := filepath.Join(cfgDir, "consul.json")
|
||||||
|
cfgFile, err := os.Create(cfgFilePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to create file %v, got error:%v", cfgFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
content := fmt.Sprintf(`{"server": true, "bind_addr" : "10.0.0.1", "data_dir": "%s"}`, dir)
|
content := fmt.Sprintf(`{"server": true, "bind_addr" : "10.0.0.1", "data_dir": "%s"}`, dir)
|
||||||
_, err := cfgFile.Write([]byte(content))
|
_, err = cfgFile.Write([]byte(content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue