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.

61 lines
1.6 KiB

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
7 years ago
7 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> {
  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>, Box<Error>> {
  25. let mut new_args = vec!["-C", self.target];
  26. new_args.extend_from_slice(args);
  27. self.command_runner.get_output("git", &new_args)
  28. }
  29. }
  30. impl<'a> Symbol for GitSubmodules<'a> {
  31. fn target_reached(&self) -> Result<bool, Box<Error>> {
  32. if !Path::new(self.target).exists() {
  33. return Ok(false);
  34. }
  35. let output = try!(String::from_utf8(try!(self._run_in_target_repo(&["submodule", "status"]))));
  36. Ok(output.lines().all(|line| line.len() == 0 || line.starts_with(' ')))
  37. }
  38. fn execute(&self) -> Result<(), Box<Error>> {
  39. try!(self._run_in_target_repo(&["submodule", "update", "--init"]));
  40. Ok(())
  41. }
  42. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  43. Box::new(SymbolAction::new(runner, self))
  44. }
  45. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  46. Box::new(OwnedSymbolAction::new(runner, *self))
  47. }
  48. }
  49. #[cfg(test)]
  50. mod test {
  51. }