Skip to main content

rustic_rs/commands/
check.rs

1//! `check` subcommand
2
3use crate::{
4    Application, RUSTIC_APP,
5    repository::{OpenRepo, get_global_grouped_snapshots},
6    status_err,
7};
8
9use abscissa_core::{Command, Runnable, Shutdown};
10use anyhow::Result;
11use rustic_core::{CheckOptions, repofile::SnapshotFile};
12
13/// `check` subcommand
14#[derive(clap::Parser, Command, Debug)]
15pub(crate) struct CheckCmd {
16    /// Snapshots to check. If none is given, use filter options to filter from all snapshots
17    ///
18    /// Snapshots can be identified the following ways: "01a2b3c4" or "latest" or "latest~N" (N >= 0)
19    #[clap(value_name = "ID")]
20    ids: Vec<String>,
21
22    /// Check options
23    #[clap(flatten)]
24    opts: CheckOptions,
25}
26
27impl Runnable for CheckCmd {
28    fn run(&self) {
29        if let Err(err) = RUSTIC_APP
30            .config()
31            .repository
32            .run_open(|repo| self.inner_run(repo))
33        {
34            status_err!("{}", err);
35            RUSTIC_APP.shutdown(Shutdown::Crash);
36        };
37    }
38}
39
40impl CheckCmd {
41    fn inner_run(&self, repo: OpenRepo) -> Result<()> {
42        let snaps: Vec<SnapshotFile> = get_global_grouped_snapshots(&repo, &self.ids)?.into();
43        let trees = snaps.into_iter().map(|snap| snap.tree).collect();
44        repo.check_with_trees(self.opts, trees)?.is_ok()?;
45        Ok(())
46    }
47}