test: add examples for finder

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-06-03 15:19:44 +02:00 committed by Márk Sági-Kazár
parent c9994ee0c6
commit dd6204a5a0
1 changed files with 75 additions and 0 deletions

75
finder_example_test.go Normal file
View File

@ -0,0 +1,75 @@
//go:build viper_finder
package viper_test
import (
"fmt"
"github.com/sagikazarmark/locafero"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
func ExampleFinder() {
fs := afero.NewMemMapFs()
fs.Mkdir("/home/user", 0o777)
f, _ := fs.Create("/home/user/myapp.yaml")
f.WriteString("foo: bar")
f.Close()
// HCL will have a "lower" priority in the search order
fs.Create("/home/user/myapp.hcl")
finder := locafero.Finder{
Paths: []string{"/home/user"},
Names: locafero.NameWithExtensions("myapp", viper.SupportedExts...),
Type: locafero.FileTypeFile, // This is important!
}
v := viper.NewWithOptions(viper.WithFinder(finder))
v.SetFs(fs)
v.ReadInConfig()
fmt.Println(v.GetString("foo"))
// Output:
// bar
}
func ExampleFinders() {
fs := afero.NewMemMapFs()
fs.Mkdir("/home/user", 0o777)
f, _ := fs.Create("/home/user/myapp.yaml")
f.WriteString("foo: bar")
f.Close()
fs.Mkdir("/etc/myapp", 0o777)
fs.Create("/etc/myapp/config.yaml")
// Combine multiple finders to search for files in multiple locations with different criteria
finder := viper.Finders(
locafero.Finder{
Paths: []string{"/home/user"},
Names: locafero.NameWithExtensions("myapp", viper.SupportedExts...),
Type: locafero.FileTypeFile, // This is important!
},
locafero.Finder{
Paths: []string{"/etc/myapp"},
Names: []string{"config.yaml"}, // Only accept YAML files in the system config directory
Type: locafero.FileTypeFile, // This is important!
},
)
v := viper.NewWithOptions(viper.WithFinder(finder))
v.SetFs(fs)
v.ReadInConfig()
fmt.Println(v.GetString("foo"))
// Output:
// bar
}