Profpatsch/users/Profpatsch/execline/importas-if.rs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
extern crate el_exec;
extern crate el_substitute;
use std::ffi::CString;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
fn main() {
    let args = std::env::args_os()
        .into_iter()
        .map(|arg| CString::new(arg.as_bytes()).unwrap())
        .collect::<Vec<CString>>();
    assert!(args.len() >= 3, "at least two arguments required");
    let import = args[2].as_bytes();
    let as_var = &args[1];
    match std::env::var_os(OsStr::from_bytes(import)) {
        // If envar doesn't exist, exit 1.
        // This makes it possible to react outside
        None => std::process::exit(1),
        // If it does, continue
        Some(val) => el_exec::xmexec0(&el_substitute::simple_substitute_argv(
            &vec![el_substitute::Subst {
                var: &as_var,
                value: &CString::new(val.as_bytes()).unwrap(),
            }],
            &args[3..],
        )),
    }
}