zero-cli 1.0.1

A command line tool for Zero Secrets Manager
use crate::common::print_formatted_error::print_formatted_error;

/// Retrieves the value of an environment variable by its name and returns it.
///
/// This function attempts to retrieve the value of an environment variable specified by
/// `env_var_name`. If the variable exists and has a value, that value is returned as a `String`.
/// If the variable is not set or there is an error while retrieving it, an error message is
/// printed to the standard error (stderr) stream, and the program exits with a status code of 1.
///
/// # Arguments
///
/// * `env_var_name` - The name of the environment variable to retrieve.
///
/// # Returns
///
/// The value of the specified environment variable as a `String`.
///
/// # Panics
///
/// This function will panic and terminate the program if the specified environment variable is
/// not set or if there is an error while retrieving it.
///
/// # Examples
///
/// ```
/// let my_var = env_var("MY_VARIABLE");
/// println!("Value of MY_VARIABLE: {}", my_var);
/// ```
///
pub fn env_var(env_var_name: &str) -> String {
    match std::env::var(env_var_name) {
        Ok(value) => return value,
        Err(_) => {
            let error_message = format!("{} must be set.", env_var_name);
            print_formatted_error(&error_message);
            std::process::exit(1);
        }
    };
}