mithril_client_cli/commands/cardano_db/
list.rs

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