xtask_toolkit/
linux_utils.rs

1pub struct LinuxUser(pub String);
2
3impl LinuxUser {
4    pub fn bash_add(&self) -> String {
5        format!(
6            r#"
7        if [ -z "$(getent passwd | grep {account})" ]; then 
8            useradd -r {account};
9        fi
10    "#,
11            account = &self.0
12        )
13    }
14
15    pub fn bash_remove(&self) -> String {
16        format!("userdel -r {};", &self.0)
17    }
18}
19
20#[derive(Debug)]
21pub struct SystemdUnit(pub String);
22
23impl SystemdUnit {
24    pub fn bash_reload_daemon() -> String {
25        format!("systemctl daemon-reload;")
26    }
27
28    pub fn bash_disable_and_stop(&self) -> String {
29        let unit = &self.0;
30        format!(
31            r#"
32            if [ $(systemctl list-unit-files {unit} &> /dev/null; echo $?) -eq 0 ]; then
33                systemctl disable {unit};
34                systemctl stop {unit};
35            fi;
36        "#
37        )
38    }
39
40    pub fn bash_restart_if_active(&self) -> String {
41        format!(
42            "systemctl is-active --quiet {} && systemctl restart {};",
43            &self.0, &self.0
44        )
45    }
46
47}