summaryrefslogtreecommitdiff
path: root/src/cmd/show_gen.rs
blob: f47a07bef7bc7bac748b05784e0701e0ee407560 (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
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
91
92
93
94
95
96
97
98
99
100
101
//! The `show-generation` subcommand.

use crate::chunk::ClientTrust;
use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::db::DbInt;
use crate::error::ObnamError;
use crate::fsentry::FilesystemKind;
use crate::generation::GenId;
use indicatif::HumanBytes;
use serde::Serialize;
use structopt::StructOpt;
use tempfile::NamedTempFile;
use tokio::runtime::Runtime;

/// Show information about a generation.
#[derive(Debug, StructOpt)]
pub struct ShowGeneration {
    /// Reference to the generation. Defaults to latest.
    #[structopt(default_value = "latest")]
    gen_id: String,
}

impl ShowGeneration {
    /// Run the command.
    pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> {
        let rt = Runtime::new()?;
        rt.block_on(self.run_async(config))
    }

    async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
        let temp = NamedTempFile::new()?;
        let client = BackupClient::new(config)?;
        let trust = client
            .get_client_trust()
            .await?
            .or_else(|| Some(ClientTrust::new("FIXME", None, "".to_string(), vec![])))
            .unwrap();

        let genlist = client.list_generations(&trust);
        let gen_id = genlist.resolve(&self.gen_id)?;
        let gen = client.fetch_generation(&gen_id, temp.path()).await?;
        let mut files = gen.files()?;
        let mut files = files.iter()?;

        let total_bytes = files.try_fold(0, |acc, file| {
            file.map(|(_, e, _, _)| {
                if e.kind() == FilesystemKind::Regular {
                    acc + e.len()
                } else {
                    acc
                }
            })
        });
        let total_bytes = total_bytes?;

        let output = Output::new(gen_id)
            .db_bytes(temp.path().metadata()?.len())
            .file_count(gen.file_count()?)
            .file_bytes(total_bytes);
        serde_json::to_writer_pretty(std::io::stdout(), &output)?;

        Ok(())
    }
}

#[derive(Debug, Default, Serialize)]
struct Output {
    generation_id: String,
    file_count: DbInt,
    file_bytes: String,
    file_bytes_raw: u64,
    db_bytes: String,
    db_bytes_raw: u64,
}

impl Output {
    fn new(gen_id: GenId) -> Self {
        Self {
            generation_id: format!("{}", gen_id),
            ..Self::default()
        }
    }

    fn file_count(mut self, n: DbInt) -> Self {
        self.file_count = n;
        self
    }

    fn file_bytes(mut self, n: u64) -> Self {
        self.file_bytes_raw = n;
        self.file_bytes = HumanBytes(n).to_string();
        self
    }

    fn db_bytes(mut self, n: u64) -> Self {
        self.db_bytes_raw = n;
        self.db_bytes = HumanBytes(n).to_string();
        self
    }
}