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
//! `list` subcommand

use crate::{commands::open_repository, status_err, Application, RUSTIC_APP};

use abscissa_core::{Command, Runnable, Shutdown};

use anyhow::{bail, Result};

use rustic_core::repofile::{FileType, IndexFile};

/// `list` subcommand
#[derive(clap::Parser, Command, Debug)]
pub(crate) struct ListCmd {
    /// File types to list
    #[clap(value_parser=["blobs", "index", "packs", "snapshots", "keys"])]
    tpe: String,
}

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

impl ListCmd {
    fn inner_run(&self) -> Result<()> {
        let config = RUSTIC_APP.config();
        let repo = open_repository(&config.repository)?;

        let tpe = match self.tpe.as_str() {
            // special treatment for listing blobs: read the index and display it
            "blobs" => {
                for item in repo.stream_files::<IndexFile>()? {
                    let (_, index) = item?;
                    index.packs.into_iter().for_each(|pack| {
                        for blob in pack.blobs {
                            println!("{:?} {:?}", blob.tpe, blob.id);
                        }
                    });
                }
                return Ok(());
            }
            "index" => FileType::Index,
            "packs" => FileType::Pack,
            "snapshots" => FileType::Snapshot,
            "keys" => FileType::Key,
            t => {
                bail!("invalid type: {}", t);
            }
        };

        for id in repo.list(tpe)? {
            println!("{id:?}");
        }

        Ok(())
    }
}