1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-11-30 20:50:49 +01:00

chore(clean): Move clean logic into its own file

Signed-off-by: Louis Chmn <louis@chmn.me>
This commit is contained in:
Louis Chmn 2026-11-07 16:28:33 +01:00
commit 971148b124
2 changed files with 68 additions and 54 deletions

63
src/clean.rs Normal file
View file

@ -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(())
}

View file

@ -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<ExitCode> {
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?;