sagiegurari / cargo-make
1
//! # trim_func
2
//!
3
//! Takes an environment variable name and returns its value trimmed.
4
//! The value will be removed if empty.
5
//!
6

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

11
use envmnt;
12

13 8
pub(crate) fn invoke(function_args: &Vec<String>) -> Vec<String> {
14 8
    if function_args.len() > 2 {
15 8
        error!("trim expects up to 2 arguments (environment variable name and optionally start/end trim flag)");
16
    }
17

18 8
    let env_key = function_args[0].clone();
19

20 8
    let value = envmnt::get_or(&env_key, "");
21

22 8
    let trimmed_value = if function_args.len() == 1 {
23 8
        value.trim().to_string()
24
    } else {
25 8
        let trim_type = function_args[1].clone();
26

27 8
        match trim_type.as_ref() {
28 8
            "start" => value.trim_start().to_string(),
29 8
            "end" => value.trim_end().to_string(),
30
            _ => {
31 8
                error!("Invalid trim type provided, only start or end are supported.");
32 0
                panic!("Invalid trim type provided, only start or end are supported.");
33
            }
34
        }
35 8
    };
36

37 8
    if trimmed_value.len() > 0 {
38 8
        vec![trimmed_value]
39
    } else {
40 8
        vec![]
41
    }
42 8
}

Read our documentation on viewing source code .

Loading