use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellConfig {
pub command: String,
pub timeout_secs: Option<u64>,
pub dir: Option<String>,
pub env: Vec<(String, String)>,
pub clean_env: bool,
}
impl ShellConfig {
pub fn new(command: &str) -> Self {
Self {
command: command.to_string(),
timeout_secs: None,
dir: None,
env: Vec::new(),
clean_env: false,
}
}
pub fn timeout_secs(mut self, secs: u64) -> Self {
self.timeout_secs = Some(secs);
self
}
pub fn dir(mut self, dir: &str) -> Self {
self.dir = Some(dir.to_string());
self
}
pub fn env(mut self, key: &str, value: &str) -> Self {
self.env.push((key.to_string(), value.to_string()));
self
}
pub fn clean_env(mut self) -> Self {
self.clean_env = true;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder() {
let config = ShellConfig::new("cargo test")
.timeout_secs(60)
.dir("/app")
.env("RUST_LOG", "debug")
.clean_env();
assert_eq!(config.command, "cargo test");
assert_eq!(config.timeout_secs, Some(60));
assert_eq!(config.dir, Some("/app".to_string()));
assert_eq!(
config.env,
vec![("RUST_LOG".to_string(), "debug".to_string())]
);
assert!(config.clean_env);
}
}