use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;
use tempfile::NamedTempFile;
use tokio::process::Child;
#[derive(Debug, Clone)]
pub struct ScriptOptions {
pub(crate) openscript_path: Option<PathBuf>,
pub(crate) working_directory: Option<PathBuf>,
pub(crate) env_vars: HashMap<String, String>,
pub(crate) args: Vec<String>,
pub(crate) stdin: IoOptions,
pub(crate) stdout: IoOptions,
pub(crate) stderr: IoOptions,
pub(crate) timeout: Option<Duration>,
pub(crate) exit_on_error: bool,
pub(crate) print_commands: bool,
pub(crate) ai_enabled: bool,
pub(crate) clear_env: bool,
}
impl ScriptOptions {
pub fn new() -> Self {
Self::default()
}
pub fn openscript_path(mut self, path: impl Into<PathBuf>) -> Self {
self.openscript_path = Some(path.into());
self
}
pub fn working_directory(mut self, path: impl Into<PathBuf>) -> Self {
self.working_directory = Some(path.into());
self
}
pub fn env(mut self, key: impl Into<String>, val: impl Into<String>) -> Self {
self.env_vars.insert(key.into(), val.into());
self
}
pub fn args(mut self, args: Vec<String>) -> Self {
self.args = args;
self
}
pub fn stdin(mut self, opt: IoOptions) -> Self {
self.stdin = opt;
self
}
pub fn stdout(mut self, opt: IoOptions) -> Self {
self.stdout = opt;
self
}
pub fn stderr(mut self, opt: IoOptions) -> Self {
self.stderr = opt;
self
}
pub fn timeout(mut self, duration: Duration) -> Self {
self.timeout = Some(duration);
self
}
pub fn exit_on_error(mut self, exit: bool) -> Self {
self.exit_on_error = exit;
self
}
pub fn print_commands(mut self, print: bool) -> Self {
self.print_commands = print;
self
}
pub fn ai_enabled(mut self, enabled: bool) -> Self {
self.ai_enabled = enabled;
self
}
pub fn clear_env(mut self, clear: bool) -> Self {
self.clear_env = clear;
self
}
}
impl Default for ScriptOptions {
fn default() -> Self {
Self {
openscript_path: None,
working_directory: None,
env_vars: HashMap::new(),
args: Vec::new(),
stdin: IoOptions::Inherit,
stdout: IoOptions::Pipe,
stderr: IoOptions::Pipe,
timeout: None,
exit_on_error: true,
print_commands: false,
ai_enabled: false,
clear_env: false,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct ExecResult {
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
pub duration: Duration,
pub timed_out: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum IoOptions {
Inherit,
#[default]
Pipe,
Null,
}
#[derive(Debug)]
pub struct SpawnResult {
pub child: Child,
pub(crate) _temp_file: Option<NamedTempFile>,
}