Add cleaning up old repos to the clean command.
This commit is contained in:
@@ -8,7 +8,12 @@ use crate::config::Config;
|
|||||||
|
|
||||||
pub(crate) async fn run_clean(args: CleanArgs) -> Result<()> {
|
pub(crate) async fn run_clean(args: CleanArgs) -> Result<()> {
|
||||||
let config = Config::load_from_file(args.config).await?;
|
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 target_directory = config.get_target_root_directory()?;
|
||||||
let existing_target_names = {
|
let existing_target_names = {
|
||||||
let mut existing_entries = tokio::fs::read_dir(&target_directory).await?;
|
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(())
|
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::<Result<HashSet<_>>>()?;
|
||||||
|
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user