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.

66 lines
1.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
4 years ago
5 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
5 years ago
7 years ago
7 years ago
5 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::symbols::Symbol;
  3. use async_trait::async_trait;
  4. use std::error::Error;
  5. use std::ffi::OsStr;
  6. use std::fmt;
  7. use std::path::Path;
  8. #[derive(Debug)]
  9. pub struct GitSubmodules<'a, P, C> {
  10. target: P,
  11. command_runner: &'a C,
  12. }
  13. impl<'a, P, C> GitSubmodules<'a, P, C> {
  14. pub const fn new(target: P, command_runner: &'a C) -> Self {
  15. Self {
  16. target,
  17. command_runner,
  18. }
  19. }
  20. }
  21. impl<P: AsRef<Path>, C: CommandRunner> fmt::Display for GitSubmodules<'_, P, C> {
  22. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  23. write!(f, "Submodules for {}", self.target.as_ref().display())
  24. }
  25. }
  26. impl<P: AsRef<Path>, C: CommandRunner> GitSubmodules<'_, P, C> {
  27. async fn _run_in_target_repo(&self, args: &[&OsStr]) -> Result<Vec<u8>, Box<dyn Error>> {
  28. let mut new_args: Vec<&OsStr> = vec![];
  29. new_args.extend_from_slice(args!["-C", self.target.as_ref()]);
  30. new_args.extend_from_slice(args);
  31. self.command_runner.get_output("git", &new_args).await
  32. }
  33. }
  34. #[async_trait(?Send)]
  35. impl<P: AsRef<Path>, C: CommandRunner> Symbol for GitSubmodules<'_, P, C> {
  36. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  37. if !self.target.as_ref().exists() {
  38. return Ok(false);
  39. }
  40. let output = String::from_utf8(
  41. self
  42. ._run_in_target_repo(args!["submodule", "status"])
  43. .await?,
  44. )?;
  45. Ok(
  46. output
  47. .lines()
  48. .all(|line| line.is_empty() || line.starts_with(' ')),
  49. )
  50. }
  51. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  52. self
  53. ._run_in_target_repo(args!["submodule", "update", "--init"])
  54. .await?;
  55. Ok(())
  56. }
  57. }
  58. #[cfg(test)]
  59. mod test {}