From 4e595cec77728ff00275ffedf3bd7ae36b5e4438 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Tue, 21 Sep 2021 11:08:46 +0200 Subject: [PATCH] feat: use io/fs for searching files on Go 1.16+ Signed-off-by: Mark Sagi-Kazar --- util.go | 13 ----------- viper.go | 33 ---------------------------- viper_go1_15.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ viper_go1_16.go | 32 +++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 46 deletions(-) create mode 100644 viper_go1_15.go create mode 100644 viper_go1_16.go diff --git a/util.go b/util.go index 09d051a..742f997 100644 --- a/util.go +++ b/util.go @@ -18,7 +18,6 @@ import ( "strings" "unicode" - "github.com/spf13/afero" "github.com/spf13/cast" jww "github.com/spf13/jwalterweatherman" ) @@ -111,18 +110,6 @@ func absPathify(inPath string) string { return "" } -// Check if file Exists -func exists(fs afero.Fs, path string) (bool, error) { - stat, err := fs.Stat(path) - if err == nil { - return !stat.IsDir(), nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err -} - func stringInSlice(a string, list []string) bool { for _, b := range list { if b == a { diff --git a/viper.go b/viper.go index 9e2e353..6c456a9 100644 --- a/viper.go +++ b/viper.go @@ -2108,39 +2108,6 @@ func (v *Viper) getConfigFile() (string, error) { return v.configFile, nil } -func (v *Viper) searchInPath(in string) (filename string) { - jww.DEBUG.Println("Searching for config in ", in) - for _, ext := range SupportedExts { - jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext)) - if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b { - jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext)) - return filepath.Join(in, v.configName+"."+ext) - } - } - - if v.configType != "" { - if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b { - return filepath.Join(in, v.configName) - } - } - - return "" -} - -// Search all configPaths for any config file. -// Returns the first path that exists (and is a config file). -func (v *Viper) findConfigFile() (string, error) { - jww.INFO.Println("Searching for config in ", v.configPaths) - - for _, cp := range v.configPaths { - file := v.searchInPath(cp) - if file != "" { - return file, nil - } - } - return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} -} - // Debug prints all configuration registries for debugging // purposes. func Debug() { v.Debug() } diff --git a/viper_go1_15.go b/viper_go1_15.go new file mode 100644 index 0000000..487e0c8 --- /dev/null +++ b/viper_go1_15.go @@ -0,0 +1,58 @@ +//go:build !go1.16 +// +build !go1.16 + +package viper + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/spf13/afero" + jww "github.com/spf13/jwalterweatherman" +) + +// Search all configPaths for any config file. +// Returns the first path that exists (and is a config file). +func (v *Viper) findConfigFile() (string, error) { + jww.INFO.Println("Searching for config in ", v.configPaths) + + for _, cp := range v.configPaths { + file := v.searchInPath(cp) + if file != "" { + return file, nil + } + } + return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} +} + +func (v *Viper) searchInPath(in string) (filename string) { + jww.DEBUG.Println("Searching for config in ", in) + for _, ext := range SupportedExts { + jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext)) + if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b { + jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext)) + return filepath.Join(in, v.configName+"."+ext) + } + } + + if v.configType != "" { + if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b { + return filepath.Join(in, v.configName) + } + } + + return "" +} + +// Check if file Exists +func exists(fs afero.Fs, path string) (bool, error) { + stat, err := fs.Stat(path) + if err == nil { + return !stat.IsDir(), nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} diff --git a/viper_go1_16.go b/viper_go1_16.go new file mode 100644 index 0000000..eb313fa --- /dev/null +++ b/viper_go1_16.go @@ -0,0 +1,32 @@ +//go:build go1.16 +// +build go1.16 + +package viper + +import ( + "fmt" + + "github.com/spf13/afero" +) + +// 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 := finder{ + paths: v.configPaths, + fileNames: []string{v.configName}, + extensions: SupportedExts, + withoutExtension: v.configType != "", + } + + file, err := finder.Find(afero.NewIOFS(v.fs)) + if err != nil { + return "", err + } + + if file == "" { + return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} + } + + return file, nil +}