From dd6204a5a061a58333a02cba84b6586e667a0eb0 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Mon, 3 Jun 2024 15:19:44 +0200 Subject: [PATCH] test: add examples for finder Signed-off-by: Mark Sagi-Kazar --- finder_example_test.go | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 finder_example_test.go diff --git a/finder_example_test.go b/finder_example_test.go new file mode 100644 index 0000000..76c5f57 --- /dev/null +++ b/finder_example_test.go @@ -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 +}