use crate::Result;
use crate::cli::json_output::{JsonSupervisorStatus, print_json};
use crate::ipc::client::IpcClient;
use crate::supervisor::supervisor_record_is_live;
#[derive(Debug, usage_rs::Args)]
#[usage()]
pub struct Status {
#[usage(long)]
json: bool,
}
impl Status {
pub async fn run(&self) -> Result<()> {
if self.json {
return print_json(&self.status_json().await);
}
let ipc = IpcClient::connect(false).await?;
info!("Pitchfork daemon is running");
if let Some(url) = ipc.get_web_url().await? {
info!("Web UI: {url}");
}
Ok(())
}
async fn status_json(&self) -> JsonSupervisorStatus {
match IpcClient::connect(false).await {
Ok(ipc) => match ipc.get_web_url().await {
Ok(web_ui) => JsonSupervisorStatus {
status: "up",
web_ui,
error: None,
},
Err(err) => JsonSupervisorStatus {
status: "up",
web_ui: None,
error: Some(format!("failed to get web UI URL: {err}")),
},
},
Err(err) => {
debug!("failed to connect to supervisor: {err:?}");
match super::existing_supervisor() {
Ok(Some(record)) if supervisor_record_is_live(&record) => {
JsonSupervisorStatus {
status: "unknown",
web_ui: None,
error: Some(format!(
"supervisor process is running but IPC connection failed: {err}"
)),
}
}
Ok(_) => JsonSupervisorStatus {
status: "down",
web_ui: None,
error: None,
},
Err(state_err) => JsonSupervisorStatus {
status: "unknown",
web_ui: None,
error: Some(format!("failed to read supervisor state: {state_err}")),
},
}
}
}
}
}