mirror of https://github.com/spf13/viper.git
feat: control finder behavior by experimental feature flag
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
2636060878
commit
b206f2075e
42
file.go
42
file.go
|
@ -1,5 +1,3 @@
|
||||||
//go:build !viper_finder
|
|
||||||
|
|
||||||
package viper
|
package viper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,14 +5,19 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/sagikazarmark/locafero"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Search all configPaths for any config file.
|
// ExperimentalFinder tells Viper to use the new Finder interface for finding configuration files.
|
||||||
// Returns the first path that exists (and is a config file).
|
func ExperimentalFinder() Option {
|
||||||
func (v *Viper) findConfigFile() (string, error) {
|
return optionFunc(func(v *Viper) {
|
||||||
if v.finder != nil {
|
v.experimentalFinder = true
|
||||||
results, err := v.finder.Find(v.fs)
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Viper) findConfigFileWithFinder(finder Finder) (string, error) {
|
||||||
|
results, err := finder.Find(v.fs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -24,6 +27,31 @@ func (v *Viper) findConfigFile() (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return results[0], nil
|
return results[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search all configPaths for any config file.
|
||||||
|
// Returns the first path that exists (and is a config file).
|
||||||
|
func (v *Viper) findConfigFile() (string, error) {
|
||||||
|
finder := v.finder
|
||||||
|
|
||||||
|
if finder == nil && v.experimentalFinder {
|
||||||
|
var names []string
|
||||||
|
|
||||||
|
if v.configType != "" {
|
||||||
|
names = locafero.NameWithOptionalExtensions(v.configName, SupportedExts...)
|
||||||
|
} else {
|
||||||
|
names = locafero.NameWithExtensions(v.configName, SupportedExts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
finder = locafero.Finder{
|
||||||
|
Paths: v.configPaths,
|
||||||
|
Names: names,
|
||||||
|
Type: locafero.FileTypeFile,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if finder != nil {
|
||||||
|
return v.findConfigFileWithFinder(finder)
|
||||||
}
|
}
|
||||||
|
|
||||||
v.logger.Info("searching for config in paths", "paths", v.configPaths)
|
v.logger.Info("searching for config in paths", "paths", v.configPaths)
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
//go:build viper_finder
|
|
||||||
|
|
||||||
package viper
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/sagikazarmark/locafero"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Search all configPaths for any config file.
|
|
||||||
// Returns the first path that exists (and is a config file).
|
|
||||||
func (v *Viper) findConfigFile() (string, error) {
|
|
||||||
finder := v.finder
|
|
||||||
|
|
||||||
if finder == nil {
|
|
||||||
var names []string
|
|
||||||
|
|
||||||
if v.configType != "" {
|
|
||||||
names = locafero.NameWithOptionalExtensions(v.configName, SupportedExts...)
|
|
||||||
} else {
|
|
||||||
names = locafero.NameWithExtensions(v.configName, SupportedExts...)
|
|
||||||
}
|
|
||||||
|
|
||||||
finder = locafero.Finder{
|
|
||||||
Paths: v.configPaths,
|
|
||||||
Names: names,
|
|
||||||
Type: locafero.FileTypeFile,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
results, err := finder.Find(v.fs)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(results) == 0 {
|
|
||||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
|
||||||
}
|
|
||||||
|
|
||||||
return results[0], nil
|
|
||||||
}
|
|
|
@ -13,13 +13,6 @@ func WithFinder(f Finder) Option {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExperimentalFinder tells Viper to use the new Finder interface for finding configuration files.
|
|
||||||
func ExperimentalFinder() Option {
|
|
||||||
return optionFunc(func(v *Viper) {
|
|
||||||
v.experimentalFinder = true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finder looks for files and directories in an [afero.Fs] filesystem.
|
// Finder looks for files and directories in an [afero.Fs] filesystem.
|
||||||
type Finder interface {
|
type Finder interface {
|
||||||
Find(fsys afero.Fs) ([]string, error)
|
Find(fsys afero.Fs) ([]string, error)
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
//go:build viper_finder
|
|
||||||
|
|
||||||
package viper_test
|
package viper_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
//go:build viper_finder
|
||||||
|
|
||||||
|
package features
|
||||||
|
|
||||||
|
const Finder = true
|
|
@ -0,0 +1,5 @@
|
||||||
|
//go:build !viper_finder
|
||||||
|
|
||||||
|
package features
|
||||||
|
|
||||||
|
const Finder = false
|
Loading…
Reference in New Issue