From 667bab1bef06e4376e912c53630c0f5690cf592e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 3 Sep 2026 22:04:25 -0400 Subject: [PATCH] Add cleaning up old repos to the clean command. --- src/command/clean/runner.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/command/clean/runner.rs b/src/command/clean/runner.rs index 2b2c348..1251996 100644 --- a/src/command/clean/runner.rs +++ b/src/command/clean/runner.rs @@ -8,7 +8,12 @@ use crate::config::Config; pub(crate) async fn run_clean(args: CleanArgs) -> Result<()> { let config = Config::load_from_file(args.config).await?; + clean_targets(&config).await?; + clean_repos(&config).await?; + Ok(()) +} +async fn clean_targets(config: &Config) -> Result<()> { let target_directory = config.get_target_root_directory()?; let existing_target_names = { let mut existing_entries = tokio::fs::read_dir(&target_directory).await?; @@ -41,3 +46,35 @@ pub(crate) async fn run_clean(args: CleanArgs) -> Result<()> { Ok(()) } + +async fn clean_repos(config: &Config) -> Result<()> { + let repo_directory = config.get_repo_root_directory()?; + let existing_repo_names = { + let mut existing_entries = tokio::fs::read_dir(&repo_directory).await?; + let mut existing_repo_names = HashSet::new(); + while let Some(existing_repo) = existing_entries.next_entry().await? { + existing_repo_names.insert(existing_repo.file_name()); + } + existing_repo_names + }; + + let configured_repo_names = config + .get_target_configs() + .iter() + .map(|target| { + target.get_repo_directory(&config).and_then(|repo_path| { + repo_path + .file_name() + .map(|name| name.to_os_string()) + .ok_or("Entry with no file name in repo directory.".into()) + }) + }) + .collect::>>()?; + + for dir in existing_repo_names.difference(&configured_repo_names) { + info!("DELETE REPO: {:?}", dir); + tokio::fs::remove_dir_all(repo_directory.join(dir)).await?; + } + + Ok(()) +}