-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
168 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use std::path::PathBuf; | ||
|
||
use indexmap::IndexMap; | ||
use itertools::Itertools; | ||
|
||
use crate::env; | ||
use crate::env_diff::{EnvDiff, EnvDiffOperation}; | ||
use crate::tera::{get_tera, BASE_CONTEXT}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct EnvMan { | ||
entries: Vec<(EnvManEntry, EnvManOpts)>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum EnvManEntry { | ||
/// simple key/value pair | ||
Val(String, String), | ||
/// remove a key | ||
Rm(String), | ||
/// key/value rendered as a tera template | ||
Tmpl(String, String), | ||
/// add a path to the PATH | ||
Path(PathBuf), | ||
/// run a bash script and apply the resulting env diff | ||
Script(PathBuf), | ||
// Plugin(String, String), | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct EnvManOpts { | ||
pub source: PathBuf, | ||
pub project_root: Option<PathBuf>, | ||
// pub cache: Option<EnvManOptsCache>, | ||
} | ||
|
||
// #[derive(Debug)] | ||
// pub struct EnvManOptsCache { | ||
// pub key: String, | ||
// pub globs: Vec<String>, | ||
// } | ||
|
||
impl EnvMan { | ||
fn new() -> Self { | ||
Self { entries: vec![] } | ||
} | ||
|
||
pub fn render(&self) -> eyre::Result<IndexMap<String, String>> { | ||
let mut env: IndexMap<String, String> = env::PRISTINE_ENV.clone().into_iter().collect(); | ||
let ctx = BASE_CONTEXT.clone(); | ||
for (entry, opts) in self.entries.iter() { | ||
match entry { | ||
EnvManEntry::Val(k, v) => { | ||
env.insert(k.clone(), v.clone()); | ||
} | ||
EnvManEntry::Rm(k) => { | ||
env.remove(k); | ||
} | ||
EnvManEntry::Tmpl(k, v) => { | ||
let mut tera = get_tera(opts.project_root.as_deref()); | ||
let v = tera.render_str(v, &ctx)?; | ||
env.insert(k.clone(), v); | ||
} | ||
EnvManEntry::Path(p) => { | ||
let mut path = env::split_paths(p).collect_vec(); | ||
if let Some(orig) = env.get("PATH") { | ||
path = path.into_iter().chain(env::split_paths(orig)).collect_vec(); | ||
} | ||
let path = env::join_paths(path)?; | ||
env.insert("PATH".into(), path.to_string_lossy().to_string()); | ||
} | ||
EnvManEntry::Script(script) => { | ||
// TODO: set cwd | ||
let ed = EnvDiff::from_bash_script(script, &env)?; | ||
for p in ed.to_patches() { | ||
match p { | ||
EnvDiffOperation::Add(k, v) | EnvDiffOperation::Change(k, v) => { | ||
env.insert(k, v); | ||
} | ||
EnvDiffOperation::Remove(k) => { | ||
env.remove(&k); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Ok(env) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_var() { | ||
let mut em = EnvMan::new(); | ||
em.entries.push(( | ||
EnvManEntry::Val("FOO".into(), "BAR".into()), | ||
Default::default(), | ||
)); | ||
let env = em.render().unwrap(); | ||
assert_eq!(env["FOO"], "BAR"); | ||
} | ||
|
||
#[test] | ||
fn test_rm() { | ||
let mut em = EnvMan::new(); | ||
em.entries.push(( | ||
EnvManEntry::Val("FOO".into(), "BAR".into()), | ||
Default::default(), | ||
)); | ||
em.entries | ||
.push((EnvManEntry::Rm("FOO".into()), Default::default())); | ||
let env = em.render().unwrap(); | ||
assert!(!env.contains_key("FOO")); | ||
} | ||
|
||
#[test] | ||
fn test_tmpl() { | ||
let mut em = EnvMan::new(); | ||
em.entries.push(( | ||
EnvManEntry::Tmpl("FOO".into(), "{{ 1 + 1 }}".into()), | ||
Default::default(), | ||
)); | ||
let env = em.render().unwrap(); | ||
assert_eq!(env["FOO"], "2"); | ||
} | ||
|
||
#[test] | ||
fn test_path() { | ||
let mut em = EnvMan::new(); | ||
em.entries | ||
.push((EnvManEntry::Path("/foo/bar".into()), Default::default())); | ||
let env = em.render().unwrap(); | ||
assert!(env["PATH"].starts_with("/foo/bar")); | ||
|
||
em.entries | ||
.push((EnvManEntry::Path("/baz/qux".into()), Default::default())); | ||
let env = em.render().unwrap(); | ||
assert!(env["PATH"].starts_with("/baz/qux:/foo/bar")); | ||
} | ||
|
||
#[test] | ||
fn test_script() { | ||
let mut em = EnvMan::new(); | ||
em.entries.push(( | ||
EnvManEntry::Script("../fixtures/exec-env".into()), | ||
Default::default(), | ||
)); | ||
let env = em.render().unwrap(); | ||
assert_eq!(&env["UNMODIFIED_VAR"], "unmodified"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters