A layered configuration loader with zero-boilerplate configuration management.
salak
try to provide an out-of-box configuration loader for creating new apps, such as cli, web, servers.
salak
defines following default PropertySource
s:
1. Command line arguments using clap
to parsing -P, --propery KEY=VALUE
.
2. System Environment.
3. app.toml(*) in current dir and $HOME dir. Or if you specify APP_CONF_DIR
dir, then only load toml in this dir.
* APP_CONF_NAME
can be specified to replace app
.
${key:default}
means get value of key
, if not exists then return default
.${key}
means get value of key
, if not exists then return PropertyError::NotFound(_)
.a.b.c
is a normal key separated by dot(.
).a.b[0]
, a.b[1]
, a.b[2]
... is a group of keys with arrays.HELLO_WORLD
<=> hello.world
, HELLO__WORLD_HOW
<=> hello_world.how
, hello[1].world
=> HELLO_1_WORLD
<=> hello.1.world
.default
to set default value.#[salak(default="string")]
#[salak(default=1)]
disable_placeholder
to disable placeholder parsing.#[salak(disable_placeholder)]
#[salak(disable_placeholder = true)]
enable_log
, enable log record if enabled.enable_toml
, enable toml support.enable_derive
, enable auto derive [FromEnvironment
] for struts.enable_clap
, enable default command line arguments parsing by clap
.enable_yaml
, enable yaml support.```rust use salak::*;
pub struct DatabaseConfig {
url: String,
#[salak(default = "salak")]
name: String,
#[salak(default = "${database.name}")]
username: String,
password: Option
fn main() {
std::env::setvar("database.url", "localhost:5432");
let env = SalakBuilder::new()
.withdefaultargs(autoreadsysargs_param!()) // This line need enable feature enable_clap
.
.build();
match env.require::
```bash git clone https://github.com/leptonyu/salak.rs.git cd salak.rs cargo run --example salak --features="default enable_clap" -- -h
```