Commit ca069ed1 authored by Xavier L'Heureux's avatar Xavier L'Heureux Committed by AdminXVII
Browse files

If the source-sh argument is a file, execute it instead of a literal script

parent e92269fb
eval "$(lorri direnv)"
\ No newline at end of file
use_nix
......@@ -434,5 +434,6 @@ where
fn word_divide(buf: &Buffer) -> Vec<(usize, usize)> {
// -> impl Iterator<Item = (usize, usize)> + '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
}
......@@ -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);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment