Skip to content

Commit

Permalink
bash mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Jun 10, 2023
1 parent 11fde60 commit 0d2d057
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 92 deletions.
80 changes: 7 additions & 73 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "adana"
version = "0.13.26"
version = "0.13.27"
edition = "2021"
authors = ["Nordine Bittich"]
license = "MIT"
Expand All @@ -23,7 +23,7 @@ bincode = "1.3.3"
dirs = "5.0.1"
log = "0.4.18"
nom = { version = "7.1.3" }
nu-ansi-term = "0.47.0"
nu-ansi-term = "0.48.0"
rustyline = "11.0.0"
rustyline-derive = "0.8.0"
serde = { version = "1.0.163", features = ['serde_derive', 'rc'] }
Expand Down
51 changes: 36 additions & 15 deletions src/cache_command/os_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,51 @@ fn extract_program(s: &str) -> Res<&str> {
pub fn exec_command<'a>(
command: &'a str,
extra_args: &'a Option<&'a str>,
bash_command: bool,
) -> Res<'a, ()> {
let (remaining, envs) = extract_envs(command)?;
let (remaining, program) = extract_program(remaining)?;
let handle = {
if bash_command {
Command::new("bash")
.args([
"-c",
&format!(
"{command} {}",
if let Some(extra_args) = extra_args {
extra_args
} else {
""
}
),
])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
} else {
let (remaining, envs) = extract_envs(command)?;
let (remaining, program) = extract_program(remaining)?;

let (_, mut args) = extract_args(remaining)?;
let (_, mut args) = extract_args(remaining)?;

if let Some(extra_args) = extra_args {
let (_, mut extra_args) = extract_args(extra_args)?;
args.append(&mut extra_args);
}
if let Some(extra_args) = extra_args {
let (_, mut extra_args) = extract_args(extra_args)?;
args.append(&mut extra_args);
}

let handle = Command::new(program)
.envs(envs)
.args(&args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn();
Command::new(program)
.envs(envs)
.args(&args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
}
};

match handle.and_then(|mut h| h.wait()) {
Ok(status) => {
debug!("{status}");
}
Err(e) => {
eprintln!("{program} failed to start with args {args:?}. err: {e}")
eprintln!("{command} failed to start. err: {e}")
}
}

Expand All @@ -89,7 +110,7 @@ mod test {

#[test]
fn test_exec_command() {
exec_command("echo 'hello world'", &None).unwrap();
exec_command("echo 'hello world'", &None, false).unwrap();
println!("bye")
}
}
6 changes: 4 additions & 2 deletions src/cache_command/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ pub fn process_command(
}
CacheCommand::Exec { key, args } => {
if let Some(value) = get_value(db, current_cache, key) {
let _ = exec_command(&value, &args)
let _ = exec_command(&value, &args, false)
.map_err(|e| anyhow::Error::msg(e.to_string()))?;
} else if !key.trim().is_empty() {
return Err(anyhow::Error::msg(format!("{key} not found")));
exec_command(key, &args, true)
.map_err(|e| anyhow::Error::msg(e.to_string()))?;
//return Err(anyhow::Error::msg(format!("{key} not found")));
}
}
CacheCommand::Using(key) => {
Expand Down

0 comments on commit 0d2d057

Please sign in to comment.