From ca069ed1c113d8d07d6535c24939389065ce761f Mon Sep 17 00:00:00 2001 From: Xavier L'Heureux Date: Fri, 1 May 2020 20:33:49 -0400 Subject: [PATCH] If the source-sh argument is a file, execute it instead of a literal script --- .envrc | 2 +- src/binary/mod.rs | 3 ++- src/lib/builtins/mod.rs | 17 ++++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.envrc b/.envrc index be81fedd..4a4726a5 100644 --- a/.envrc +++ b/.envrc @@ -1 +1 @@ -eval "$(lorri direnv)" \ No newline at end of file +use_nix diff --git a/src/binary/mod.rs b/src/binary/mod.rs index 2a371edb..89e10a32 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -434,5 +434,6 @@ where fn word_divide(buf: &Buffer) -> Vec<(usize, usize)> { // -> impl Iterator + 'a - WordDivide { iter: buf.chars().copied().enumerate(), count: 0, word_start: None }.collect() // TODO: return iterator directly :D + WordDivide { iter: buf.chars().copied().enumerate(), count: 0, word_start: None }.collect() + // TODO: return iterator directly :D } diff --git a/src/lib/builtins/mod.rs b/src/lib/builtins/mod.rs index ea43f459..1935d214 100644 --- a/src/lib/builtins/mod.rs +++ b/src/lib/builtins/mod.rs @@ -260,17 +260,21 @@ SYNOPSYS source-sh SCRIPT DESCRIPTION - Execute the script literal given in argument and apply env vars diff to the current shell" + Execute the script given in argument and apply env vars diff to the current shell + If the script is a file, the file is executed, else is it treated as a literal script" )] pub fn source_sh(args: &[types::Str], _shell: &mut Shell<'_>) -> Status { - let arg = match args.get(1) { + let mut arg = match args.get(1) { None => return Status::bad_argument("Please pass a shell script as option"), - Some(arg) => arg, + Some(arg) => Cow::Borrowed(arg), }; let temp = match Temp::new_file() { Ok(f) => f, Err(e) => return Status::error(format!("Could not create temp file for source-sh: {}", e)), }; + if let Ok(s) = std::fs::read_to_string(arg.as_str()) { + arg = Cow::Owned(s.into()); + } let script = format!("{}\nenv | sort > {}", arg, temp.as_path().display()); match Command::new("sh") .args(&["-c", &script]) @@ -296,14 +300,13 @@ pub fn source_sh(args: &[types::Str], _shell: &mut Shell<'_>) -> Status { let val = match iter.next() { Some(v) => v, None => { - return Status::error(format!( - "Could not parse env file, no value for: '{}'", - name - )) + eprintln!("Invalid environment variable '{}'. Proceeding anyway", name); + continue; } }; let prev_val = std::env::var_os(name); if prev_val.as_ref().and_then(|x| x.to_str()) != Some(val) { + println!("Set {} to {}", name, val); std::env::set_var(name, val); } } -- GitLab