use crate::{models::config::Config, utils::paths::MakeDirs};
use std::path::Path;
#[derive(Debug, clap::Args)]
pub struct CleanCmd {
#[arg(short, long)]
dots: bool,
#[arg(short, long)]
backups: bool,
#[arg(short = 'y', long)]
assumeyes: bool,
}
impl super::RunWith<Config> for CleanCmd {
fn run_with(&self, config: &Config) -> anyhow::Result<()> {
if self.dots {
confirm_clean(
&format!(
" ~ {}. Continue?",
console::style("Cleaning dots").yellow().bold()
),
&config.dots_dir,
self.assumeyes,
)?
}
if self.backups {
confirm_clean(
&format!(
" ~ {}. Continue?",
console::style("Cleaning backups").yellow().bold()
),
&config.backup_dir,
self.assumeyes,
)?
}
Ok(())
}
}
fn confirm_clean(prompt: &str, path: &Path, assumeyes: bool) -> anyhow::Result<()> {
match assumeyes {
true => clean(path),
false => confirm(prompt, clean, path),
}
}
fn confirm(
prompt: &str,
func: impl Fn(&Path) -> anyhow::Result<()>,
path: &Path,
) -> anyhow::Result<()> {
if dialoguer::Confirm::new()
.with_prompt(prompt)
.interact()
.is_ok()
{
return func(path);
}
Ok(())
}
fn clean(dir: &Path) -> anyhow::Result<()> {
trash::delete(dir)?;
dir.make_all_dirs()?;
log::info!(
"🗑️ {} {}",
console::style("Cleaned").bold(),
console::style(dir.display()).bold().red()
);
Ok(())
}