[][src]Crate envmnt

envmnt

Environment variables utility functions.

This library has many helper functions to access/modify/check environment variables.

Example

Get/Set/Remove environment variables

extern crate envmnt;

fn main() {
    if !envmnt::exists("MY_ENV_VAR") {
        envmnt::set("MY_ENV_VAR", "SOME VALUE");
    }

    let mut value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE");
    println!("Env Value: {}", &value);

    value = envmnt::get_or_panic("MY_ENV_VAR");

    let pre_value = envmnt::get_set("MY_ENV_VAR", "SOME NEW VALUE");

    let value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE");
    println!("New Env Value: {}", &value);
    println!("Previous Env Value: {:?}", &pre_value);

    let all_vars = envmnt::vars(); // returned as Vec<(String, String)>

    for (key, value) in all_vars {
        println!("{}: {}", key, value);
    }
}

Get/Set boolean environment variables and other comparisons

extern crate envmnt;

fn main() {
    envmnt::set_bool("FLAG_VAR", true);
    let flag_value = envmnt::is_or("FLAG_VAR", false);
    println!("Bool Flag: {}", &flag_value);

    let pre_value = envmnt::get_set("MY_ENV_VAR", "SOME NEW VALUE");

    envmnt::set("MY_ENV_VAR", "SOME VALUE");
    let same = envmnt::is_equal("MY_ENV_VAR", "SOME VALUE");
    println!("Value Is Same: {}", &same);
}

Bulk Operations

extern crate envmnt;

fn main() {
    let mut found = envmnt::is_any_exists(&vec![
        "ENV_VAR1",
        "ENV_VAR2",
    ]);

    println!("Any Found: {}", &found);

    found = envmnt::is_all_exists(&vec![
        "ENV_VAR1",
        "ENV_VAR2",
    ]);

    println!("All Found: {}", &found);
}

Installation

In order to use this library, just add it as a dependency:

[dependencies]
envmnt = "*"

Contributing

See contributing guide

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.

Functions

exists

Returns true environment variable is defined.

get_or

Returns the environment variable value or if is not defined, the default value will be returned.

get_or_panic

Returns the environment variable value. If the variable is not defined, this function will panic.

get_remove

Removes the provided environment variable and returns its previous value (if any).

get_set

Sets the environment variable value and returns the previous value.

is_all_exists

Returns true if all of environment variables are defined.

is_any_exists

Returns true if any of environment variables is defined.

is_equal

Returns true if the provided environment variable is defined and equals the provided value.

is_or

Returns false if environment variable value if falsy. The value is falsy if it is one of the following:

remove

Removes the provided environment variable.

set

Sets the environment variable value.

set_bool

Sets the environment variable with a true/false value as string.

vars

Returns all environment variables as a vector.