mirror of
https://github.com/spf13/viper
synced 2024-12-22 03:27:03 +00:00
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
7 changed files with 49 additions and 60 deletions
46
file.go
46
file.go
|
@ -1,5 +1,3 @@
|
|||
//go:build !viper_finder
|
||||
|
||||
package viper
|
||||
|
||||
import (
|
||||
|
@ -7,23 +5,53 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/sagikazarmark/locafero"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
// ExperimentalFinder tells Viper to use the new Finder interface for finding configuration files.
|
||||
func ExperimentalFinder() Option {
|
||||
return optionFunc(func(v *Viper) {
|
||||
v.experimentalFinder = true
|
||||
})
|
||||
}
|
||||
|
||||
func (v *Viper) findConfigFileWithFinder(finder Finder) (string, error) {
|
||||
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
|
||||
}
|
||||
|
||||
// Search all configPaths for any config file.
|
||||
// Returns the first path that exists (and is a config file).
|
||||
func (v *Viper) findConfigFile() (string, error) {
|
||||
if v.finder != nil {
|
||||
results, err := v.finder.Find(v.fs)
|
||||
if err != nil {
|
||||
return "", err
|
||||
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...)
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
||||
finder = locafero.Finder{
|
||||
Paths: v.configPaths,
|
||||
Names: names,
|
||||
Type: locafero.FileTypeFile,
|
||||
}
|
||||
}
|
||||
|
||||
return results[0], nil
|
||||
if finder != nil {
|
||||
return v.findConfigFileWithFinder(finder)
|
||||
}
|
||||
|
||||
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.
|
||||
type Finder interface {
|
||||
Find(fsys afero.Fs) ([]string, error)
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
//go:build viper_finder
|
||||
|
||||
package viper_test
|
||||
|
||||
import (
|
||||
|
|
5
internal/features/finder.go
Normal file
5
internal/features/finder.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
//go:build viper_finder
|
||||
|
||||
package features
|
||||
|
||||
const Finder = true
|
5
internal/features/finder_default.go
Normal file
5
internal/features/finder_default.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
//go:build !viper_finder
|
||||
|
||||
package features
|
||||
|
||||
const Finder = false
|
2
viper.go
2
viper.go
|
@ -218,6 +218,8 @@ func New() *Viper {
|
|||
|
||||
v.resetEncoding()
|
||||
|
||||
v.experimentalFinder = features.Finder
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue