diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-01-04 04:18:30 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-01-04 04:18:30 -0500 |
commit | 8d71dab0c08ab613cf00601dea2af16bedc339f6 (patch) | |
tree | 14328aaf6c00f97f8ebf023d0478b0562d8801f9 /src | |
parent | 36e0c3f8acea18bf2d0b5bcd11191daede7a49ef (diff) | |
download | nbsh-8d71dab0c08ab613cf00601dea2af16bedc339f6.tar.gz nbsh-8d71dab0c08ab613cf00601dea2af16bedc339f6.zip |
fix error display a bit
Diffstat (limited to 'src')
-rw-r--r-- | src/pipeline/command.rs | 68 |
1 files changed, 47 insertions, 21 deletions
diff --git a/src/pipeline/command.rs b/src/pipeline/command.rs index e4a3537..408f845 100644 --- a/src/pipeline/command.rs +++ b/src/pipeline/command.rs @@ -1,56 +1,74 @@ use async_std::os::unix::process::CommandExt as _; -pub enum Command { +pub struct Command { + inner: Inner, + exe: String, +} +pub enum Inner { Binary(async_std::process::Command), Builtin(crate::builtins::Command), } impl Command { pub fn new(exe: crate::parse::Exe) -> Self { - crate::builtins::Command::new(exe) - .map_or_else(Self::new_binary, Self::Builtin) + let exe_str = exe.exe().to_string(); + Self { + inner: crate::builtins::Command::new(exe).map_or_else( + |exe| Self::new_binary(exe).inner, + Inner::Builtin, + ), + exe: exe_str, + } } #[allow(clippy::needless_pass_by_value)] pub fn new_binary(exe: crate::parse::Exe) -> Self { + let exe_str = exe.exe().to_string(); let mut cmd = async_std::process::Command::new(exe.exe()); cmd.args(exe.args()); - Self::Binary(cmd) + Self { + inner: Inner::Binary(cmd), + exe: exe_str, + } } pub fn new_builtin(exe: crate::parse::Exe) -> Self { - crate::builtins::Command::new(exe) - .map_or_else(|_| todo!(), Self::Builtin) + let exe_str = exe.exe().to_string(); + Self { + inner: crate::builtins::Command::new(exe) + .map_or_else(|_| todo!(), Inner::Builtin), + exe: exe_str, + } } pub fn stdin(&mut self, fh: std::fs::File) { - match self { - Self::Binary(cmd) => { + match &mut self.inner { + Inner::Binary(cmd) => { cmd.stdin(fh); } - Self::Builtin(cmd) => { + Inner::Builtin(cmd) => { cmd.stdin(fh); } } } pub fn stdout(&mut self, fh: std::fs::File) { - match self { - Self::Binary(cmd) => { + match &mut self.inner { + Inner::Binary(cmd) => { cmd.stdout(fh); } - Self::Builtin(cmd) => { + Inner::Builtin(cmd) => { cmd.stdout(fh); } } } pub fn stderr(&mut self, fh: std::fs::File) { - match self { - Self::Binary(cmd) => { + match &mut self.inner { + Inner::Binary(cmd) => { cmd.stderr(fh); } - Self::Builtin(cmd) => { + Inner::Builtin(cmd) => { cmd.stderr(fh); } } @@ -60,20 +78,28 @@ impl Command { where F: 'static + FnMut() -> std::io::Result<()> + Send + Sync, { - match self { - Self::Binary(cmd) => { + match &mut self.inner { + Inner::Binary(cmd) => { cmd.pre_exec(f); } - Self::Builtin(cmd) => { + Inner::Builtin(cmd) => { cmd.pre_exec(f); } } } pub fn spawn(self, env: &crate::env::Env) -> anyhow::Result<Child> { - match self { - Self::Binary(mut cmd) => Ok(Child::Binary(cmd.spawn()?)), - Self::Builtin(cmd) => Ok(Child::Builtin(cmd.spawn(env)?)), + match self.inner { + Inner::Binary(mut cmd) => { + Ok(Child::Binary(cmd.spawn().map_err(|e| { + anyhow::anyhow!( + "{}: {}", + crate::format::io_error(&e), + self.exe + ) + })?)) + } + Inner::Builtin(cmd) => Ok(Child::Builtin(cmd.spawn(env)?)), } } } |