use std::{fmt::Display, process::Command}; use serde::Deserialize; use crate::{Error, Status}; #[derive(Debug, Clone, Deserialize)] pub struct Systemd { pub service: String, } impl Display for Systemd { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}.service", self.service.trim_end_matches(".service")) } } impl Systemd { pub async fn check(&self) -> Result { let output = Command::new("systemctl") .arg("is-active") .arg(&self.service) .output()?; let status = match output.status.success() { true => Status::Pass, false => { let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string(); Status::Fail(Some(format!("Service state: {}", stdout))) } }; Ok(status) } }