rustic_rs/commands/
repair.rs

1//! `repair` subcommand
2
3use crate::{
4    Application, RUSTIC_APP,
5    repository::{CliIndexedRepo, CliOpenRepo},
6    status_err,
7};
8use abscissa_core::{Command, Runnable, Shutdown};
9
10use anyhow::Result;
11
12use rustic_core::{RepairIndexOptions, RepairSnapshotsOptions};
13
14/// `repair` subcommand
15#[derive(clap::Parser, Command, Debug)]
16pub(crate) struct RepairCmd {
17    /// Subcommand to run
18    #[clap(subcommand)]
19    cmd: RepairSubCmd,
20}
21
22#[derive(clap::Subcommand, Debug, Runnable)]
23enum RepairSubCmd {
24    /// Repair the repository index
25    Index(IndexSubCmd),
26    /// Repair snapshots
27    Snapshots(SnapSubCmd),
28}
29
30#[derive(Default, Debug, clap::Parser, Command)]
31struct IndexSubCmd {
32    /// Index repair options
33    #[clap(flatten)]
34    opts: RepairIndexOptions,
35}
36
37/// `repair snapshots` subcommand
38#[derive(Default, Debug, clap::Parser, Command)]
39struct SnapSubCmd {
40    /// Snapshot repair options
41    #[clap(flatten)]
42    opts: RepairSnapshotsOptions,
43
44    /// Snapshots to repair. If none is given, use filter to filter from all snapshots.
45    #[clap(value_name = "ID")]
46    ids: Vec<String>,
47}
48
49impl Runnable for RepairCmd {
50    fn run(&self) {
51        self.cmd.run();
52    }
53}
54
55impl Runnable for IndexSubCmd {
56    fn run(&self) {
57        let config = RUSTIC_APP.config();
58        if let Err(err) = config.repository.run_open(|repo| self.inner_run(repo)) {
59            status_err!("{}", err);
60            RUSTIC_APP.shutdown(Shutdown::Crash);
61        };
62    }
63}
64
65impl IndexSubCmd {
66    fn inner_run(&self, repo: CliOpenRepo) -> Result<()> {
67        let config = RUSTIC_APP.config();
68        repo.repair_index(&self.opts, config.global.dry_run)?;
69        Ok(())
70    }
71}
72
73impl Runnable for SnapSubCmd {
74    fn run(&self) {
75        let config = RUSTIC_APP.config();
76        if let Err(err) = config.repository.run_indexed(|repo| self.inner_run(repo)) {
77            status_err!("{}", err);
78            RUSTIC_APP.shutdown(Shutdown::Crash);
79        };
80    }
81}
82
83impl SnapSubCmd {
84    fn inner_run(&self, repo: CliIndexedRepo) -> Result<()> {
85        let config = RUSTIC_APP.config();
86        let snaps = if self.ids.is_empty() {
87            repo.get_all_snapshots()?
88        } else {
89            repo.get_snapshots(&self.ids)?
90        };
91        repo.repair_snapshots(&self.opts, snaps, config.global.dry_run)?;
92        Ok(())
93    }
94}