1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
//! This crate will one day parse environment variables by defining a struct like
//! [StructOpt](https://crates.io/crates/structopt) does for command line arguments.
//!
//! For now it just provides the convenience method `var_or_exit`.

use std::{env, process};

pub fn var_or_exit(var: &str, default: Option<String>) -> String {
    match (env::var(var), default) {
        (Ok(value), _) => value,
        (Err(_), Some(default)) => default,
        (Err(_), None) => {
            eprintln!("environment variable '{}' not set", var);
            process::exit(64);
        }
    }
}