diff options
author | Toby Vincent <tobyv@tobyvin.dev> | 2024-09-26 17:31:16 -0500 |
---|---|---|
committer | Toby Vincent <tobyv@tobyvin.dev> | 2024-09-26 17:31:16 -0500 |
commit | fd992d7e3c03f37fbcafe9d3f26c72a2ead3b2a7 (patch) | |
tree | f3e29427d1bbe4a8d6e050abbd9f66afb5fa2152 /src/service/systemd.rs | |
parent | cbfca14b38806798847e3f2008038b25194a9b8b (diff) |
feat!: impl full api
Diffstat (limited to 'src/service/systemd.rs')
-rw-r--r-- | src/service/systemd.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/service/systemd.rs b/src/service/systemd.rs new file mode 100644 index 0000000..2e3b74c --- /dev/null +++ b/src/service/systemd.rs @@ -0,0 +1,33 @@ +use std::{fmt::Display, process::Command}; + +use serde::Deserialize; + +use crate::{Check, 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<Check, Error> { + let output = Command::new("systemctl") + .arg("is-active") + .arg(&self.service) + .output()?; + + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); + Ok((!output.status.success()) + .then(|| Check { + status: Status::Fail, + output: Some(format!("Service state: {}", stdout.trim())), + }) + .unwrap_or_default()) + } +} |