cleanux 0.1.0

Linux cleaner CLI (CleanMyMac-like): scan & safely trash cache, find duplicates, list large files
Documentation
pub mod cleaners;
pub mod cli;
pub mod config;
pub mod features;
pub mod fsutil;
pub mod model;

use anyhow::Result;
use bytesize::ByteSize;
use cli::{Cli, Command, Target};
use dialoguer::Confirm;
use std::path::PathBuf;

pub fn scan_target(target: Target, excludes: &[PathBuf]) -> Result<model::Plan> {
    match target {
        Target::Cache => cleaners::xdg_cache::scan_cache(excludes),
    }
}

pub fn print_plan(plan: &model::Plan) {
    println!("Items: {}", plan.items.len());
    println!("Total: {}", ByteSize(plan.total_bytes));
    for item in &plan.items {
        println!("  {:>10}  {}", ByteSize(item.bytes), item.path.display());
    }
}

pub fn run(args: Cli) -> Result<()> {
    let cfg = config::load_config()?;

    match args.cmd {
        Command::Scan { target } => {
            let plan = scan_target(target, &cfg.exclude_prefixes)?;
            print_plan(&plan);
        }
        Command::Clean { target } => {
            let plan = scan_target(target, &cfg.exclude_prefixes)?;
            print_plan(&plan);

            if !args.apply {
                println!("\n(dry-run) Add --apply to actually move items to Trash.");
                return Ok(());
            }

            if !args.yes {
                let ok = confirm_proceed()?;
                if !ok {
                    println!("Cancelled.");
                    return Ok(());
                }
            }

            let paths: Vec<_> = plan.items.iter().map(|i| i.path.clone()).collect();
            if paths.is_empty() {
                println!("Nothing to clean.");
                return Ok(());
            }

            trash::delete_all(&paths)?;
            println!("Done. Moved {} items to Trash.", paths.len());
        }
        Command::Dups { path, trash } => {
            features::dups::run_dups(&path, trash)?;
        }
        Command::Large { path, top } => {
            features::large::run_large(&path, top)?;
        }
    }

    Ok(())
}

fn confirm_proceed() -> Result<bool> {
    if let Ok(choice) = std::env::var("CLEANUX_CONFIRM") {
        match choice.as_str() {
            "yes" => return Ok(true),
            "no" => return Ok(false),
            _ => {}
        }
    }

    match Confirm::new()
        .with_prompt("Proceed to move these items to Trash?")
        .default(false)
        .interact()
    {
        Ok(ok) => Ok(ok),
        Err(dialoguer::Error::IO(err)) if err.kind() == std::io::ErrorKind::NotConnected => {
            Ok(false)
        }
        Err(err) => Err(err.into()),
    }
}