use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone, Default)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum SystemdRunUser {
Root {
#[serde(default = "crate::utils::yes", skip_serializing_if = "crate::utils::is_true")]
preserve_current_cache: bool,
},
#[default]
CurrentUser,
Another {
username: String,
#[serde(default = "crate::utils::yes", skip_serializing_if = "crate::utils::is_true")]
preserve_current_cache: bool,
},
}
impl SystemdRunUser {
pub fn preserve_current_cache_rule(&self) -> bool {
match self {
Self::CurrentUser => false,
Self::Root { preserve_current_cache } => *preserve_current_cache,
Self::Another {
preserve_current_cache, ..
} => *preserve_current_cache,
}
}
pub fn user_name_rule(&self) -> anyhow::Result<String> {
match self {
Self::Root { .. } => Ok(String::new()),
Self::Another { username, .. } => Ok(format!("User={username}")),
Self::CurrentUser => {
let user_uid = nix::unistd::getuid();
let user = nix::unistd::User::from_uid(user_uid)?.expect("Can't get current user name by UID!");
Ok(format!("User={}", user.name))
}
}
}
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone)]
pub struct SystemdOpts {
#[serde(default = "crate::utils::yes", skip_serializing_if = "crate::utils::is_true")]
pub use_defined_shell: bool,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub cli_options: String,
#[serde(default)]
pub user: SystemdRunUser,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub after: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub restart_interval: Option<String>,
}
impl Default for SystemdOpts {
fn default() -> Self {
Self {
use_defined_shell: true,
user: SystemdRunUser::default(),
cli_options: Default::default(),
after: Default::default(),
restart_interval: None,
}
}
}