mehcode / config-rs

⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).
Apache License 2.0
2.43k stars 206 forks source link

Searching for configuration file in parent directories #386

Open Teajey opened 1 year ago

Teajey commented 1 year ago

I use this pattern to load my config file

      Config::builder()
            .add_source(File::with_name(".myconfig"))
            .build()?
            .try_deserialize::<MyConfig>()

By default this only picks up a config file in the current directory, but I'm hoping to be able to run my program anywhere within a project that has ".myconfig" in the root directory. Is there a preferred way to do this?

My first thought is to successively ConfigBuilder::build in each parent directory up to / every time ConfigError::NotFound occurs; but that seems pretty naive

matthiasbeyer commented 1 year ago

I am afraid you have to build that logic yourself. config-rs does not support searching for configuration files, only loading and accessing them!

You could have a look at the xdg or directories crate or maybe at the walkdir crate!