From b206f2075e2dea77a2aecd70eab2ca4704a9b0bb Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Tue, 4 Jun 2024 15:26:20 +0200 Subject: [PATCH] feat: control finder behavior by experimental feature flag Signed-off-by: Mark Sagi-Kazar --- file.go | 46 +++++++++++++++++++++++------ file_finder.go | 42 -------------------------- finder.go | 7 ----- finder_example_test.go | 2 -- internal/features/finder.go | 5 ++++ internal/features/finder_default.go | 5 ++++ viper.go | 2 ++ 7 files changed, 49 insertions(+), 60 deletions(-) delete mode 100644 file_finder.go create mode 100644 internal/features/finder.go create mode 100644 internal/features/finder_default.go diff --git a/file.go b/file.go index 96991ae..5f8a22c 100644 --- a/file.go +++ b/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) diff --git a/file_finder.go b/file_finder.go deleted file mode 100644 index c873621..0000000 --- a/file_finder.go +++ /dev/null @@ -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 -} diff --git a/finder.go b/finder.go index 3944370..ab4b595 100644 --- a/finder.go +++ b/finder.go @@ -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) diff --git a/finder_example_test.go b/finder_example_test.go index 76c5f57..8f09452 100644 --- a/finder_example_test.go +++ b/finder_example_test.go @@ -1,5 +1,3 @@ -//go:build viper_finder - package viper_test import ( diff --git a/internal/features/finder.go b/internal/features/finder.go new file mode 100644 index 0000000..983ea3a --- /dev/null +++ b/internal/features/finder.go @@ -0,0 +1,5 @@ +//go:build viper_finder + +package features + +const Finder = true diff --git a/internal/features/finder_default.go b/internal/features/finder_default.go new file mode 100644 index 0000000..89bcb06 --- /dev/null +++ b/internal/features/finder_default.go @@ -0,0 +1,5 @@ +//go:build !viper_finder + +package features + +const Finder = false diff --git a/viper.go b/viper.go index 81a62b9..3f9d164 100644 --- a/viper.go +++ b/viper.go @@ -218,6 +218,8 @@ func New() *Viper { v.resetEncoding() + v.experimentalFinder = features.Finder + return v }