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

8 years ago
8 years ago
5 years ago
8 years ago
5 years ago
5 years ago
5 years ago
7 years ago
8 years ago
5 years ago
7 years ago
7 years ago
5 years ago
8 years ago
8 years ago
5 years ago
5 years ago
8 years ago
5 years ago
5 years ago
8 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::symbols::Symbol;
  3. use std::error::Error;
  4. #[derive(Debug)]
  5. pub struct User<U, C> {
  6. user_name: U,
  7. command_runner: C,
  8. }
  9. impl<U, C> User<U, C> {
  10. pub fn new(user_name: U, command_runner: C) -> Self {
  11. Self {
  12. user_name,
  13. command_runner,
  14. }
  15. }
  16. }
  17. impl<U: AsRef<str>, C: CommandRunner> Symbol for User<U, C> {
  18. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  19. let output = self
  20. .command_runner
  21. .run_with_args("getent", args!["passwd", self.user_name.as_ref()])?;
  22. match output.status.code() {
  23. Some(2) => Ok(false),
  24. Some(0) => Ok(true),
  25. _ => Err("Unknown error".into()),
  26. }
  27. }
  28. fn execute(&self) -> Result<(), Box<dyn Error>> {
  29. self.command_runner.run_successfully(
  30. "adduser",
  31. args![
  32. // "-m", // Necessary for Fedora, not accepted in Debian
  33. "--system",
  34. self.user_name.as_ref(),
  35. ],
  36. )?;
  37. Ok(())
  38. }
  39. }
  40. #[cfg(test)]
  41. mod test {
  42. use crate::command_runner::StdCommandRunner;
  43. use crate::symbols::user::User;
  44. use crate::symbols::Symbol;
  45. #[test]
  46. fn test_target_reached_nonexisting() {
  47. let symbol = User {
  48. user_name: "nonexisting",
  49. command_runner: StdCommandRunner,
  50. };
  51. assert_eq!(symbol.target_reached().unwrap(), false);
  52. }
  53. #[test]
  54. fn test_target_reached_root() {
  55. let symbol = User {
  56. user_name: "root",
  57. command_runner: StdCommandRunner,
  58. };
  59. assert_eq!(symbol.target_reached().unwrap(), true);
  60. }
  61. }