use crate::command_runner::CommandRunner; use crate::symbols::Symbol; use std::error::Error; #[derive(Debug)] pub struct User { user_name: U, command_runner: C, } impl User { pub fn new(user_name: U, command_runner: C) -> Self { Self { user_name, command_runner, } } } impl, C: CommandRunner> Symbol for User { fn target_reached(&self) -> Result> { 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> { 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); } }