sagiegurari / cargo-make
1
//! # trim_func
2
//!
3
//! Takes an environment variable name and returns its mapped value.
4
//! In case no mapped value if found, the default value will be returned if provided as last argument or if no default value is provided, the original value is returned.
5
//! The value will be removed if empty.
6
//!
7

8
#[cfg(test)]
9
#[path = "./decode_func_test.rs"]
10
mod decode_func_test;
11

12
use crate::environment;
13
use envmnt;
14

15 8
pub(crate) fn invoke(function_args: &Vec<String>) -> Vec<String> {
16 8
    if function_args.len() == 0 {
17 8
        error!("decode expects at least one argument.");
18
    }
19

20 8
    let env_key = function_args[0].clone();
21 8
    let env_value = envmnt::get_or(&env_key, "");
22

23 8
    let mut mapped_value = None;
24 8
    let mut found = false;
25 8
    let mut skip = true;
26 8
    for item in function_args.iter() {
27 8
        if skip {
28 8
            skip = false;
29 8
        } else if found {
30 8
            mapped_value = Some(item.to_string());
31 6
            break;
32 8
        } else if item.to_string() == env_value {
33 8
            found = true;
34
        } else {
35 8
            skip = true;
36
        }
37
    }
38

39
    // if no mapped value found and default value provided
40 8
    let mut output_value = match mapped_value {
41 8
        Some(value) => value.clone(),
42
        None => {
43 8
            if function_args.len() % 2 == 0 && function_args.len() > 1 {
44 8
                function_args[function_args.len() - 1].to_string()
45
            } else {
46 8
                env_value.clone()
47
            }
48
        }
49
    };
50

51 8
    output_value = environment::expand_value(&output_value);
52

53 8
    if output_value.len() > 0 {
54 8
        vec![output_value]
55
    } else {
56 8
        vec![]
57
    }
58 8
}

Read our documentation on viewing source code .

Loading