2024-06-03 07:32:13 +00:00
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WithFinder sets a custom [Finder].
|
|
|
|
func WithFinder(f Finder) Option {
|
|
|
|
return optionFunc(func(v *Viper) {
|
|
|
|
v.finder = f
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:57 +00:00
|
|
|
// ExperimentalFinder tells Viper to use the new Finder interface for finding configuration files.
|
|
|
|
func ExperimentalFinder() Option {
|
|
|
|
return optionFunc(func(v *Viper) {
|
|
|
|
v.experimentalFinder = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-03 07:32:13 +00:00
|
|
|
// Finder looks for files and directories in an [afero.Fs] filesystem.
|
|
|
|
type Finder interface {
|
|
|
|
Find(fsys afero.Fs) ([]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finders combines multiple finders into one.
|
|
|
|
func Finders(finders ...Finder) Finder {
|
2024-06-03 12:48:11 +00:00
|
|
|
return &combinedFinder{finders: finders}
|
2024-06-03 07:32:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 12:48:11 +00:00
|
|
|
// combinedFinder is a Finder that combines multiple finders.
|
|
|
|
type combinedFinder struct {
|
2024-06-03 07:32:13 +00:00
|
|
|
finders []Finder
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find implements the [Finder] interface.
|
2024-06-03 12:48:11 +00:00
|
|
|
func (c *combinedFinder) Find(fsys afero.Fs) ([]string, error) {
|
2024-06-03 07:32:13 +00:00
|
|
|
var results []string
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
for _, finder := range c.finders {
|
|
|
|
r, err := finder.Find(fsys)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
results = append(results, r...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, errors.Join(errs...)
|
|
|
|
}
|