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.

47 lines
1.2 KiB

8 years ago
7 years ago
8 years ago
  1. use std::io::Result as IoResult;
  2. use std::process::Command;
  3. use std::process::Output;
  4. pub trait CommandRunner {
  5. fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult<Output>;
  6. }
  7. #[derive(Debug)]
  8. pub struct StdCommandRunner;
  9. impl CommandRunner for StdCommandRunner {
  10. fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult<Output> {
  11. // FIXME: logger
  12. println!("{} {:?}", program, args);
  13. let res = Command::new(program).args(args).output();
  14. println!("{:?}", res);
  15. res
  16. }
  17. }
  18. #[derive(Debug)]
  19. pub struct UserCommandRunner<'a, C> where C: 'a + CommandRunner {
  20. command_runner: &'a C,
  21. user_name: &'a str
  22. }
  23. impl<'a, C> UserCommandRunner<'a, C> where C: 'a + CommandRunner {
  24. pub fn new(user_name: &'a str, command_runner: &'a C) -> UserCommandRunner<'a, C> {
  25. UserCommandRunner {
  26. command_runner: command_runner,
  27. user_name: user_name
  28. }
  29. }
  30. }
  31. impl<'a, C> CommandRunner for UserCommandRunner<'a, C> where C: 'a + CommandRunner {
  32. fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult<Output> {
  33. let mut new_args = vec![self.user_name, "-s", "/usr/bin/env", "--", program];
  34. new_args.extend_from_slice(args);
  35. self.command_runner.run_with_args("su", &new_args)
  36. }
  37. }
  38. #[cfg(test)]
  39. mod test {
  40. }