use std::process::Output;
use std::time::{Instant};
use tokio::process::Command;
use crate::logging::{log_timed, LogLevel};
pub async fn run_setup(debug: bool) -> Result<(), String> {
let setup_cmd: String = "sudo apt install -y net-tools nginx pkg-config libssl-dev build-essential plocate sshpass neofetch certbot python3-certbot-nginx".to_string();
if debug {
println!("[DEBUG] Running setup command: {}", setup_cmd);
}
let start: Instant = Instant::now();
let output: Output = Command::new("sh")
.arg("-c")
.arg(setup_cmd)
.output()
.await
.map_err(|e| format!("Failed to execute setup command: {}", e))?;
let elapsed = start.elapsed();
let _ = log_timed(LogLevel::Success, "setup", "Setup completed", elapsed.as_millis() as u64).await;
if debug {
println!("[DEBUG] Setup command output: {:?}", output);
println!("[DEBUG] Setup command took: {:.2?}", elapsed);
}
if !output.status.success() {
return Err(format!(
"Setup failed: {}",
String::from_utf8_lossy(&output.stderr)
));
}
if !output.stdout.is_empty() {
println!(
"\x1b[92mSetup output:\x1b[0m {}",
String::from_utf8_lossy(&output.stdout)
);
}
println!("\x1b[92mSetup completed successfully!\x1b[0m");
Ok(())
}