()
| 38 | } |
| 39 | |
| 40 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…