mirror of
https://github.com/spf13/viper
synced 2025-05-15 08:37:19 +00:00
Add ConfigExtension
This forces Viper to look for config files with a given extension which short circuits the supported extensions checks. See https://github.com/spf13/viper/issues/1163
This commit is contained in:
parent
5c0d079c4e
commit
361864a3a8
2 changed files with 59 additions and 9 deletions
42
viper.go
42
viper.go
|
@ -233,6 +233,7 @@ type Viper struct {
|
||||||
configName string
|
configName string
|
||||||
configFile string
|
configFile string
|
||||||
configType string
|
configType string
|
||||||
|
configExtension string
|
||||||
configPermissions os.FileMode
|
configPermissions os.FileMode
|
||||||
envPrefix string
|
envPrefix string
|
||||||
|
|
||||||
|
@ -2059,6 +2060,17 @@ func (v *Viper) SetConfigType(in string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetConfigExtension forces viper to look for a specific file extension
|
||||||
|
// rather than checking all supported types
|
||||||
|
func SetConfigExtension(in string) { v.SetConfigExtension(in) }
|
||||||
|
|
||||||
|
func (v *Viper) SetConfigExtension(in string) {
|
||||||
|
if in != "" {
|
||||||
|
// normalize extensions, for example ".yml" and "yml"
|
||||||
|
v.configExtension = strings.TrimPrefix(in, ".")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SetConfigPermissions sets the permissions for the config file.
|
// SetConfigPermissions sets the permissions for the config file.
|
||||||
func SetConfigPermissions(perm os.FileMode) { v.SetConfigPermissions(perm) }
|
func SetConfigPermissions(perm os.FileMode) { v.SetConfigPermissions(perm) }
|
||||||
|
|
||||||
|
@ -2105,17 +2117,31 @@ func (v *Viper) getConfigFile() (string, error) {
|
||||||
|
|
||||||
func (v *Viper) searchInPath(in string) (filename string) {
|
func (v *Viper) searchInPath(in string) (filename string) {
|
||||||
jww.DEBUG.Println("Searching for config in", in)
|
jww.DEBUG.Println("Searching for config in", in)
|
||||||
for _, ext := range SupportedExts {
|
|
||||||
jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext))
|
basePath := filepath.Join(in, v.configName)
|
||||||
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
|
if v.configType != "" {
|
||||||
jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext))
|
jww.DEBUG.Println("configType specified; checking for", basePath)
|
||||||
return filepath.Join(in, v.configName+"."+ext)
|
if b, _ := exists(v.fs, basePath); b {
|
||||||
|
jww.DEBUG.Println("Found: ", basePath)
|
||||||
|
return basePath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.configType != "" {
|
if v.configExtension != "" {
|
||||||
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
|
path := basePath + "." + v.configExtension
|
||||||
return filepath.Join(in, v.configName)
|
jww.DEBUG.Println("configExtension specified; checking for", path)
|
||||||
|
if b, _ := exists(v.fs, path); b {
|
||||||
|
jww.DEBUG.Println("Found: ", path)
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ext := range SupportedExts {
|
||||||
|
path := basePath + "." + ext
|
||||||
|
jww.DEBUG.Println("Checking for", path)
|
||||||
|
if b, _ := exists(v.fs, path); b {
|
||||||
|
jww.DEBUG.Println("Found: ", path)
|
||||||
|
return path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -342,6 +342,30 @@ func TestSearchInPath(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSearchInPath_extension(t *testing.T) {
|
||||||
|
filename := "config"
|
||||||
|
path := "/tmp"
|
||||||
|
file := filepath.Join(path, filename)
|
||||||
|
SetConfigName(filename)
|
||||||
|
SetConfigExtension("yaml")
|
||||||
|
AddConfigPath(path)
|
||||||
|
_, createErr := v.fs.Create(file+".yaml")
|
||||||
|
assert.NoError(t, createErr)
|
||||||
|
|
||||||
|
// also create a second file which we want to ignore
|
||||||
|
_, createErr2 := v.fs.Create(file+".json")
|
||||||
|
assert.NoError(t, createErr2)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = v.fs.Remove(file+".yaml")
|
||||||
|
_ = v.fs.Remove(file+".json")
|
||||||
|
}()
|
||||||
|
|
||||||
|
filename, err := v.getConfigFile()
|
||||||
|
assert.Equal(t, file+".yaml", filename)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSearchInPath_FilesOnly(t *testing.T) {
|
func TestSearchInPath_FilesOnly(t *testing.T) {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue