Add a command for cleaning up old targets.
This commit is contained in:
3
src/command/clean/mod.rs
Normal file
3
src/command/clean/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod runner;
|
||||
|
||||
pub(crate) use runner::run_clean;
|
||||
43
src/command/clean/runner.rs
Normal file
43
src/command/clean/runner.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use tracing::info;
|
||||
|
||||
use crate::Result;
|
||||
use crate::cli::parameters::CleanArgs;
|
||||
use crate::config::Config;
|
||||
|
||||
pub(crate) async fn run_clean(args: CleanArgs) -> Result<()> {
|
||||
let config = Config::load_from_file(args.config).await?;
|
||||
|
||||
let target_directory = config.get_target_root_directory()?;
|
||||
let existing_target_names = {
|
||||
let mut existing_entries = tokio::fs::read_dir(&target_directory).await?;
|
||||
let mut existing_target_names = HashSet::new();
|
||||
while let Some(existing_target) = existing_entries.next_entry().await? {
|
||||
existing_target_names.insert(existing_target.file_name());
|
||||
}
|
||||
existing_target_names
|
||||
};
|
||||
|
||||
let configured_target_names = config
|
||||
.get_target_configs()
|
||||
.iter()
|
||||
.map(|target| {
|
||||
target
|
||||
.get_target_directory(&config)
|
||||
.and_then(|target_path| {
|
||||
target_path
|
||||
.file_name()
|
||||
.map(|name| name.to_os_string())
|
||||
.ok_or("Entry with no file name in target directory.".into())
|
||||
})
|
||||
})
|
||||
.collect::<Result<HashSet<_>>>()?;
|
||||
|
||||
for dir in existing_target_names.difference(&configured_target_names) {
|
||||
info!("DELETE TARGET: {:?}", dir);
|
||||
tokio::fs::remove_dir_all(target_directory.join(dir)).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub(crate) mod build;
|
||||
pub(crate) mod clean;
|
||||
pub(crate) mod daemon;
|
||||
pub(crate) mod feed_logs;
|
||||
|
||||
Reference in New Issue
Block a user