MCPcopy
hub / github.com/spf13/viper / ExampleFinders

Function ExampleFinders

finder_example_test.go:40–73  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

38}
39
40func ExampleFinders() {
41 fs := afero.NewMemMapFs()
42
43 fs.Mkdir("/home/user", 0o777)
44 f, _ := fs.Create("/home/user/myapp.yaml")
45 f.WriteString("foo: bar")
46 f.Close()
47
48 fs.Mkdir("/etc/myapp", 0o777)
49 fs.Create("/etc/myapp/config.yaml")
50
51 // Combine multiple finders to search for files in multiple locations with different criteria
52 finder := viper.Finders(
53 locafero.Finder{
54 Paths: []string{"/home/user"},
55 Names: locafero.NameWithExtensions("myapp", viper.SupportedExts...),
56 Type: locafero.FileTypeFile, // This is important!
57 },
58 locafero.Finder{
59 Paths: []string{"/etc/myapp"},
60 Names: []string{"config.yaml"}, // Only accept YAML files in the system config directory
61 Type: locafero.FileTypeFile, // This is important!
62 },
63 )
64
65 v := viper.NewWithOptions(viper.WithFinder(finder))
66 v.SetFs(fs)
67 v.ReadInConfig()
68
69 fmt.Println(v.GetString("foo"))
70
71 // Output:
72 // bar
73}

Callers

nothing calls this directly

Calls 6

FindersFunction · 0.92
NewWithOptionsFunction · 0.92
WithFinderFunction · 0.92
SetFsMethod · 0.80
ReadInConfigMethod · 0.80
GetStringMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…