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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! `repoinfo` subcommand

use crate::{
    commands::{get_repository, open_repository},
    helpers::{bytes_size_to_string, table_right_from},
    status_err, Application, RUSTIC_APP,
};

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

use anyhow::Result;
use rustic_core::{IndexInfos, RepoFileInfo, RepoFileInfos};

/// `repoinfo` subcommand
#[derive(clap::Parser, Command, Debug)]
pub(crate) struct RepoInfoCmd {
    /// Only scan repository files (doesn't need repository password)
    #[clap(long)]
    only_files: bool,

    /// Only scan index
    #[clap(long)]
    only_index: bool,

    /// Show infos in json format
    #[clap(long)]
    json: bool,
}

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

/// Infos about the repository
///
/// This struct is used to serialize infos in `json` format.
#[serde_with::apply(Option => #[serde(default, skip_serializing_if = "Option::is_none")])]
#[derive(Serialize)]
struct Infos {
    files: Option<RepoFileInfos>,
    index: Option<IndexInfos>,
}

impl RepoInfoCmd {
    fn inner_run(&self) -> Result<()> {
        let config = RUSTIC_APP.config();

        let infos = Infos {
            files: (!self.only_index)
                .then(|| -> Result<_> {
                    let repo = get_repository(&config.repository)?;
                    Ok(repo.infos_files()?)
                })
                .transpose()?,
            index: (!self.only_files)
                .then(|| -> Result<_> {
                    let repo = open_repository(&config.repository)?;
                    Ok(repo.infos_index()?)
                })
                .transpose()?,
        };

        if self.json {
            let mut stdout = std::io::stdout();
            serde_json::to_writer_pretty(&mut stdout, &infos)?;
            return Ok(());
        }

        if let Some(file_info) = infos.files {
            print_file_info("repository files", file_info.repo);
            if let Some(info) = file_info.repo_hot {
                print_file_info("hot repository files", info);
            }
        }

        if let Some(index_info) = infos.index {
            print_index_info(index_info);
        }
        Ok(())
    }
}

/// Print infos about repository files
///
/// # Arguments
///
/// * `text` - the text to print before the table
/// * `info` - the [`RepoFileInfo`]s to print
pub fn print_file_info(text: &str, info: Vec<RepoFileInfo>) {
    let mut table = table_right_from(1, ["File type", "Count", "Total Size"]);
    let mut total_count = 0;
    let mut total_size = 0;
    for row in info {
        _ = table.add_row([
            format!("{:?}", row.tpe),
            row.count.to_string(),
            bytes_size_to_string(row.size),
        ]);
        total_count += row.count;
        total_size += row.size;
    }
    println!("{text}");
    _ = table.add_row([
        "Total".to_string(),
        total_count.to_string(),
        bytes_size_to_string(total_size),
    ]);

    println!();
    println!("{table}");
    println!();
}

/// Print infos about index
///
/// # Arguments
///
/// * `index_info` - the [`IndexInfos`] to print
pub fn print_index_info(index_info: IndexInfos) {
    let mut table = table_right_from(
        1,
        ["Blob type", "Count", "Total Size", "Total Size in Packs"],
    );

    let mut total_count = 0;
    let mut total_data_size = 0;
    let mut total_size = 0;

    for blobs in &index_info.blobs {
        _ = table.add_row([
            format!("{:?}", blobs.blob_type),
            blobs.count.to_string(),
            bytes_size_to_string(blobs.data_size),
            bytes_size_to_string(blobs.size),
        ]);
        total_count += blobs.count;
        total_data_size += blobs.data_size;
        total_size += blobs.size;
    }
    for blobs in &index_info.blobs_delete {
        if blobs.count > 0 {
            _ = table.add_row([
                format!("{:?} to delete", blobs.blob_type),
                blobs.count.to_string(),
                bytes_size_to_string(blobs.data_size),
                bytes_size_to_string(blobs.size),
            ]);
            total_count += blobs.count;
            total_data_size += blobs.data_size;
            total_size += blobs.size;
        }
    }

    _ = table.add_row([
        "Total".to_string(),
        total_count.to_string(),
        bytes_size_to_string(total_data_size),
        bytes_size_to_string(total_size),
    ]);

    println!();
    println!("{table}");

    let mut table = table_right_from(
        1,
        ["Blob type", "Pack Count", "Minimum Size", "Maximum Size"],
    );

    for packs in index_info.packs {
        _ = table.add_row([
            format!("{:?} packs", packs.blob_type),
            packs.count.to_string(),
            packs.min_size.map_or("-".to_string(), bytes_size_to_string),
            packs.max_size.map_or("-".to_string(), bytes_size_to_string),
        ]);
    }
    for packs in index_info.packs_delete {
        if packs.count > 0 {
            _ = table.add_row([
                format!("{:?} packs to delete", packs.blob_type),
                packs.count.to_string(),
                packs.min_size.map_or("-".to_string(), bytes_size_to_string),
                packs.max_size.map_or("-".to_string(), bytes_size_to_string),
            ]);
        }
    }
    println!();
    println!("{table}");
}