okey/cli/utils/
systemctl.rs

1use std::process::{Command, Stdio};
2
3use anyhow::Result;
4use nix::unistd;
5
6fn systemctl(args: &[&str]) -> Result<()> {
7    let args = match unistd::geteuid().is_root() {
8        true => args.to_vec(),
9        false => [&["--user"], args].concat(),
10    };
11
12    Command::new("systemctl")
13        .args(args)
14        .stdout(Stdio::null())
15        .stderr(Stdio::null())
16        .status()?;
17
18    Ok(())
19}
20
21pub fn reload_daemon() -> Result<()> {
22    systemctl(&["daemon-reload"])
23}
24
25pub fn start() -> Result<()> {
26    systemctl(&["enable", "okey"])?;
27    systemctl(&["start", "okey"])
28}
29
30pub fn restart() -> Result<()> {
31    systemctl(&["restart", "okey"])
32}
33
34pub fn stop() -> Result<()> {
35    systemctl(&["stop", "okey"])?;
36    systemctl(&["disable", "okey"])
37}
38
39pub fn status() -> Result<()> {
40    let args = match unistd::geteuid().is_root() {
41        true => vec!["status", "okey"],
42        false => vec!["--user", "status", "okey"],
43    };
44
45    Command::new("systemctl").args(args).status()?;
46
47    Ok(())
48}