use crate::{
Application, RUSTIC_APP,
repository::{IndexedRepo, OpenRepo, get_snapots_from_ids},
status_err,
};
use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::Result;
use rustic_core::{RepairIndexOptions, RepairSnapshotsOptions};
#[derive(clap::Parser, Command, Debug)]
pub(crate) struct RepairCmd {
#[clap(subcommand)]
cmd: RepairSubCmd,
}
#[derive(clap::Subcommand, Debug, Runnable)]
enum RepairSubCmd {
Index(IndexSubCmd),
Snapshots(SnapSubCmd),
}
#[derive(Default, Debug, clap::Parser, Command)]
struct IndexSubCmd {
#[clap(flatten)]
opts: RepairIndexOptions,
}
#[derive(Default, Debug, clap::Parser, Command)]
struct SnapSubCmd {
#[clap(flatten)]
opts: RepairSnapshotsOptions,
#[clap(value_name = "ID")]
ids: Vec<String>,
}
impl Runnable for RepairCmd {
fn run(&self) {
self.cmd.run();
}
}
impl Runnable for IndexSubCmd {
fn run(&self) {
let config = RUSTIC_APP.config();
if let Err(err) = config.repository.run_open(|repo| self.inner_run(repo)) {
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}
impl IndexSubCmd {
fn inner_run(&self, repo: OpenRepo) -> Result<()> {
let config = RUSTIC_APP.config();
repo.repair_index(&self.opts, config.global.dry_run)?;
Ok(())
}
}
impl Runnable for SnapSubCmd {
fn run(&self) {
let config = RUSTIC_APP.config();
if let Err(err) = config.repository.run_indexed(|repo| self.inner_run(repo)) {
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}
impl SnapSubCmd {
fn inner_run(&self, repo: IndexedRepo) -> Result<()> {
let config = RUSTIC_APP.config();
let snaps = get_snapots_from_ids(&repo, &self.ids)?;
repo.repair_snapshots(&self.opts, snaps, config.global.dry_run)?;
Ok(())
}
}