summaryrefslogtreecommitdiff
path: root/src/cmd/list_files.rs
blob: 22b102e683ddbfbbe69f9c95ec41a436a970c7e2 (plain)
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
use crate::backup_reason::Reason;
use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use crate::fsentry::{FilesystemEntry, FilesystemKind};
use structopt::StructOpt;
use tempfile::NamedTempFile;

#[derive(Debug, StructOpt)]
pub struct ListFiles {
    #[structopt(default_value = "latest")]
    gen_id: String,
}

impl ListFiles {
    pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> {
        let temp = NamedTempFile::new()?;

        let client = BackupClient::new(config)?;

        let genlist = client.list_generations()?;
        let gen_id: String = genlist.resolve(&self.gen_id)?;

        let gen = client.fetch_generation(&gen_id, temp.path())?;
        for file in gen.files()? {
            println!("{}", format_entry(&file.entry(), file.reason()));
        }

        Ok(())
    }
}

fn format_entry(e: &FilesystemEntry, reason: Reason) -> String {
    let kind = match e.kind() {
        FilesystemKind::Regular => "-",
        FilesystemKind::Directory => "d",
        FilesystemKind::Symlink => "l",
        FilesystemKind::Socket => "s",
        FilesystemKind::Fifo => "p",
    };
    format!("{} {} ({})", kind, e.pathbuf().display(), reason)
}