rotate_backup 0.1.2

Rotate you backups easily
Documentation
use clap::Parser;
use rotate_backup::execute::rotate::RotateOutputType;
use rotate_backup::execute::{MainCommand, SubCommands};
use std::io::Read;
use std::path::PathBuf;

fn main() -> anyhow::Result<()> {
    let mut params = MainCommand::parse();

    match &mut params.sub_commands {
        SubCommands::Rotate(rotate) => {
            if rotate.files.is_none() || rotate.files.as_ref().unwrap().is_empty() {
                let mut input = String::new();
                std::io::stdin().lock().read_to_string(&mut input)?;
                let files = shlex::split(&input).ok_or_else(|| {
                    anyhow::anyhow!("Expected files names as input, but received: {input}")
                })?;
                rotate.files = Some(files.into_iter().map(PathBuf::from).collect())
            }

            let mut out = rotate_backup::execute::rotate::rotate(
                rotate.files.as_ref().unwrap(),
                rotate.base.unwrap_or_default(),
                rotate.format.as_deref().unwrap_or("%Y-%m-%d"),
            )?;
            let to_print = match rotate.output {
                RotateOutputType::Keep => &mut out.keep,
                RotateOutputType::Delete => &mut out.delete,
            };
            if let Some(path) = to_print.pop() {
                print!("{path:?}")
            }
            for out in to_print {
                print!(" {out:?}")
            }
            println!();
        }
        SubCommands::Debug(debug) => {
            let base = debug.base.unwrap_or_default();
            let starts =
                rotate_backup::execute::debug::debug_partitions(base, debug.partition_count);
            println!(
                "\x1b[1mShowing {} partitions with exponential base {}\x1b[m\n\
                \x1b[2mRanges represents the file age in days since oldest backup\n\
                Only 1 file is kept per partition\x1b[m\n\
                ------------------------------------------------\n\
                \x1b[1mNo | Start..End\x1b[m",
                debug.partition_count, base
            );

            for (i, start) in starts.windows(2).enumerate() {
                let color = if i % 2 == 0 {
                    "\x1b[m"
                } else {
                    "\x1b[38;5;235m"
                };
                println!("{color}{i:03}| {:05}..{:05}", start[0], start[1])
            }
        }
    }

    Ok(())
}