use std::collections::HashMap;
use std::io::{self, Write};
use anyhow::Result;
use crossterm::{cursor, execute, terminal};
use crate::config::{expand_template, CommandEntry};
fn announce(id: usize, cmd: &CommandEntry, expanded: &str) -> Result<()> {
let mut stdout = io::stdout();
let _ = execute!(stdout, terminal::Clear(terminal::ClearType::All), cursor::MoveTo(0, 0));
println!("Running [{id}] {}", cmd.name);
println!("$ {expanded}");
stdout.flush()?;
Ok(())
}
#[cfg(unix)]
pub fn run_command(id: usize, cmd: &CommandEntry, values: &HashMap<String, String>) -> Result<i32> {
use std::os::unix::process::CommandExt;
let expanded = expand_template(&cmd.command, values);
announce(id, cmd, &expanded)?;
let err = std::process::Command::new("sh").arg("-c").arg(&expanded).exec();
Err(err.into())
}
#[cfg(not(unix))]
pub fn run_command(id: usize, cmd: &CommandEntry, values: &HashMap<String, String>) -> Result<i32> {
let expanded = expand_template(&cmd.command, values);
announce(id, cmd, &expanded)?;
let status = std::process::Command::new("sh").arg("-c").arg(&expanded).status()?;
Ok(status.code().unwrap_or(1))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn expand_template_used_by_run_command_matches_config_module() {
let mut values = HashMap::new();
values.insert("x".to_string(), "42".to_string());
assert_eq!(expand_template("echo {{x}}", &values), "echo 42");
}
}