use std::io::Write; use std::{ error::Error, fs::{remove_file, File}, path::Path, process::Command, }; use anyhow::{Context, Result}; const COMMAND_NAME: &str = "../target/release/tinted-builder-rust"; #[allow(dead_code)] pub fn run_command(command_vec: Vec) -> Result<(String, String), Box> { let output = Command::new(COMMAND_NAME) .args(&command_vec) .output() .expect("Failed to execute command"); if !output.stderr.is_empty() { println!( "Init command stderr: {}", String::from_utf8_lossy(&output.stderr) ); } let stdout = strip_ansi_escapes::strip(String::from_utf8(output.stdout)?); let stderr = strip_ansi_escapes::strip(String::from_utf8(output.stderr)?); Ok((String::from_utf8(stdout)?, String::from_utf8(stderr)?)) } #[allow(dead_code)] pub fn write_to_file(path: &Path, contents: &str) -> Result<()> { if path.exists() { remove_file(path).with_context(|| format!("Unable to remove file: {}", path.display()))?; } let mut file = File::create(path).with_context(|| format!("Unable to create file: {}", path.display()))?; file.write_all(contents.as_bytes())?; Ok(()) }