1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! `repair` subcommand

use crate::{commands::open_repository, status_err, Application, RUSTIC_APP};
use abscissa_core::{Command, Runnable, Shutdown};

use anyhow::Result;

use rustic_core::{RepairIndexOptions, RepairSnapshotsOptions};

/// `repair` subcommand
#[derive(clap::Parser, Command, Debug)]
pub(crate) struct RepairCmd {
    /// Subcommand to run
    #[clap(subcommand)]
    cmd: RepairSubCmd,
}

#[derive(clap::Subcommand, Debug, Runnable)]
enum RepairSubCmd {
    /// Repair the repository index
    Index(IndexSubCmd),
    /// Repair snapshots
    Snapshots(SnapSubCmd),
}

#[derive(Default, Debug, clap::Parser, Command)]
struct IndexSubCmd {
    /// Index repair options
    #[clap(flatten)]
    opts: RepairIndexOptions,
}

/// `repair snapshots` subcommand
#[derive(Default, Debug, clap::Parser, Command)]
struct SnapSubCmd {
    /// Snapshot repair options
    #[clap(flatten)]
    opts: RepairSnapshotsOptions,

    /// Snapshots to repair. If none is given, use filter to filter from all snapshots.
    #[clap(value_name = "ID")]
    ids: Vec<String>,
}

impl Runnable for RepairCmd {
    fn run(&self) {
        self.cmd.run();
    }
}

impl Runnable for IndexSubCmd {
    fn run(&self) {
        if let Err(err) = self.inner_run() {
            status_err!("{}", err);
            RUSTIC_APP.shutdown(Shutdown::Crash);
        };
    }
}

impl IndexSubCmd {
    fn inner_run(&self) -> Result<()> {
        let config = RUSTIC_APP.config();
        let repo = open_repository(&config.repository)?;
        repo.repair_index(&self.opts, config.global.dry_run)?;
        Ok(())
    }
}

impl Runnable for SnapSubCmd {
    fn run(&self) {
        if let Err(err) = self.inner_run() {
            status_err!("{}", err);
            RUSTIC_APP.shutdown(Shutdown::Crash);
        };
    }
}

impl SnapSubCmd {
    fn inner_run(&self) -> Result<()> {
        let config = RUSTIC_APP.config();
        let repo = open_repository(&config.repository)?.to_indexed()?;
        let snaps = if self.ids.is_empty() {
            repo.get_all_snapshots()?
        } else {
            repo.get_snapshots(&self.ids)?
        };
        repo.repair_snapshots(&self.opts, snaps, config.global.dry_run)?;
        Ok(())
    }
}