use crate::command_runner::CommandRunner; use crate::symbols::Symbol; use async_trait::async_trait; use std::error::Error; use std::ffi::OsStr; use std::fmt; use std::path::Path; #[derive(Debug)] pub struct GitSubmodules<'a, P, C> { target: P, command_runner: &'a C, } impl<'a, P, C> GitSubmodules<'a, P, C> { pub const fn new(target: P, command_runner: &'a C) -> Self { Self { target, command_runner, } } } impl, C: CommandRunner> fmt::Display for GitSubmodules<'_, P, C> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Submodules for {}", self.target.as_ref().display()) } } impl, C: CommandRunner> GitSubmodules<'_, P, C> { async fn _run_in_target_repo(&self, args: &[&OsStr]) -> Result, Box> { let mut new_args: Vec<&OsStr> = vec![]; new_args.extend_from_slice(args!["-C", self.target.as_ref()]); new_args.extend_from_slice(args); self.command_runner.get_output("git", &new_args).await } } #[async_trait(?Send)] impl, C: CommandRunner> Symbol for GitSubmodules<'_, P, C> { async fn target_reached(&self) -> Result> { if !self.target.as_ref().exists() { return Ok(false); } let output = String::from_utf8( self ._run_in_target_repo(args!["submodule", "status"]) .await?, )?; Ok( output .lines() .all(|line| line.is_empty() || line.starts_with(' ')), ) } async fn execute(&self) -> Result<(), Box> { self ._run_in_target_repo(args!["submodule", "update", "--init"]) .await?; Ok(()) } } #[cfg(test)] mod test {}