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

42
file.go
View File

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

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. // 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)

View File

@ -1,5 +1,3 @@
//go:build viper_finder
package viper_test package viper_test
import ( 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.resetEncoding()
v.experimentalFinder = features.Finder
return v return v
} }