ilyakaznacheev / cleanenv

✨Clean and minimalistic environment configuration reader for Golang
MIT License
1.61k stars 111 forks source link

Allow reading from `fs.FS` #135

Open Aquarthur opened 8 months ago

Aquarthur commented 8 months ago

Provide an interface for the user to pass in an fs.FS alongside a filename.

This will add some flexibility about how the file is loaded, and makes it easier for users to write tests to verify configs via testing/fstest.

Something like:

type ConfigDatabase struct {
    Port     string `yaml:"port" env:"PORT" env-default:"5432"`
    Host     string `yaml:"host" env:"HOST" env-default:"localhost"`
    Name     string `yaml:"name" env:"NAME" env-default:"postgres"`
    User     string `yaml:"user" env:"USER" env-default:"user"`
    Password string `yaml:"password" env:"PASSWORD"`
}

var cfg ConfigDatabase

fsys := os.DirFS("/example/config/folder")
err := cleanenv.ReadConfigFS(fsys, "config.yml", &cfg)
if err != nil {
    ...
}