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