xtask_toolkit/
linux_utils.rs1use std::path::{Path, PathBuf};
2
3pub struct LinuxUser(pub String);
4
5impl LinuxUser {
6 pub fn bash_add(&self) -> String {
7 format!(
8 r#"
9 if [ -z "$(getent passwd | grep {account})" ]; then
10 useradd -r {account};
11 fi
12 "#,
13 account = &self.0
14 )
15 }
16
17 pub fn bash_remove(&self) -> String {
18 format!("userdel -r {};", &self.0)
19 }
20}
21
22#[derive(Debug)]
23pub struct SystemdUnit(pub String);
24
25impl TryFrom<&Path> for SystemdUnit
26{
27 type Error = ();
28
29 fn try_from(value: &Path) -> Result<Self, Self::Error> {
30 Ok(Self(
31 value
32 .file_name()
33 .ok_or(())?
34 .to_string_lossy()
35 .to_string(),
36 ))
37 }
38}
39
40impl TryFrom<&PathBuf> for SystemdUnit
41{
42 type Error = ();
43
44 fn try_from(value: &PathBuf) -> Result<Self, Self::Error> {
45 Self::try_from(value.as_path())
46 }
47}
48
49impl SystemdUnit {
50 pub fn bash_reload_daemon() -> String {
51 format!("systemctl daemon-reload;")
52 }
53
54 pub fn bash_disable_and_stop(&self) -> String {
55 let unit = &self.0;
56 format!(
57 r#"
58 if [ $(systemctl list-unit-files {unit} &> /dev/null; echo $?) -eq 0 ]; then
59 systemctl disable {unit};
60 systemctl stop {unit};
61 fi;
62 "#
63 )
64 }
65
66 pub fn bash_restart_if_active(&self) -> String {
67 format!(
68 "systemctl is-active --quiet {} && systemctl restart {};",
69 &self.0, &self.0
70 )
71 }
72}