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.

68 lines
1.5 KiB

use crate::command_runner::CommandRunner;
use crate::symbols::Symbol;
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,
}
}
}
impl<U: AsRef<str>, C: CommandRunner> Symbol for User<U, C> {
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()])?;
match output.status.code() {
Some(2) => Ok(false),
Some(0) => Ok(true),
_ => Err("Unknown error".into()),
}
}
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(),
],
)?;
Ok(())
}
}
#[cfg(test)]
mod test {
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!(symbol.target_reached().unwrap(), false);
}
#[test]
fn test_target_reached_root() {
let symbol = User {
user_name: "root",
command_runner: StdCommandRunner,
};
assert_eq!(symbol.target_reached().unwrap(), true);
}
}