use super::{bind_addr, paths_daemon_log, spawn_detached, stop_existing_for_restart};
pub fn restart(json: bool, quiet: bool) -> anyhow::Result<()> {
let old_pid = stop_existing_for_restart(json || quiet)?;
let new_pid = spawn_detached()?;
if json {
println!("{}", restart_json(old_pid, new_pid));
} else {
println!("{}", restart_rotation_line(old_pid, new_pid));
if !quiet {
report_endpoints();
}
}
Ok(())
}
pub(crate) fn restart_rotation_line(old: Option<u32>, new: u32) -> String {
let old = old.map_or_else(|| "none".to_string(), |pid| pid.to_string());
format!("restarted: pid {old} -> {new}")
}
pub(crate) fn restart_json(old: Option<u32>, new: u32) -> String {
serde_json::json!({
"old": old,
"new": new,
"address": bind_addr(),
})
.to_string()
}
pub(crate) fn start_detached_and_report(verb: &str) -> anyhow::Result<()> {
let pid = spawn_detached()?;
println!(
"moadim {verb} in the background (pid {pid}) at http://{}",
bind_addr()
);
report_endpoints();
Ok(())
}
fn report_endpoints() {
println!(" UI http://{}", bind_addr());
println!(" stop moadim stop (or use the STOP button in the UI)");
println!(" logs {}", paths_daemon_log());
}