#command-line #configuration #environment #configuration-management #env-var #macro

config-manager

Crate to build config from environment, command line and files

3 unstable releases

0.2.0 Apr 3, 2023
0.1.1 Mar 9, 2023
0.1.0 Dec 29, 2022

#753 in Configuration

Download history 60/week @ 2024-07-19 89/week @ 2024-07-26 84/week @ 2024-08-02 94/week @ 2024-08-09 83/week @ 2024-08-16 159/week @ 2024-08-23 99/week @ 2024-08-30 104/week @ 2024-09-06 128/week @ 2024-09-13 137/week @ 2024-09-20 194/week @ 2024-09-27 83/week @ 2024-10-04 79/week @ 2024-10-11 121/week @ 2024-10-18 53/week @ 2024-10-25 51/week @ 2024-11-01

315 downloads per month

MIT license

45KB
475 lines

Crate to build config from environment, command line and files

Motivation

Non-runtime data generally comes to a project from command line, environment and configuration files.
Sometimes it comes from each of the sources simultaneously, so all of them must be handled.
None of the popular crates (including clap and config) can't handle all 3 together, so this crate has been created to solve this problem.

Basis

The Core of the crate is an attribute-macro #[config].
Annotate structure with this macro and a field of it with the source attribute, so the field will be searched in one of the provided sources. The sources can be provided by using the following nested source attributes:

  1. clap: command line argument
  2. env: environment variable
  3. config: configuration file key
  4. default: default value

Example

use config_manager::config;

#[config]
struct ApplicationConfig {
    #[source(clap(long, short = 'p'), env = "APP_MODEL_PATH", config)]
    model_path: String,
    #[source(env, config, default = 0)]
    prediction_delay: u64,
}

In the example above, to set the value of the model_path field, a user may provide:

  • command line argument --model_path
  • environment variable named model_path
  • configuration file containing field model_path

If the value is found in multiple provided sources, the value will be assigned according to the provided order (the order for the model_path field is clap -> env -> config and env -> config -> default for the prediction_delay).
If none of them (including the default value) isn't found, the program returns error MissingArgument.

Note: the default value is always assigned last.

Attributes documentation

For further understanding of project syntax and features, it is recommended to visit Cookbook.

Complex example

use std::collections::HashMap;

use config_manager::{config, ConfigInit};

const SUFFIX: &str = "_env";

#[derive(Debug)]
#[config(
    clap(version, author),
    env_prefix = "demo",
    file(
        format = "toml",
        clap(long = "config", short = 'c', help = "path to configuration file"),
        env = "demo_config",
        default = "./config.toml"
    )
)]
struct MethodConfig {
    #[source(clap(long, short))]
    a: i32,
    #[source(
        env(init_from = "&format!(\"b{}\", SUFFIX)"),
        default = "\"abc\".to_string()"
    )]
    b: String,
    #[source(config = "bpm")]
    c: i32,
    #[source(default = "HashMap::new()")]
    d: HashMap<i32, String>,
}

fn main() {
    dbg!(MethodConfig::parse().unwrap());
}

Run

cargo run --package examples --bin demo -- --config="examples/config.toml" --a=5

Result must be:

[examples/src/demo.rs:34] &*CFG = MethodConfig {
    a: 5,
    b: "qwerty",
    c: 165,
    d: {},
}

Dependencies

~3.5–4.5MB
~89K SLoC