use std::path::PathBuf;
#[derive(Debug, Clone)]
pub enum LaunchSpec {
Launch {
headless: bool,
from_profile: Option<PathBuf>,
clone_state_from: Option<String>,
no_stealth: bool,
},
Connect { ws_url: String },
}
impl LaunchSpec {
pub fn is_live(&self) -> bool {
matches!(self, LaunchSpec::Connect { .. })
}
}
pub fn resolve_cdp_spec(spec: &str) -> Result<String, String> {
let trimmed = spec.trim();
if trimmed.eq_ignore_ascii_case("auto") {
return auto_connect().map(|(_, url)| url);
}
if trimmed.starts_with("ws://") || trimmed.starts_with("wss://") {
return Ok(trimmed.to_string());
}
let port: u16 = trimmed.parse().map_err(|_| {
format!(
"Invalid --cdp spec '{}': expected port, ws:// URL, or 'auto'",
spec
)
})?;
eoka::cdp::discover::discover_browser_ws("127.0.0.1", port)
.map_err(|e| format!("Failed to discover Chrome on 127.0.0.1:{}: {}", port, e))
}
pub fn auto_connect() -> Result<(u16, String), String> {
eoka::cdp::discover::auto_connect(9222, 9229)
.ok_or_else(|| "No running Chrome found on ports 9222-9229".to_string())
}
pub fn session_suffix(spec: &LaunchSpec) -> &'static str {
match spec {
LaunchSpec::Launch { headless: true, .. } => "",
LaunchSpec::Launch {
headless: false, ..
} => "-headed",
LaunchSpec::Connect { .. } => "-live",
}
}