use std::io;
use std::path::PathBuf;
use clap::Args;
use super::{Format, PruneArgs};
use crate::docker::VolumeScope;
use crate::error::Result;
use crate::policy::parse_duration;
#[expect(
clippy::struct_excessive_bools,
reason = "these mirror command-line flags, which are bools by nature"
)]
#[derive(Debug, Args)]
#[command(group(clap::ArgGroup::new("worktree_removal").args(["remove_merged_worktrees", "aggressive"]).multiple(true)))]
pub struct GitPruneArgs {
#[arg(value_name = "PATH", default_value = ".")]
pub paths: Vec<PathBuf>,
#[arg(short = 'n', long)]
pub dry_run: bool,
#[arg(long)]
pub aggressive: bool,
#[arg(long, value_enum, default_value_t = Format::Human)]
pub format: Format,
#[arg(long)]
pub remotes: bool,
#[arg(long, value_name = "DURATION")]
pub timeout: Option<String>,
#[arg(long)]
pub remove_merged_worktrees: bool,
#[arg(long, requires = "worktree_removal")]
pub github_merged_worktrees: bool,
#[arg(long, value_name = "DURATION", requires = "worktree_removal")]
pub stale_worktrees: Option<String>,
#[arg(long, value_name = "TIME")]
pub expire: Option<String>,
}
impl GitPruneArgs {
pub fn to_git_options(&self, prune: &PruneArgs) -> Result<crate::git::GitPruneOptions> {
Ok(crate::git::GitPruneOptions {
roots: self.paths.clone(),
dry_run: self.dry_run,
jobs: prune.jobs,
one_file_system: prune.one_file_system,
timeout: self
.timeout
.as_deref()
.map(parse_duration)
.transpose()?
.unwrap_or(crate::git::DEFAULT_TIMEOUT),
exclude: crate::scan::PatternSet::new(prune.exclude.clone())?,
remotes: self.remotes,
worktree_expire: self
.expire
.clone()
.unwrap_or_else(|| crate::git::WORKTREE_PRUNE_EXPIRE.to_owned()),
count_objects: true,
})
}
}
pub fn render_git(
result: &crate::git::GitPruneResult,
args: &GitPruneArgs,
out: &mut impl io::Write,
) -> io::Result<()> {
match args.format {
Format::Human => crate::git::render_human(result, out),
Format::Json => crate::git::render_json(result, out),
}
}
#[expect(
clippy::struct_excessive_bools,
reason = "these mirror command-line flags, which are bools by nature"
)]
#[derive(Debug, Args)]
pub struct ClaudePruneArgs {
#[arg(value_name = "PATH")]
pub roots: Vec<PathBuf>,
#[arg(long)]
pub remove: bool,
#[arg(short = 'n', long)]
pub dry_run: bool,
#[arg(long, value_name = "DURATION")]
pub min_age: Option<String>,
#[arg(long, value_enum, default_value_t = Format::Human)]
pub format: Format,
#[arg(long, default_value_t = true, action = clap::ArgAction::Set)]
pub one_file_system: bool,
#[arg(long)]
pub force: bool,
}
impl ClaudePruneArgs {
pub fn to_claude_options(&self) -> Result<crate::claude::ClaudePruneOptions> {
Ok(crate::claude::ClaudePruneOptions {
roots: if self.roots.is_empty() {
crate::claude::default_root()
} else {
self.roots.clone()
},
min_age: self
.min_age
.as_deref()
.map(parse_duration)
.transpose()?
.unwrap_or(crate::claude::DEFAULT_MIN_AGE),
remove: self.remove,
dry_run: self.dry_run,
force: self.force,
one_file_system: self.one_file_system,
})
}
}
pub fn render_claude(
result: &crate::claude::ClaudePruneResult,
args: &ClaudePruneArgs,
out: &mut impl io::Write,
) -> io::Result<()> {
match args.format {
Format::Human => crate::claude::render_human(result, out),
Format::Json => crate::claude::render_json(result, out),
}
}
#[expect(
clippy::struct_excessive_bools,
reason = "these mirror command-line flags, which are bools by nature"
)]
#[derive(Debug, Args)]
pub struct DockerPruneArgs {
#[arg(short = 'n', long)]
pub dry_run: bool,
#[arg(long, value_enum, default_value_t = Format::Human)]
pub format: Format,
#[arg(long, value_name = "DURATION")]
pub max_age: Option<String>,
#[arg(long)]
pub volumes: bool,
#[arg(long)]
pub volumes_all: bool,
#[arg(long)]
pub no_trim: bool,
#[arg(long, value_name = "DURATION")]
pub timeout: Option<String>,
}
impl DockerPruneArgs {
pub fn to_docker_options(&self) -> Result<crate::docker::DockerPruneOptions> {
Ok(crate::docker::DockerPruneOptions {
dry_run: self.dry_run,
max_age: self
.max_age
.as_deref()
.map(parse_duration)
.transpose()?
.unwrap_or(crate::docker::DEFAULT_MAX_AGE),
volumes: volume_scope(self.volumes, self.volumes_all),
trim: !self.no_trim,
timeout: self
.timeout
.as_deref()
.map(parse_duration)
.transpose()?
.unwrap_or(crate::docker::DEFAULT_TIMEOUT),
})
}
}
#[must_use]
pub fn volume_scope(volumes: bool, all: bool) -> Option<VolumeScope> {
match (volumes, all) {
(_, true) => Some(VolumeScope::All),
(true, false) => Some(VolumeScope::Anonymous),
(false, false) => None,
}
}
pub fn render_docker(
result: &crate::docker::DockerPruneResult,
args: &DockerPruneArgs,
out: &mut impl io::Write,
) -> io::Result<()> {
match args.format {
Format::Human => crate::docker::render_human(result, out),
Format::Json => crate::docker::render_json(result, out),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_leave_volumes_alone_unless_a_flag_asks() {
assert_eq!(volume_scope(false, false), None);
}
#[test]
fn should_remove_only_anonymous_volumes_for_the_plain_flag() {
assert_eq!(volume_scope(true, false), Some(VolumeScope::Anonymous));
}
#[test]
fn should_let_the_all_flag_imply_volumes_and_win() {
assert_eq!(volume_scope(false, true), Some(VolumeScope::All));
assert_eq!(volume_scope(true, true), Some(VolumeScope::All));
}
}