use std::process::ExitCode;
use clap::Args;
use crate::config::{resolve_dashboard_port, CliConfig};
use super::pid;
#[derive(Debug, Args)]
pub struct OpenArgs {
#[arg(long, env = "AASM_DASHBOARD_PORT")]
pub port: Option<u16>,
}
pub fn dispatch(args: OpenArgs, config: &CliConfig) -> ExitCode {
let port = pid::read_pid()
.map(|(_, p)| p)
.unwrap_or_else(|| resolve_dashboard_port(config, args.port));
let url = format!("http://127.0.0.1:{port}");
let reachable = reqwest::blocking::get(&url)
.map(|r| r.status().is_success() || r.status().as_u16() < 500)
.unwrap_or(false);
if !reachable {
eprintln!("error: dashboard is not running at {url}");
eprintln!("hint: start it first with `aasm dashboard start`");
return ExitCode::FAILURE;
}
if let Err(e) = open::that(&url) {
eprintln!("error: could not open browser: {e}");
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}