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.

58 lines
1.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::path::Path;
  4. use command_runner::CommandRunner;
  5. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  6. pub struct GitSubmodules<'a, C: 'a + CommandRunner> {
  7. target: &'a str,
  8. command_runner: &'a C
  9. }
  10. impl<'a, C: CommandRunner> GitSubmodules<'a, C> {
  11. pub fn new(target: &'a str, command_runner: &'a C) -> Self {
  12. GitSubmodules { target, command_runner }
  13. }
  14. }
  15. impl<'a, C: CommandRunner> fmt::Display for GitSubmodules<'a, C> {
  16. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  17. write!(f, "Submodules for {}", self.target)
  18. }
  19. }
  20. impl<'a, C: CommandRunner> GitSubmodules<'a, C> {
  21. fn _run_in_target_repo(&self, args: &[&str]) -> Result<Vec<u8>, Box<Error>> {
  22. let mut new_args = vec!["-C", self.target];
  23. new_args.extend_from_slice(args);
  24. self.command_runner.get_output("git", &new_args)
  25. }
  26. }
  27. impl<'a, C: CommandRunner> Symbol for GitSubmodules<'a, C> {
  28. fn target_reached(&self) -> Result<bool, Box<Error>> {
  29. if !Path::new(self.target).exists() {
  30. return Ok(false);
  31. }
  32. let output = try!(String::from_utf8(try!(self._run_in_target_repo(&["submodule", "status"]))));
  33. Ok(output.lines().all(|line| line.is_empty() || 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. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  40. Box::new(SymbolAction::new(runner, self))
  41. }
  42. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  43. Box::new(OwnedSymbolAction::new(runner, *self))
  44. }
  45. }
  46. #[cfg(test)]
  47. mod test {
  48. }