prune: add 'pkh prune' command
CI / build (push) Successful in 15m13s
CI / snap (push) Successful in 2m13s

This commit is contained in:
2026-08-14 10:28:56 +02:00
parent 09cbf56ebc
commit ec77bc6d4d
3 changed files with 687 additions and 0 deletions
+72
View File
@@ -101,6 +101,26 @@ fn main() {
.arg(arg!(<name> "Context name"))
)
)
.subcommand(
Command::new("prune")
.about("Prune residual pkh build artifacts and caches")
// NOTE: --dry-run is defined via the builder API because clap's
// `arg!` macro mis-tokenizes hyphenated long names (it would
// parse `--dry-run` as long="dry" plus a spurious short flag,
// tripping the "Short flags should precede long flags" assert).
.arg(
clap::Arg::new("dry_run")
.long("dry-run")
.action(clap::ArgAction::SetTrue)
.help("List what would be removed without removing anything"),
)
.arg(
clap::Arg::new("all")
.long("all")
.action(clap::ArgAction::SetTrue)
.help("Also remove cached chroot tarballs (expensive to re-download)"),
)
)
.get_matches();
match matches.subcommand() {
@@ -359,6 +379,58 @@ fn main() {
_ => unreachable!(),
}
}
Some(("prune", sub_matches)) => {
let dry_run = sub_matches
.get_one::<bool>("dry_run")
.copied()
.unwrap_or(false);
let all = sub_matches.get_one::<bool>("all").copied().unwrap_or(false);
let options = pkh::prune::PruneOptions { dry_run, all };
match pkh::prune::prune(options) {
Ok(report) => {
if report.is_empty() {
info!("Nothing to prune.");
} else {
let action = if report.dry_run {
"Would remove"
} else {
"Removed"
};
for path in &report.removed {
info!("{} {}", action, path.display());
}
for (path, err) in &report.failed {
error!("Failed to remove {}: {}", path.display(), err);
}
if report.dry_run {
info!(
"(dry run) {} item(s) would be removed.",
report.removed.len()
);
} else {
info!(
"Pruned {} item(s){} ({} failure(s)).",
report.removed.len(),
if all {
" including cached chroot tarballs"
} else {
""
},
report.failed.len()
);
}
}
if !report.failed.is_empty() {
std::process::exit(1);
}
}
Err(e) => {
error!("{}", e);
std::process::exit(1);
}
}
}
_ => unreachable!("Exhausted list of subcommands and subcommand_required prevents `None`"),
}
}