summaryrefslogtreecommitdiffstats
path: root/src/pipeline/builtins/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/pipeline/builtins/mod.rs')
-rw-r--r--src/pipeline/builtins/mod.rs71
1 files changed, 36 insertions, 35 deletions
diff --git a/src/pipeline/builtins/mod.rs b/src/pipeline/builtins/mod.rs
index 9c1ea9f..5c30575 100644
--- a/src/pipeline/builtins/mod.rs
+++ b/src/pipeline/builtins/mod.rs
@@ -6,7 +6,7 @@ pub use command::{Child, Command};
type Builtin = &'static (dyn for<'a> Fn(
crate::parse::Exe,
&'a Env,
- command::Io,
+ command::Cfg,
) -> anyhow::Result<command::Child<'a>>
+ Sync
+ Send);
@@ -28,24 +28,24 @@ static BUILTINS: once_cell::sync::Lazy<
});
macro_rules! bail {
- ($io:expr, $exe:expr, $msg:expr $(,)?) => {
- $io.write_stderr(
+ ($cfg:expr, $exe:expr, $msg:expr $(,)?) => {
+ $cfg.io().write_stderr(
format!("{}: {}\n", $exe.exe().display(), $msg).as_bytes()
)
.await
.unwrap();
return std::process::ExitStatus::from_raw(1 << 8);
};
- ($io:expr, $exe:expr, $msg:expr, $($arg:tt)*) => {
- $io.write_stderr(
+ ($cfg:expr, $exe:expr, $msg:expr, $($arg:tt)*) => {
+ $cfg.io().write_stderr(
format!("{}: ", $exe.exe().display()).as_bytes()
)
.await
.unwrap();
- $io.write_stderr(format!($msg, $($arg)*).as_bytes())
+ $cfg.io().write_stderr(format!($msg, $($arg)*).as_bytes())
.await
.unwrap();
- $io.write_stderr(b"\n").await.unwrap();
+ $cfg.io().write_stderr(b"\n").await.unwrap();
return std::process::ExitStatus::from_raw(1 << 8);
};
}
@@ -55,19 +55,19 @@ macro_rules! bail {
fn cd(
exe: crate::parse::Exe,
env: &Env,
- io: command::Io,
+ cfg: command::Cfg,
) -> anyhow::Result<command::Child> {
async fn async_cd(
exe: crate::parse::Exe,
_env: &Env,
- mut io: command::Io,
+ cfg: command::Cfg,
) -> std::process::ExitStatus {
let dir = exe.args().get(0).map_or("", String::as_str);
let dir = if dir.is_empty() {
if let Some(dir) = home(None) {
dir
} else {
- bail!(io, exe, "couldn't find current user");
+ bail!(cfg, exe, "couldn't find current user");
}
} else if dir.starts_with('~') {
let path: std::path::PathBuf = dir.into();
@@ -84,7 +84,7 @@ fn cd(
home.join(path.strip_prefix(prefix).unwrap())
} else {
bail!(
- io,
+ cfg,
exe,
"no such user: {}",
name.map(std::ffi::OsStr::to_string_lossy)
@@ -102,7 +102,7 @@ fn cd(
};
if let Err(e) = std::env::set_current_dir(&dir) {
bail!(
- io,
+ cfg,
exe,
"{}: {}",
crate::format::io_error(&e),
@@ -113,7 +113,7 @@ fn cd(
}
Ok(command::Child::new_fut(async move {
- async_cd(exe, env, io).await
+ async_cd(exe, env, cfg).await
}))
}
@@ -121,22 +121,22 @@ fn cd(
fn setenv(
exe: crate::parse::Exe,
env: &Env,
- io: command::Io,
+ cfg: command::Cfg,
) -> anyhow::Result<command::Child> {
async fn async_setenv(
exe: crate::parse::Exe,
_env: &Env,
- mut io: command::Io,
+ cfg: command::Cfg,
) -> std::process::ExitStatus {
let k = if let Some(k) = exe.args().get(0).map(String::as_str) {
k
} else {
- bail!(io, exe, "usage: setenv key value");
+ bail!(cfg, exe, "usage: setenv key value");
};
let v = if let Some(v) = exe.args().get(1).map(String::as_str) {
v
} else {
- bail!(io, exe, "usage: setenv key value");
+ bail!(cfg, exe, "usage: setenv key value");
};
std::env::set_var(k, v);
@@ -144,7 +144,7 @@ fn setenv(
}
Ok(command::Child::new_fut(async move {
- async_setenv(exe, env, io).await
+ async_setenv(exe, env, cfg).await
}))
}
@@ -152,17 +152,17 @@ fn setenv(
fn unsetenv(
exe: crate::parse::Exe,
env: &Env,
- io: command::Io,
+ cfg: command::Cfg,
) -> anyhow::Result<command::Child> {
async fn async_unsetenv(
exe: crate::parse::Exe,
_env: &Env,
- mut io: command::Io,
+ cfg: command::Cfg,
) -> std::process::ExitStatus {
let k = if let Some(k) = exe.args().get(0).map(String::as_str) {
k
} else {
- bail!(io, exe, "usage: unsetenv key");
+ bail!(cfg, exe, "usage: unsetenv key");
};
std::env::remove_var(k);
@@ -170,7 +170,7 @@ fn unsetenv(
}
Ok(command::Child::new_fut(async move {
- async_unsetenv(exe, env, io).await
+ async_unsetenv(exe, env, cfg).await
}))
}
@@ -181,17 +181,18 @@ fn unsetenv(
fn echo(
exe: crate::parse::Exe,
env: &Env,
- io: command::Io,
+ cfg: command::Cfg,
) -> anyhow::Result<command::Child> {
async fn async_echo(
exe: crate::parse::Exe,
_env: &Env,
- mut io: command::Io,
+ cfg: command::Cfg,
) -> std::process::ExitStatus {
macro_rules! write_stdout {
($bytes:expr) => {
- if let Err(e) = io.write_stdout($bytes).await {
- io.write_stderr(format!("echo: {}", e).as_bytes())
+ if let Err(e) = cfg.io().write_stdout($bytes).await {
+ cfg.io()
+ .write_stderr(format!("echo: {}", e).as_bytes())
.await
.unwrap();
return async_std::process::ExitStatus::from_raw(1 << 8);
@@ -212,19 +213,19 @@ fn echo(
}
Ok(command::Child::new_fut(async move {
- async_echo(exe, env, io).await
+ async_echo(exe, env, cfg).await
}))
}
fn and(
mut exe: crate::parse::Exe,
env: &Env,
- io: command::Io,
+ cfg: command::Cfg,
) -> anyhow::Result<command::Child> {
exe.shift();
if env.latest_status().success() {
let mut cmd = crate::pipeline::Command::new(exe);
- io.setup_command(&mut cmd);
+ cfg.setup_command(&mut cmd);
Ok(command::Child::new_wrapped(cmd.spawn(env)?))
} else {
let status = *env.latest_status();
@@ -235,7 +236,7 @@ fn and(
fn or(
mut exe: crate::parse::Exe,
env: &Env,
- io: command::Io,
+ cfg: command::Cfg,
) -> anyhow::Result<command::Child> {
exe.shift();
if env.latest_status().success() {
@@ -243,7 +244,7 @@ fn or(
Ok(command::Child::new_fut(async move { status }))
} else {
let mut cmd = crate::pipeline::Command::new(exe);
- io.setup_command(&mut cmd);
+ cfg.setup_command(&mut cmd);
Ok(command::Child::new_wrapped(cmd.spawn(env)?))
}
}
@@ -251,22 +252,22 @@ fn or(
fn command(
mut exe: crate::parse::Exe,
env: &Env,
- io: command::Io,
+ cfg: command::Cfg,
) -> anyhow::Result<command::Child> {
exe.shift();
let mut cmd = crate::pipeline::Command::new_binary(exe);
- io.setup_command(&mut cmd);
+ cfg.setup_command(&mut cmd);
Ok(command::Child::new_wrapped(cmd.spawn(env)?))
}
fn builtin(
mut exe: crate::parse::Exe,
env: &Env,
- io: command::Io,
+ cfg: command::Cfg,
) -> anyhow::Result<command::Child> {
exe.shift();
let mut cmd = crate::pipeline::Command::new_builtin(exe);
- io.setup_command(&mut cmd);
+ cfg.setup_command(&mut cmd);
Ok(command::Child::new_wrapped(cmd.spawn(env)?))
}