use std::{
collections::BTreeMap,
io::{self, Read},
path::PathBuf,
process::{Child, Command, ExitStatus, Stdio},
sync::{
atomic::{AtomicBool, Ordering},
mpsc::{self, Receiver, RecvTimeoutError},
Arc,
},
thread,
time::{Duration, Instant},
};
use crate::{CommandContext, CommandError};
const DEFAULT_MAX_OUTPUT_BYTES: usize = 1024 * 1024;
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(300);
const MAX_STDERR_BYTES: usize = 64 * 1024;
const WAIT_INTERVAL: Duration = Duration::from_millis(10);
#[cfg(windows)]
const SHELL: &str = "cmd";
#[cfg(windows)]
const SHELL_FLAG: &str = "/C";
#[cfg(not(windows))]
const SHELL: &str = "sh";
#[cfg(not(windows))]
const SHELL_FLAG: &str = "-c";
#[derive(Debug, Clone)]
pub struct Shell {
directory: PathBuf,
origins: BTreeMap<String, PathBuf>,
max_output_bytes: usize,
timeout: Option<Duration>,
}
impl Shell {
#[must_use]
pub fn new(directory: impl Into<PathBuf>) -> Self {
Self {
directory: directory.into(),
origins: BTreeMap::new(),
max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES,
timeout: Some(DEFAULT_TIMEOUT),
}
}
#[must_use]
pub fn with_origins(directory: impl Into<PathBuf>, origins: BTreeMap<String, PathBuf>) -> Self {
Self {
directory: directory.into(),
origins,
max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES,
timeout: Some(DEFAULT_TIMEOUT),
}
}
#[must_use]
pub const fn with_max_output_bytes(mut self, value: usize) -> Self {
self.max_output_bytes = value;
self
}
#[must_use]
pub const fn with_timeout(mut self, value: Duration) -> Self {
self.timeout = Some(value);
self
}
#[must_use]
pub const fn without_timeout(mut self) -> Self {
self.timeout = None;
self
}
#[must_use = "callbacks are lazy and do nothing unless passed to resolution"]
pub fn callback(&self) -> impl FnMut(CommandContext<'_>) -> Result<String, CommandError> + '_ {
move |context| self.run(&context)
}
pub fn run(&self, context: &CommandContext<'_>) -> Result<String, CommandError> {
let directory = context
.source
.and_then(|source| self.origins.get(source))
.unwrap_or(&self.directory);
let mut child = Command::new(SHELL)
.arg(SHELL_FLAG)
.arg(context.command)
.current_dir(directory)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|error| {
CommandError::new(format!(
"failed to run command in {}: {error}",
directory.display()
))
})?;
let stdout = child
.stdout
.take()
.ok_or_else(|| CommandError::new("command standard output was not captured"))?;
let stderr = child
.stderr
.take()
.ok_or_else(|| CommandError::new("command standard error was not captured"))?;
let overflow = Arc::new(AtomicBool::new(false));
let stdout_overflow = Arc::clone(&overflow);
let cap = self.max_output_bytes;
let (stdout_sender, stdout_receiver) = mpsc::channel();
let (stderr_sender, stderr_receiver) = mpsc::channel();
let stdout_reader = thread::spawn(move || {
let _ = stdout_sender.send(read_limited(stdout, cap, Some(&stdout_overflow)));
});
let stderr_reader = thread::spawn(move || {
let _ = stderr_sender.send(read_limited(stderr, MAX_STDERR_BYTES, None));
});
let status = wait_for_child(&mut child, self.timeout, &overflow, cap);
let status = status?;
let stdout = receive_reader(&stdout_receiver, "standard output")?;
let stderr = receive_reader(&stderr_receiver, "standard error")?;
drop(stdout_reader);
drop(stderr_reader);
if overflow.load(Ordering::Relaxed) {
return Err(CommandError::new(format!(
"command output exceeds {cap} bytes"
)));
}
if !status.success() {
let stderr = String::from_utf8_lossy(&stderr);
return Err(CommandError::new(format!(
"command failed with {}: {}",
status,
stderr.trim_end()
)));
}
let mut stdout = String::from_utf8(stdout)
.map_err(|_| CommandError::new("command output is not valid UTF-8"))?;
if stdout.ends_with('\n') {
stdout.pop();
if stdout.ends_with('\r') {
stdout.pop();
}
}
Ok(stdout)
}
}
fn wait_for_child(
child: &mut Child,
timeout: Option<Duration>,
overflow: &AtomicBool,
cap: usize,
) -> Result<ExitStatus, CommandError> {
let started = Instant::now();
loop {
if overflow.load(Ordering::Relaxed) {
stop_child(child);
return Err(CommandError::new(format!(
"command output exceeds {cap} bytes"
)));
}
match child.try_wait() {
Ok(Some(status)) => return Ok(status),
Ok(None) => {}
Err(error) => {
stop_child(child);
return Err(CommandError::new(format!(
"failed to wait for command: {error}"
)));
}
}
if let Some(timeout) = timeout {
if started.elapsed() >= timeout {
stop_child(child);
return Err(CommandError::new(format!(
"command timed out after {} seconds",
timeout.as_secs_f64()
)));
}
}
thread::sleep(WAIT_INTERVAL);
}
}
fn stop_child(child: &mut Child) {
let _ = child.kill();
let _ = child.wait();
}
fn read_limited(
mut reader: impl Read,
cap: usize,
overflow: Option<&Arc<AtomicBool>>,
) -> io::Result<Vec<u8>> {
let mut bytes = Vec::with_capacity(cap.min(8192));
let mut buffer = [0u8; 8192];
loop {
match reader.read(&mut buffer) {
Ok(0) => return Ok(bytes),
Ok(read) => {
let room = cap.saturating_sub(bytes.len());
if room > 0 {
bytes.extend_from_slice(&buffer[..read.min(room)]);
}
if read > room {
if let Some(flag) = overflow {
flag.store(true, Ordering::Relaxed);
}
}
}
Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
Err(error) => return Err(error),
}
}
}
fn receive_reader(
receiver: &Receiver<io::Result<Vec<u8>>>,
stream: &str,
) -> Result<Vec<u8>, CommandError> {
match receiver.recv_timeout(Duration::from_secs(2)) {
Ok(Ok(bytes)) => Ok(bytes),
Ok(Err(error)) => Err(CommandError::new(format!(
"failed to read command {stream}: {error}"
))),
Err(RecvTimeoutError::Timeout) => Err(CommandError::new(format!(
"command {stream} remained open after it stopped"
))),
Err(RecvTimeoutError::Disconnected) => Err(CommandError::new(format!(
"command {stream} reader stopped"
))),
}
}
#[cfg(test)]
#[cfg(unix)]
mod tests {
use super::Shell;
use crate::{parse_strict, ExpandOptions};
#[test]
fn runs_a_command_and_trims_one_trailing_newline() {
let shell = Shell::new(".");
let doc = parse_strict("A=$(printf 'hi\\n\\n')\n").unwrap();
let resolved = doc
.resolve_with_commands(
|_| None,
shell.callback(),
Some("synthetic"),
&ExpandOptions::dotenv_expand(),
)
.unwrap();
assert_eq!(resolved.entries()[0].value.as_deref(), Some("hi\n"));
assert_eq!(resolved.commands().len(), 1);
}
#[test]
fn rejects_nonzero_status_and_missing_directories() {
let shell = Shell::new(".");
let error = shell
.run(&crate::CommandContext {
command: "exit 7",
reference: 0..0,
payload: 0..0,
source: None,
})
.unwrap_err();
assert!(error.message().contains("failed"));
let shell = Shell::new("/definitely/not/a/real/adotenv-directory");
let error = shell
.run(&crate::CommandContext {
command: "printf hi",
reference: 0..0,
payload: 0..0,
source: None,
})
.unwrap_err();
assert!(error.message().contains("failed"));
}
fn context(command: &str) -> crate::CommandContext<'_> {
crate::CommandContext {
command,
reference: 0..0,
payload: 0..0,
source: None,
}
}
#[test]
fn default_output_cap_allows_more_than_the_mutated_small_caps() {
let shell = Shell::new(".");
let output = shell.run(&context("printf '%4096s' ''")).unwrap();
assert_eq!(output.len(), 4096);
}
#[test]
fn stderr_and_reader_caps_count_bytes_exactly() {
use std::io::Cursor;
let input = vec![b'x'; super::MAX_STDERR_BYTES + 1];
let bytes = super::read_limited(Cursor::new(input), super::MAX_STDERR_BYTES, None).unwrap();
assert_eq!(bytes.len(), super::MAX_STDERR_BYTES);
let overflow = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let input = vec![b'x'; 4];
let bytes = super::read_limited(Cursor::new(input), 4, Some(&overflow)).unwrap();
assert_eq!(bytes.len(), 4);
assert!(!overflow.load(std::sync::atomic::Ordering::Relaxed));
let input = vec![b'x'; 5];
let bytes = super::read_limited(Cursor::new(input), 4, Some(&overflow)).unwrap();
assert_eq!(bytes.len(), 4);
assert!(overflow.load(std::sync::atomic::Ordering::Relaxed));
}
#[test]
fn stops_a_command_that_exceeds_the_output_cap() {
let shell = Shell::new(".").with_max_output_bytes(4);
let error = shell.run(&context("printf 'abcdef'")).unwrap_err();
assert!(error.message().contains("exceeds 4 bytes"), "{error}");
}
#[test]
fn stops_a_command_that_exceeds_the_timeout() {
let shell = Shell::new(".").with_timeout(std::time::Duration::from_millis(100));
let error = shell.run(&context("sleep 5")).unwrap_err();
assert!(error.message().contains("timed out"), "{error}");
}
#[test]
fn source_origins_select_an_explicit_directory() {
let directory = std::env::temp_dir().join(format!(
"adotenv-exec-test-{}-{}",
std::process::id(),
std::thread::current().name().unwrap_or("thread")
));
std::fs::create_dir_all(&directory).unwrap();
let mut origins = std::collections::BTreeMap::new();
origins.insert("synthetic".to_string(), directory.clone());
let shell = Shell::with_origins(".", origins);
let output = shell
.run(&crate::CommandContext {
command: "pwd",
reference: 0..0,
payload: 0..0,
source: Some("synthetic"),
})
.unwrap();
assert_eq!(
std::fs::canonicalize(output.trim()).unwrap(),
std::fs::canonicalize(directory).unwrap()
);
std::fs::remove_dir_all(std::fs::canonicalize(output.trim()).unwrap()).ok();
}
}