mod cli;
use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use pimalaya_cli::{
clap::{
args::{JsonFlag, LogFlags},
commands::{CompletionCommand, ManualCommand},
},
error::ErrorReport,
log::Logger,
long_version,
printer::{Printer, StdoutPrinter},
};
use crate::cli::{
StoreFlags, check::CheckCommand, collection::CollectionCommand, export::ExportCommand,
gc::GcCommand, item::ItemCommand, queue::QueueCommand, store::StoreCommand,
};
fn main() {
let cli = Cli::parse();
Logger::try_init(&cli.log).expect("init logger");
let mut printer = StdoutPrinter::new(&cli.json);
let result = cli.command.execute(&mut printer, &cli.store);
ErrorReport::eval(&mut printer, result)
}
#[derive(Parser, Debug)]
#[command(name = env!("CARGO_BIN_NAME"))]
#[command(about = "CLI to inspect, repair and recover a pimdir store")]
#[command(author, version, long_version = long_version!())]
#[command(propagate_version = true, infer_subcommands = true)]
struct Cli {
#[command(subcommand)]
pub command: Command,
#[command(flatten)]
pub store: StoreFlags,
#[command(flatten)]
pub log: LogFlags,
#[command(flatten)]
pub json: JsonFlag,
}
#[derive(Subcommand, Debug)]
enum Command {
#[command(subcommand)]
Collection(CollectionCommand),
#[command(subcommand)]
Item(ItemCommand),
#[command(subcommand)]
Queue(QueueCommand),
#[command(subcommand)]
Store(StoreCommand),
Check(CheckCommand),
Gc(GcCommand),
Export(ExportCommand),
Completions(CompletionCommand),
Manuals(ManualCommand),
}
impl Command {
pub fn execute(self, printer: &mut impl Printer, store: &StoreFlags) -> Result<()> {
match self {
Self::Collection(cmd) => cmd.execute(printer, store),
Self::Item(cmd) => cmd.execute(printer, store),
Self::Queue(cmd) => cmd.execute(printer, store),
Self::Store(cmd) => cmd.execute(printer, store),
Self::Check(cmd) => cmd.execute(printer, store),
Self::Gc(cmd) => cmd.execute(printer, store),
Self::Export(cmd) => cmd.execute(printer, store),
Self::Completions(cmd) => cmd.execute(printer, Cli::command()),
Self::Manuals(cmd) => cmd.execute(printer, Cli::command()),
}
}
}