Files
nucleic-brush/brush-builtins/src/dot.rs
T

51 lines
1.7 KiB
Rust
Raw Normal View History

use std::path::{Path, PathBuf};
use brush_core::builtins;
use clap::Parser;
/// Evaluate the provided script in the current shell environment.
#[derive(Parser)]
pub(crate) struct DotCommand {
/// Path to the script to evaluate.
script_path: String,
/// Any arguments to be passed as positional parameters to the script.
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
script_args: Vec<String>,
}
impl builtins::Command for DotCommand {
type Error = brush_core::Error;
async fn execute<SE: brush_core::ShellExtensions>(
&self,
context: brush_core::ExecutionContext<'_, SE>,
) -> Result<brush_core::ExecutionResult, Self::Error> {
// TODO(dot): Handle trap inheritance.
// nash: POSIX requires `.` to search PATH when its operand has no slash.
// Git's `git-subtree` relies on this with `. git-sh-setup`; treating the
// operand as cwd-relative leaves `require_work_tree` undefined.
let script_path = Path::new(&self.script_path);
let resolved_path = if context.shell.options().source_builtin_searches_path
&& !self.script_path.chars().any(std::path::is_separator)
{
context
.shell
.env_str("PATH")
.and_then(|path| {
std::env::split_paths(path.as_ref())
.map(|dir| dir.join(script_path))
.find(|candidate| candidate.is_file())
})
.unwrap_or_else(|| PathBuf::from(script_path))
} else {
PathBuf::from(script_path)
};
context
.shell
.source_script(&resolved_path, self.script_args.iter(), &context.params)
.await
}
}