A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.6 KiB

use crate::command_runner::CommandRunner;
use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
#[derive(Debug)]
pub struct User<U, C> {
user_name: U,
command_runner: C,
}
impl<U, C> User<U, C> {
pub fn new(user_name: U, command_runner: C) -> Self {
Self {
user_name,
command_runner,
}
}
}
#[async_trait(?Send)]
impl<U: AsRef<str>, C: CommandRunner> Symbol for User<U, C> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let output = self
.command_runner
.run_with_args("getent", args!["passwd", self.user_name.as_ref()])
.await?;
match output.status.code() {
Some(2) => Ok(false),
Some(0) => Ok(true),
_ => Err("Unknown error".into()),
}
}
async fn execute(&self) -> Result<(), Box<dyn Error>> {
self
.command_runner
.run_successfully(
"adduser",
args![
// "-m", // Necessary for Fedora, not accepted in Debian
"--system",
self.user_name.as_ref(),
],
)
.await
}
}
#[cfg(test)]
mod test {
use crate::async_utils::run;
use crate::command_runner::StdCommandRunner;
use crate::symbols::user::User;
use crate::symbols::Symbol;
#[test]
fn test_target_reached_nonexisting() {
let symbol = User {
user_name: "nonexisting",
command_runner: StdCommandRunner,
};
assert_eq!(run(symbol.target_reached()).unwrap(), false);
}
#[test]
fn test_target_reached_root() {
let symbol = User {
user_name: "root",
command_runner: StdCommandRunner,
};
assert_eq!(run(symbol.target_reached()).unwrap(), true);
}
}