mirror of https://github.com/status-im/consul.git
fix: return error when config file with unknown extension is passed (#15107)
This commit is contained in:
parent
01a4510d6c
commit
6fb628c07d
|
@ -0,0 +1,3 @@
|
|||
```release-note:bug
|
||||
cli: fatal error if config file does not have HCL or JSON extension, instead of warn and skip
|
||||
```
|
|
@ -56,6 +56,10 @@ type LoadOpts struct {
|
|||
|
||||
// ConfigFiles is a slice of paths to config files and directories that will
|
||||
// be loaded.
|
||||
//
|
||||
// It is an error for any config files to have an extension other than `hcl`
|
||||
// or `json`, unless ConfigFormat is also set. However, non-HCL/JSON files in
|
||||
// a config directory are merely skipped, with a warning.
|
||||
ConfigFiles []string
|
||||
|
||||
// ConfigFormat forces all config files to be interpreted as this format
|
||||
|
@ -228,8 +232,7 @@ func (b *builder) sourcesFromPath(path string, format string) ([]Source, error)
|
|||
|
||||
if !fi.IsDir() {
|
||||
if !shouldParseFile(path, format) {
|
||||
b.warn("skipping file %v, extension must be .hcl or .json, or config format must be set", path)
|
||||
return nil, nil
|
||||
return nil, fmt.Errorf("file %v has unknown extension; must be .hcl or .json, or config format must be set", path)
|
||||
}
|
||||
|
||||
src, err := newSourceFromFile(path, format)
|
||||
|
|
|
@ -68,41 +68,6 @@ func TestShouldParseFile(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestNewBuilder_PopulatesSourcesFromConfigFiles(t *testing.T) {
|
||||
paths := setupConfigFiles(t)
|
||||
|
||||
b, err := newBuilder(LoadOpts{ConfigFiles: paths})
|
||||
require.NoError(t, err)
|
||||
|
||||
expected := []Source{
|
||||
FileSource{Name: paths[0], Format: "hcl", Data: "content a"},
|
||||
FileSource{Name: paths[1], Format: "json", Data: "content b"},
|
||||
FileSource{Name: filepath.Join(paths[3], "a.hcl"), Format: "hcl", Data: "content a"},
|
||||
FileSource{Name: filepath.Join(paths[3], "b.json"), Format: "json", Data: "content b"},
|
||||
}
|
||||
require.Equal(t, expected, b.Sources)
|
||||
require.Len(t, b.Warnings, 2)
|
||||
}
|
||||
|
||||
func TestNewBuilder_PopulatesSourcesFromConfigFiles_WithConfigFormat(t *testing.T) {
|
||||
paths := setupConfigFiles(t)
|
||||
|
||||
b, err := newBuilder(LoadOpts{ConfigFiles: paths, ConfigFormat: "hcl"})
|
||||
require.NoError(t, err)
|
||||
|
||||
expected := []Source{
|
||||
FileSource{Name: paths[0], Format: "hcl", Data: "content a"},
|
||||
FileSource{Name: paths[1], Format: "hcl", Data: "content b"},
|
||||
FileSource{Name: paths[2], Format: "hcl", Data: "content c"},
|
||||
FileSource{Name: filepath.Join(paths[3], "a.hcl"), Format: "hcl", Data: "content a"},
|
||||
FileSource{Name: filepath.Join(paths[3], "b.json"), Format: "hcl", Data: "content b"},
|
||||
FileSource{Name: filepath.Join(paths[3], "c.yaml"), Format: "hcl", Data: "content c"},
|
||||
}
|
||||
require.Equal(t, expected, b.Sources)
|
||||
}
|
||||
|
||||
// TODO: this would be much nicer with gotest.tools/fs
|
||||
func setupConfigFiles(t *testing.T) []string {
|
||||
t.Helper()
|
||||
path, err := os.MkdirTemp("", t.Name())
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { os.RemoveAll(path) })
|
||||
|
@ -121,12 +86,43 @@ func setupConfigFiles(t *testing.T) []string {
|
|||
err = os.WriteFile(filepath.Join(dir, "c.yaml"), []byte("content c"), 0644)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
return []string{
|
||||
paths := []string{
|
||||
filepath.Join(path, "a.hcl"),
|
||||
filepath.Join(path, "b.json"),
|
||||
filepath.Join(path, "c.yaml"),
|
||||
subpath,
|
||||
}
|
||||
|
||||
t.Run("fail on unknown files", func(t *testing.T) {
|
||||
_, err := newBuilder(LoadOpts{ConfigFiles: append(paths, subpath)})
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("skip on unknown files in dir", func(t *testing.T) {
|
||||
b, err := newBuilder(LoadOpts{ConfigFiles: []string{subpath}})
|
||||
require.NoError(t, err)
|
||||
|
||||
expected := []Source{
|
||||
FileSource{Name: filepath.Join(subpath, "a.hcl"), Format: "hcl", Data: "content a"},
|
||||
FileSource{Name: filepath.Join(subpath, "b.json"), Format: "json", Data: "content b"},
|
||||
}
|
||||
require.Equal(t, expected, b.Sources)
|
||||
require.Len(t, b.Warnings, 1)
|
||||
})
|
||||
|
||||
t.Run("force config format", func(t *testing.T) {
|
||||
b, err := newBuilder(LoadOpts{ConfigFiles: append(paths, subpath), ConfigFormat: "hcl"})
|
||||
require.NoError(t, err)
|
||||
|
||||
expected := []Source{
|
||||
FileSource{Name: paths[0], Format: "hcl", Data: "content a"},
|
||||
FileSource{Name: paths[1], Format: "hcl", Data: "content b"},
|
||||
FileSource{Name: paths[2], Format: "hcl", Data: "content c"},
|
||||
FileSource{Name: filepath.Join(subpath, "a.hcl"), Format: "hcl", Data: "content a"},
|
||||
FileSource{Name: filepath.Join(subpath, "b.json"), Format: "hcl", Data: "content b"},
|
||||
FileSource{Name: filepath.Join(subpath, "c.yaml"), Format: "hcl", Data: "content c"},
|
||||
}
|
||||
require.Equal(t, expected, b.Sources)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLoad_NodeName(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue