1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 17:14:08 +02:00

improve cleanup of leftover networks and workdirs

This commit is contained in:
Robin Appelman 2026-02-09 17:30:41 +01:00
commit 755573ba4b
2 changed files with 32 additions and 9 deletions

View file

@ -17,7 +17,7 @@ use bollard::Docker;
use itertools::Itertools;
use miette::{IntoDiagnostic, Report, Result, WrapErr};
use std::env::{var, vars};
use std::fs::{create_dir_all, write};
use std::fs::{create_dir_all, remove_dir_all, write};
use std::io::stdout;
use std::os::unix::process::CommandExt;
use std::process::{Command, ExitCode};
@ -66,12 +66,33 @@ async fn main() -> Result<ExitCode> {
match args {
HazeArgs::Clean => {
let list = Cloud::list(&docker, None, &config).await?;
for cloud in list.into_iter().filter(|cloud| !cloud.pinned) {
let (retain, remove) = list
.into_iter()
.partition::<Vec<_>, _>(|cloud| cloud.pinned);
for cloud in remove {
if let Err(e) = cloud.destroy(&docker).await {
eprintln!("Error while removing cloud: {:#}", e);
}
}
clear_networks(&docker).await?;
clear_networks(&docker, &retain).await?;
for cache_dir in config.work_dir.read_dir().into_diagnostic()? {
let cache_dir = cache_dir.into_diagnostic()?;
if let Some(id) = cache_dir
.file_name()
.to_str()
.and_then(|name| name.strip_prefix("haze-"))
{
if !retain.iter().any(|cloud| cloud.id == id) {
let path = cache_dir.path();
remove_dir_all(&path).into_diagnostic().wrap_err_with(|| {
format!("Failed to cleanup stray workdir: {}", path.display())
})?;
}
}
}
}
HazeArgs::List { filter } => {
let list = Cloud::list(&docker, filter, &config).await?;