use anyhow::{anyhow, Result};
use log::{info, warn};
use regex::Regex;
use std::{
process::{Command, Stdio},
thread,
time::Duration,
};
use which::which;
pub fn check_foundry_installed() -> Result<()> {
which("foundry").map_err(|_| anyhow!("Foundry is not installed or not on PATH!"))?;
Ok(())
}
pub fn get_service_uri() -> Option<String> {
let output = Command::new("foundry")
.args(["service", "status"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.ok()?;
let stdout = String::from_utf8_lossy(&output.stdout);
let re = Regex::new(r"http://(?:[a-zA-Z0-9.-]+|\d{1,3}(\.\d{1,3}){3}):\d+").ok()?;
re.find(&stdout).map(|m| m.as_str().to_string())
}
pub fn start_service() -> Result<String> {
if let Some(service_url) = get_service_uri() {
info!("Foundry service is already running at {service_url}");
return Ok(service_url);
}
let _child = Command::new("foundry").args(["service", "start"]).spawn()?;
for _ in 0..10 {
if let Some(service_url) = get_service_uri() {
info!("Foundry service started successfully at {service_url}");
return Ok(service_url);
}
thread::sleep(Duration::from_millis(100));
}
warn!("Foundry service did not start within the expected time. May not be running.");
Err(anyhow!("Failed to start Foundry service"))
}