diff --git a/src/clean.rs b/src/clean.rs new file mode 100644 index 0000000..f5b5dcd --- /dev/null +++ b/src/clean.rs @@ -0,0 +1,63 @@ +use crate::cloud::Cloud; +use crate::config::HazeConfig; +use crate::git::prune_worktrees; +use crate::network::clear_networks; +use bollard::Docker; +use miette::{IntoDiagnostic, Result, WrapErr}; +use std::fs::{remove_dir_all, remove_file}; + +pub async fn clean(docker: &Docker, config: &HazeConfig) -> Result<()> { + let list = Cloud::list(docker, None, config).await?; + let mut retain = Vec::new(); + let mut remove = Vec::new(); + for cloud in list { + if cloud.is_pinned(&docker).await.unwrap_or(false) { + retain.push(cloud); + } else { + remove.push(cloud); + } + } + + for cloud in remove { + if let Err(e) = cloud.destroy(&docker, &config).await { + eprintln!("Error while removing cloud: {:#}", e); + } + } + + clear_networks(docker, &retain).await?; + + for cache_dir in config + .work_dir + .read_dir() + .into_diagnostic() + .wrap_err_with(|| format!("Failed to read dir {}", config.work_dir))? + { + let cache_dir = cache_dir + .into_diagnostic() + .wrap_err_with(|| "Failed to unwrap cache_dir")?; + if let Some(id) = cache_dir.file_name().to_str() + && id.starts_with("haze-") + && !retain.iter().any(|cloud| cloud.id == id) + { + let path = cache_dir.path(); + // By default, the worktrees live within the work dir, we do not want to delete it here. + if path == config.worktree_dir { + continue; + } + + if path.is_dir() { + remove_dir_all(&path).into_diagnostic().wrap_err_with(|| { + format!("Failed to cleanup stray cache dir: {}", path.display()) + })?; + } else { + remove_file(&path).into_diagnostic().wrap_err_with(|| { + format!("Failed to cleanup stray cache file: {}", path.display()) + })?; + } + } + } + + prune_worktrees(config)?; + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 2271d5c..08ceaba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,14 @@ extern crate core; use crate::args::{ExecService, GitOperation, HazeArgs}; +use crate::clean::clean; use crate::cloud::{Cloud, CloudOptions}; use crate::config::HazeConfig; use crate::database::DatabaseFamily; use crate::exec::{container_logs, exec, exec_tty}; -use crate::git::{checkout_all, prune_worktrees, pull_all}; +use crate::git::{checkout_all, pull_all}; use crate::help::help; use crate::image::update_image; -use crate::network::clear_networks; use crate::php::PhpVersion; use crate::proxy::proxy; use crate::script::run_script; @@ -21,7 +21,7 @@ use itertools::Itertools; use miette::{IntoDiagnostic, Report, Result, WrapErr}; use std::collections::HashMap; use std::env::{var, vars}; -use std::fs::{create_dir_all, remove_dir_all, remove_file, write}; +use std::fs::{create_dir_all, write}; use std::io::stdout; use std::os::unix::process::CommandExt; use std::pin::pin; @@ -30,6 +30,7 @@ use std::time::Duration; use tokio::time::sleep; mod args; +mod clean; mod cloud; mod config; mod database; @@ -74,57 +75,7 @@ async fn main() -> Result { match args { HazeArgs::Clean => { - let list = Cloud::list(&docker, None, &config).await?; - let mut retain = Vec::new(); - let mut remove = Vec::new(); - for cloud in list { - if cloud.is_pinned(&docker).await.unwrap_or(false) { - retain.push(cloud); - } else { - remove.push(cloud); - } - } - - for cloud in remove { - if let Err(e) = cloud.destroy(&docker, &config).await { - eprintln!("Error while removing cloud: {:#}", e); - } - } - - clear_networks(&docker, &retain).await?; - - for cache_dir in config - .work_dir - .read_dir() - .into_diagnostic() - .wrap_err_with(|| format!("Failed to read dir {}", config.work_dir))? - { - let cache_dir = cache_dir - .into_diagnostic() - .wrap_err_with(|| "Failed to unwrap cache_dir")?; - if let Some(id) = cache_dir.file_name().to_str() - && id.starts_with("haze-") - && !retain.iter().any(|cloud| cloud.id == id) - { - let path = cache_dir.path(); - // By default, the worktrees live within the work dir, we do not want to delete it here. - if path == config.worktree_dir { - continue; - } - - if path.is_dir() { - remove_dir_all(&path).into_diagnostic().wrap_err_with(|| { - format!("Failed to cleanup stray cache dir: {}", path.display()) - })?; - } else { - remove_file(&path).into_diagnostic().wrap_err_with(|| { - format!("Failed to cleanup stray cache file: {}", path.display()) - })?; - } - } - } - - prune_worktrees(&config).wrap_err_with(|| "Failed to prune worktree")?; + clean(&docker, &config).await?; } HazeArgs::List { filter } => { let list = Cloud::list(&docker, filter, &config).await?;