Skip to main content

rustic_rs/commands/
repair.rs

1//! `repair` subcommand
2
3use crate::{
4    Application, RUSTIC_APP,
5    repository::{IndexedRepo, OpenRepo, get_snapots_from_ids},
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    ///
46    /// Snapshots can be identified the following ways: "01a2b3c4" or "latest" or "latest~N" (N >= 0)
47    #[clap(value_name = "ID")]
48    ids: Vec<String>,
49}
50
51impl Runnable for RepairCmd {
52    fn run(&self) {
53        self.cmd.run();
54    }
55}
56
57impl Runnable for IndexSubCmd {
58    fn run(&self) {
59        let config = RUSTIC_APP.config();
60        if let Err(err) = config.repository.run_open(|repo| self.inner_run(repo)) {
61            status_err!("{}", err);
62            RUSTIC_APP.shutdown(Shutdown::Crash);
63        };
64    }
65}
66
67impl IndexSubCmd {
68    fn inner_run(&self, repo: OpenRepo) -> Result<()> {
69        let config = RUSTIC_APP.config();
70        repo.repair_index(&self.opts, config.global.dry_run)?;
71        Ok(())
72    }
73}
74
75impl Runnable for SnapSubCmd {
76    fn run(&self) {
77        let config = RUSTIC_APP.config();
78        if let Err(err) = config.repository.run_indexed(|repo| self.inner_run(repo)) {
79            status_err!("{}", err);
80            RUSTIC_APP.shutdown(Shutdown::Crash);
81        };
82    }
83}
84
85impl SnapSubCmd {
86    fn inner_run(&self, repo: IndexedRepo) -> Result<()> {
87        let config = RUSTIC_APP.config();
88        let snaps = get_snapots_from_ids(&repo, &self.ids)?;
89        repo.repair_snapshots(&self.opts, snaps, config.global.dry_run)?;
90        Ok(())
91    }
92}