knadh / koanf

Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.
MIT License
2.71k stars 150 forks source link

feature: search file #293

Closed bersace closed 3 months ago

bersace commented 5 months ago

Hi,

Thanks for koanf, it's awesome.

I think it may be useful if file module provides a helper to find a file amongst candidate. e.g.

path := file.Find(
    "ldaprc",
    ".ldaprc",
    "ldap.conf",
)

And also finding a file in parent directory:

path := file.FindParent("docker-compose.yml")

What do you think of this need ?

Regards, Étienne

knadh commented 5 months ago

Thanks @bersace. Unsure if we should add these as util functions as they only take a couple of lines of code to implement. In the same vein, there may be many other niche util functions that'll then have to be considered. FindRecursively() etc.

Implementing a Find functionality can be as simple as this for instance.

    for _, f := range []string{"ldaprc", ".ldaprc", "ldap.conf"} {
        if _, err := os.Stat(filename); err == nil {
            k.Load(file.Provider(f), toml.Parser())
            break
        }
    }