Skip to content

Commit

Permalink
[feat] add "env" context
Browse files Browse the repository at this point in the history
- reads environment variables and injects them into own context
  • Loading branch information
icepuma authored and rawkode committed Mar 16, 2022
1 parent 3a8e7e7 commit 37f599f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/contexts/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use super::ContextProvider;
use crate::contexts::Context;

pub struct EnvContextProvider;

impl ContextProvider for EnvContextProvider {
fn get_prefix(&self) -> String {
String::from("env")
}

fn get_contexts(&self) -> Vec<super::Context> {
let mut contexts = vec![];

for (key, value) in std::env::vars() {
contexts.push(Context::KeyValueContext(key, value));
}

contexts
}
}
32 changes: 31 additions & 1 deletion src/contexts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use user::UserContextProvider;

use crate::{
config::Config,
contexts::{os::OSContextProvider, variables::VariablesContextProvider},
contexts::{
env::EnvContextProvider, os::OSContextProvider, variables::VariablesContextProvider,
},
};

pub mod env;
pub mod os;
/// User context provider: understands the user running the command
pub mod user;
Expand All @@ -35,6 +38,7 @@ pub fn build_contexts(config: &Config) -> Contexts {
let context_providers: Vec<Box<dyn ContextProvider>> = vec![
Box::new(UserContextProvider {}),
Box::new(OSContextProvider {}),
Box::new(EnvContextProvider {}),
Box::new(VariablesContextProvider { config }),
];

Expand Down Expand Up @@ -135,4 +139,30 @@ mod test {

Ok(())
}

#[test]
fn env_context() -> anyhow::Result<()> {
let config = Config {
manifests: vec![],
variables: None,
};

std::env::set_var("ASCENDED_NAME", "Morgan Le Fay");
std::env::set_var("REAL_NAME", "Ganos Lal");

let contexts = build_contexts(&config);
let env_context_values = contexts.get("env");

assert_eq!(env_context_values.is_some(), true);
assert_eq!(
env_context_values.unwrap().get("ASCENDED_NAME").unwrap(),
"Morgan Le Fay"
);
assert_eq!(
env_context_values.unwrap().get("REAL_NAME").unwrap(),
"Ganos Lal"
);

Ok(())
}
}

0 comments on commit 37f599f

Please sign in to comment.