envmnt

crates.io Build Status Build status codecov
license Libraries.io for GitHub Documentation downloads
Built with cargo-make

Environment variables utility functions.

Overview

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

Usage

Simply include the library and invoke the various utility functions.

Get/Set/Remove environment variables

```rust extern crate envmnt;

fn main() { if !envmnt::exists("MYENVVAR") { envmnt::set("MYENVVAR", "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");
println!("Env Value: {}", &value);

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 var_was_set = envmnt::set_optional("MY_ENV_VAR", &Some("OPTIONAL VALUE"));
println!("Env Was Modified: {}", var_was_set);

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

```rust extern crate envmnt;

fn main() { envmnt::setbool("FLAGVAR", true); let mut flagvalue = envmnt::isor("FLAGVAR", false); println!("Bool Flag: {}", &flagvalue);

flag_value = envmnt::is("FLAG_VAR");
assert!(flag_value);

envmnt::set_bool("FLAG_VAR", true);
assert!(envmnt::is_equal("FLAG_VAR", "true"));

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

} ```

Bulk Operations

```rust extern crate envmnt; extern crate indexmap;

use indexmap::IndexMap;

fn main() { let mut env: IndexMap = IndexMap::new(); env.insert("ENVVAR1".tostring(), "MY VALUE".tostring()); env.insert("ENVVAR2".tostring(), "MY VALUE2".tostring());

envmnt::set_all(&env);

let value = envmnt::get_or_panic("ENV_VAR1");
println!("Value Is: {}", &value);

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);

env = IndexMap::new();
env.insert("ENV_VAR1".to_string(), "MY VALUE".to_string());
env.insert("ENV_VAR2".to_string(), "MY VALUE2".to_string());

let eval_env = |value: String| {
    let mut buffer = String::from("VALUE-");
    buffer.push_str(&value);
    buffer
};

envmnt::evaluate_and_set_all(&env, eval_env);

let value = envmnt::get_or_panic("ENV_VAR1");
println!("Value Is: {}", &value);

} ```

File Operations

```rust extern crate envmnt;

fn main() { let mut output = envmnt::loadfile("./src/test/var.env"); assert!(output.isok());

let eval_env = |value: String| {
    let mut buffer = String::from("PREFIX-");
    buffer.push_str(&value);
    buffer
};

output = envmnt::evaluate_and_load_file("./src/test/var.env", eval_env);
assert!(output.is_ok());

} ```

Installation

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

ini [dependencies] envmnt = "*"

API Documentation

See full docs at: API Docs

Contributing

See contributing guide

Release History

See Changelog

License

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