feat: control finder behavior by experimental feature flag

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-06-04 15:26:20 +02:00 committed by Márk Sági-Kazár
parent 2636060878
commit b206f2075e
7 changed files with 49 additions and 60 deletions

46
file.go
View File

@ -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)

View File

@ -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
}

View File

@ -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)

View File

@ -1,5 +1,3 @@
//go:build viper_finder
package viper_test
import (

View File

@ -0,0 +1,5 @@
//go:build viper_finder
package features
const Finder = true

View File

@ -0,0 +1,5 @@
//go:build !viper_finder
package features
const Finder = false

View File

@ -218,6 +218,8 @@ func New() *Viper {
v.resetEncoding()
v.experimentalFinder = features.Finder
return v
}