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.

67 lines
1.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 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 {
  13. target,
  14. command_runner,
  15. }
  16. }
  17. }
  18. impl<'a, C: CommandRunner> fmt::Display for GitSubmodules<'a, C> {
  19. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  20. write!(f, "Submodules for {}", self.target)
  21. }
  22. }
  23. impl<'a, C: CommandRunner> GitSubmodules<'a, C> {
  24. fn _run_in_target_repo(&self, args: &[&str]) -> Result<Vec<u8>, Box<dyn 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, C: CommandRunner> Symbol for GitSubmodules<'a, C> {
  31. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  32. if !Path::new(self.target).exists() {
  33. return Ok(false);
  34. }
  35. let output = String::from_utf8(self._run_in_target_repo(&["submodule", "status"])?)?;
  36. Ok(
  37. output
  38. .lines()
  39. .all(|line| line.is_empty() || line.starts_with(' ')),
  40. )
  41. }
  42. fn execute(&self) -> Result<(), Box<dyn Error>> {
  43. self._run_in_target_repo(&["submodule", "update", "--init"])?;
  44. Ok(())
  45. }
  46. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  47. Box::new(SymbolAction::new(runner, self))
  48. }
  49. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  50. where
  51. Self: 'b,
  52. {
  53. Box::new(OwnedSymbolAction::new(runner, *self))
  54. }
  55. }
  56. #[cfg(test)]
  57. mod test {}