use clap::Parser;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
#[derive(Debug, Parser)]
pub struct Opts {
#[arg(long = "port")]
pub port: Option<u16>,
#[arg(long = "open", default_value_t = false)]
pub open: bool,
}
pub async fn run(port: Option<u16>, open: bool) -> anyhow::Result<()> {
let port = port.unwrap_or(4200);
let mut command = format!("==> trunk serve --port {port}");
let mut args = vec!["--port".to_string(), format!("{port}")];
if open {
command = format!("{command} --open");
args.push("--open".to_string());
}
eprintln!("{command}");
let mut cmd = Command::new("trunk");
let child = cmd.arg("serve").args(args.as_slice()).stdout(Stdio::piped()).spawn()?;
if let Some(stdout) = child.stdout {
let reader = BufReader::new(stdout);
for res in reader.lines() {
match res {
Ok(line) => {
eprintln!("{line}");
}
Err(_) => break,
}
}
}
Ok(())
}