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.

50 lines
1.3 KiB

7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::io;
  4. use command_runner::CommandRunner;
  5. use symbols::Symbol;
  6. pub struct GitSubmodules<'a> {
  7. target: &'a str,
  8. command_runner: &'a CommandRunner
  9. }
  10. impl<'a> GitSubmodules<'a> {
  11. pub fn new(target: &'a str, command_runner: &'a CommandRunner) -> GitSubmodules<'a> {
  12. GitSubmodules {
  13. target: target,
  14. command_runner: command_runner
  15. }
  16. }
  17. }
  18. impl<'a> fmt::Display for GitSubmodules<'a> {
  19. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  20. write!(f, "Submodules for {}", self.target)
  21. }
  22. }
  23. impl<'a> GitSubmodules<'a> {
  24. fn _run_in_target_repo(&self, args: &[&str]) -> Result<Vec<u8>, io::Error> {
  25. let mut new_args = vec!["-C", self.target];
  26. new_args.extend_from_slice(args);
  27. self.command_runner.run_with_args("git", &new_args).map(|res| res.stdout)
  28. }
  29. }
  30. impl<'a> Symbol for GitSubmodules<'a> {
  31. fn target_reached(&self) -> Result<bool, Box<Error>> {
  32. let output = try!(self._run_in_target_repo(&["submodule", "status"]));
  33. Ok(String::from_utf8(output).unwrap().lines().all(|line| line.len() == 0 || line.starts_with(' ')))
  34. }
  35. fn execute(&self) -> Result<(), Box<Error>> {
  36. try!(self._run_in_target_repo(&["submodule", "update", "--init"]));
  37. Ok(())
  38. }
  39. }
  40. #[cfg(test)]
  41. mod test {
  42. }