summaryrefslogtreecommitdiffstats
path: root/src/service/systemd.rs
blob: 45f3bf99e844d1a2710a287cb5a9d851816fcc89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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<Status, Error> {
        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)
    }
}