mithril_client_cli/commands/cardano_db/
list.rs

1use clap::Parser;
2use cli_table::{Cell, Table, format::Justify, print_stdout};
3
4use crate::{
5    CommandContext,
6    commands::{cardano_db::CardanoDbCommandsBackend, client_builder_with_fallback_genesis_key},
7    utils::CardanoDbUtils,
8};
9use mithril_client::{Client, MithrilResult};
10
11/// Clap command to list existing Cardano dbs
12#[derive(Parser, Debug, Clone)]
13pub struct CardanoDbListCommand {
14    ///Backend to use, either: `v1` (default, full database restoration only) or `v2` (full or partial database restoration)
15    #[arg(short, long, value_enum, default_value_t)]
16    backend: CardanoDbCommandsBackend,
17}
18
19impl CardanoDbListCommand {
20    /// Main command execution
21    pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
22        let client = client_builder_with_fallback_genesis_key(context.config_parameters())?
23            .with_logger(context.logger().clone())
24            .build()?;
25
26        match self.backend {
27            CardanoDbCommandsBackend::V1 => self.print_v1(client, context).await?,
28            CardanoDbCommandsBackend::V2 => self.print_v2(client, context).await?,
29        }
30
31        Ok(())
32    }
33
34    async fn print_v1(&self, client: Client, context: CommandContext) -> MithrilResult<()> {
35        let items = client.cardano_database().list().await?;
36
37        if context.is_json_output_enabled() {
38            println!("{}", serde_json::to_string(&items)?);
39        } else {
40            let items = items
41                .into_iter()
42                .map(|item| {
43                    vec![
44                        format!("{}", item.beacon.epoch).cell(),
45                        format!("{}", item.beacon.immutable_file_number).cell(),
46                        item.network.cell(),
47                        item.digest.cell(),
48                        CardanoDbUtils::format_bytes_to_gigabytes(item.size).cell(),
49                        format!("{}", item.locations.len()).cell(),
50                        item.created_at.to_string().cell(),
51                    ]
52                })
53                .collect::<Vec<_>>()
54                .table()
55                .title(vec![
56                    "Epoch".cell(),
57                    "Immutable".cell(),
58                    "Network".cell(),
59                    "Digest".cell(),
60                    "Size".cell().justify(Justify::Right),
61                    "Locations".cell().justify(Justify::Right),
62                    "Created".cell().justify(Justify::Right),
63                ]);
64            print_stdout(items)?;
65        }
66        Ok(())
67    }
68
69    async fn print_v2(&self, client: Client, context: CommandContext) -> MithrilResult<()> {
70        let items = client.cardano_database_v2().list().await?;
71
72        if context.is_json_output_enabled() {
73            println!("{}", serde_json::to_string(&items)?);
74        } else {
75            let items = items
76                .into_iter()
77                .map(|item| {
78                    vec![
79                        format!("{}", item.beacon.epoch).cell(),
80                        format!("{}", item.beacon.immutable_file_number).cell(),
81                        item.hash.cell(),
82                        item.merkle_root.cell(),
83                        CardanoDbUtils::format_bytes_to_gigabytes(item.total_db_size_uncompressed)
84                            .cell(),
85                        item.cardano_node_version.cell(),
86                        item.created_at.to_string().cell(),
87                    ]
88                })
89                .collect::<Vec<_>>()
90                .table()
91                .title(vec![
92                    "Epoch".cell(),
93                    "Immutable".cell(),
94                    "Hash".cell(),
95                    "Merkle root".cell(),
96                    "Database size".cell().justify(Justify::Right),
97                    "Cardano node".cell(),
98                    "Created".cell().justify(Justify::Right),
99                ]);
100            print_stdout(items)?;
101        }
102
103        Ok(())
104    }
105}