sagiegurari / envmnt Goto Github PK
View Code? Open in Web Editor NEWEnvironment variables utility functions.
License: Apache License 2.0
Environment variables utility functions.
License: Apache License 2.0
enable to get an eval function as an optional parameter to enable os specific expansion
support operations on vec of env vars.
.expand()
doesn't work if variables wrap :
.
Run code below.
use envmnt::{ExpandOptions, ExpansionType};
use std::env;
#[test]
fn not_expanded_with_var_colon_var() {
env::set_var("ASDF", "asdf");
env::set_var("QWER", "qwer");
let options = ExpandOptions {
expansion_type: Some(ExpansionType::Unix),
default_to_empty: true,
};
let expanded = envmnt::expand("$ASDF:$QWER", Some(options));
assert_eq!(expanded, "asdf:qwer");
}
$ cargo test
running 1 test
test test::not_expanded_with_var_colon_var ... FAILED
failures:
---- test::not_expanded_with_var_colon_var stdout ----
thread 'test::not_expanded_with_var_colon_var' panicked at 'assertion failed: `(left == right)`
left: `""`,
right: `"asdf:qwer"`', src/lib.rs:13:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Thank you for your hard work!!!
Have EnvmntError
be more docile when matching or chaining against it from downstream. For chaining implementing std::error::Error
is necessary.
Move ErrorKind member into EnvmntError, making the latter an enumeration. Implement std::error::Error
for it.
Reduction of code for anything depending on matching against EnvmntError.kind
.
get the env var value or panic if value doesn't exist.
A function to access and parse into any type T or error out.
A function envmnt::get<T>() -> Result<T, ?> where T: FromStr<T>
. This way a environment variable can be gotten and parsed by its return type, or gracefully error out.
pub fn get<T, E>(key: &'static str) -> EnvmtResult<T>
where T: std::str::FromStr + std::str::FromStr<Err=E>,
E: std::fmt::Display
{
let var = std::env::var(key)
.map_err(|e|
EnvmtError::Missing(format!("Failed to load environment variable: '{}' ({})", name, e))
)?;
let val = var.parse::<T>()
.map_err(|e|
EnvmtError::InvalidType(
format!("Failed to parse environment variable: '{}' to type '{}' ({})", name, stringify!(T), e)
)
)?;
Ok(val)
}
For instance numeric.rs can be simplified by this, since this generic specializes amongst other things into all its variants.
I'm currently carrying this boiler plate around project after project, so I figured it might be useful to upstream it into an existing env utilities crate like yours. Would you accept a pull request for something like this?
enable to check if the provided env contains the given value
Features Description
Allow for manual env expansion
Describe the solution you'd like
A new function that would get the env value and expansion type enum (OS default, windows, unix or unix subset) and return the updated value after expansion.
Code Sample
let expanded_value = envmnt::expand("${MY_ENV}_123", ExpansionType::OS);
Features Description
The exposure I have to this crate leads me to believe that either the default syntax isn't documented, or it doesn't support default values.
Describe the solution you'd like
Today, I can expand things like ${MY_VAR}
, but I would like to be able to supply defaults, e.g. ${MY_VAR:default_value}
so that if the environment variable isn't present, it uses a default value that's supplied within the text being expanded.
removes all provided env vars.
In the version 0.10.0 if I use ${ADDRESS:http://127.0.0.1:1234}
and the env variable is not defined it was returning http://127.0.0.1:1234
in the version 0.10.2 this doesn't work and it returns the same ${ADDRESS:http://127.0.0.1:1234}
.expand()
not working with /
.
Run code below.
#[test]
fn not_expanded_with_slash() {
env::set_var("EDITOR", "nvim");
let options = ExpandOptions {
expansion_type: Some(ExpansionType::Unix),
default_to_empty: true,
};
let expanded = envmnt::expand("$EDITOR/asdf", Some(options));
assert_eq!("nvim/asdf", expanded);
}
$ cargo test
running 1 test
test not_expanded_with_slash ... FAILED
failures:
---- not_expanded_with_slash stdout ----
thread 'not_expanded_with_slash' panicked at 'assertion failed: `(left == right)`
left: `"nvim/asdf"`,
right: `""`', src/main.rs:15:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
similar to std::env
instead of checking var after var for existing value, have a get_any(vec) fn
Describe the bug
In v0.8.2 when an environment variable value contains double quotes those are not evaluated by the library, instead the whole value is returned including the double quotes wrapper.
To Reproduce
Just create an environment variable and wrap its value with double quotes like LOG_LEVEL="info"
and just run the code example below.
Code Sample
#[test]
fn env_file_quoted() {
let output = envmnt::load_file("load.env");
assert!(output.is_ok());
// Panic: `LOG_LEVEL` returns ""info"" and not "info"
assert_eq!(envmnt::get_or_panic("LOG_LEVEL"), "info");
}
Error Stack
$ cargo test
running 1 test
test env_file_quoted ... FAILED
failures:
---- env_file_quoted stdout ----
thread 'env_file_quoted' panicked at 'assertion failed: `(left == right)`
left: `"\"info\""`,
right: `"info"`', src/main.rs:12:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.