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
|
package viper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,23 +5,53 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/sagikazarmark/locafero"
|
||||||
"github.com/spf13/afero"
|
"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.
|
// Search all configPaths for any config file.
|
||||||
// Returns the first path that exists (and is a config file).
|
// Returns the first path that exists (and is a config file).
|
||||||
func (v *Viper) findConfigFile() (string, error) {
|
func (v *Viper) findConfigFile() (string, error) {
|
||||||
if v.finder != nil {
|
finder := v.finder
|
||||||
results, err := v.finder.Find(v.fs)
|
|
||||||
if err != nil {
|
if finder == nil && v.experimentalFinder {
|
||||||
return "", err
|
var names []string
|
||||||
|
|
||||||
|
if v.configType != "" {
|
||||||
|
names = locafero.NameWithOptionalExtensions(v.configName, SupportedExts...)
|
||||||
|
} else {
|
||||||
|
names = locafero.NameWithExtensions(v.configName, SupportedExts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(results) == 0 {
|
finder = locafero.Finder{
|
||||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
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)
|
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 (
|
||||||
|
|
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.resetEncoding()
|
||||||
|
|
||||||
|
v.experimentalFinder = features.Finder
|
||||||
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue