mithril_client_cli/commands/cardano_db_v2/
list.rs

1use clap::Parser;
2use cli_table::{format::Justify, print_stdout, Cell, Table};
3
4use mithril_client::MithrilResult;
5
6use crate::{
7    commands::{client_builder_with_fallback_genesis_key, SharedArgs},
8    utils::CardanoDbUtils,
9    CommandContext,
10};
11
12/// Clap command to list existing Cardano db snapshots
13#[derive(Parser, Debug, Clone)]
14pub struct CardanoDbListCommand {
15    #[clap(flatten)]
16    shared_args: SharedArgs,
17}
18
19impl CardanoDbListCommand {
20    /// Is JSON output enabled
21    pub fn is_json_output_enabled(&self) -> bool {
22        self.shared_args.json
23    }
24
25    /// Main command execution
26    pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
27        let params = context.config_parameters()?;
28        let client = client_builder_with_fallback_genesis_key(&params)?
29            .with_logger(context.logger().clone())
30            .build()?;
31        let items = client.cardano_database_v2().list().await?;
32
33        if self.is_json_output_enabled() {
34            println!("{}", serde_json::to_string(&items)?);
35        } else {
36            let items = items
37                .into_iter()
38                .map(|item| {
39                    vec![
40                        format!("{}", item.beacon.epoch).cell(),
41                        format!("{}", item.beacon.immutable_file_number).cell(),
42                        item.hash.cell(),
43                        item.merkle_root.cell(),
44                        CardanoDbUtils::format_bytes_to_gigabytes(item.total_db_size_uncompressed)
45                            .cell(),
46                        item.cardano_node_version.cell(),
47                        item.created_at.to_string().cell(),
48                    ]
49                })
50                .collect::<Vec<_>>()
51                .table()
52                .title(vec![
53                    "Epoch".cell(),
54                    "Immutable".cell(),
55                    "Hash".cell(),
56                    "Merkle root".cell(),
57                    "Database size".cell().justify(Justify::Right),
58                    "Cardano node".cell(),
59                    "Created".cell().justify(Justify::Right),
60                ]);
61            print_stdout(items)?;
62        }
63
64        Ok(())
65    }
66}