config/
config.rs

1// This is free and unencumbered software released into the public domain.
2
3use asimov_module::ModuleManifest;
4use std::error::Error;
5
6const YAML: &str = r#"
7name: example
8label: Example
9summary: Example module
10links:
11    - https://github.com/asimov-platform/asimov.rs/tree/master/lib/asimov-module/examples/config.rs
12
13config:
14  variables:
15    # name is the only mandatory field
16
17    - name: api_key
18      description: "api key to authorize requests"
19
20    - name: other_var
21      default_value: "foobar"
22
23    - name: var_from_env
24      environment: VAR_FROM_ENV
25"#;
26
27fn main() -> Result<(), Box<dyn Error>> {
28    let manifest: ModuleManifest = serde_yml::from_str(YAML)?;
29
30    let api_key = manifest.variable("api_key", None).unwrap_or_default();
31    if api_key.is_empty() {
32        println!("api_key: `{api_key}`");
33        println!(
34            "(consider `mkdir -p ~/.asimov/configs/default/example/ && echo -n \"<api-key-value>\" >> ~/.asimov/configs/default/example/api_key` or for a non-example module `asimov module config <example> api_key <api-key-value>`)"
35        );
36    } else {
37        println!("api_key: `{api_key}`");
38    }
39
40    let other_var = manifest.variable("other_var", None).unwrap_or_default();
41    println!("\nother_var: `{other_var}`");
42
43    let env_var = manifest.variable("var_from_env", None).unwrap_or_default();
44    println!("\nvar_from_env: `{env_var}`");
45
46    println!("setting env var `VAR_FROM_ENV` manually...");
47    unsafe { std::env::set_var("VAR_FROM_ENV", "hello!") }
48    let env_var = manifest.variable("var_from_env", None).unwrap_or_default();
49    println!("var_from_env: `{env_var}`");
50
51    // alternative way:
52    // let vars = manifest.read_variables(None)?;
53    // let api_key = vars.get("api_key");
54    // println!("api_key: `{api_key:?}`");
55
56    Ok(())
57}