mithril_client_cli/commands/cardano_db/
show.rs1use anyhow::{anyhow, Context};
2use clap::Parser;
3use cli_table::{print_stdout, Cell, Table};
4
5use crate::{
6 commands::{client_builder_with_fallback_genesis_key, SharedArgs},
7 utils::{CardanoDbUtils, ExpanderUtils},
8 CommandContext,
9};
10use mithril_client::MithrilResult;
11
12#[derive(Parser, Debug, Clone)]
14pub struct CardanoDbShowCommand {
15 #[clap(flatten)]
16 shared_args: SharedArgs,
17
18 digest: String,
20}
21
22impl CardanoDbShowCommand {
23 pub fn is_json_output_enabled(&self) -> bool {
25 self.shared_args.json
26 }
27
28 pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
30 let params = context.config_parameters()?;
31 let client = client_builder_with_fallback_genesis_key(¶ms)?
32 .with_logger(context.logger().clone())
33 .build()?;
34
35 let get_list_of_artifact_ids = || async {
36 let cardano_dbs = client.cardano_database().list().await.with_context(|| {
37 "Can not get the list of artifacts while retrieving the latest cardano db digest"
38 })?;
39
40 Ok(cardano_dbs
41 .iter()
42 .map(|cardano_db| cardano_db.digest.to_owned())
43 .collect::<Vec<String>>())
44 };
45
46 let cardano_db_message = client
47 .cardano_database()
48 .get(
49 &ExpanderUtils::expand_eventual_id_alias(&self.digest, get_list_of_artifact_ids())
50 .await?,
51 )
52 .await?
53 .ok_or_else(|| anyhow!("Cardano DB not found for digest: '{}'", &self.digest))?;
54
55 if self.is_json_output_enabled() {
56 println!("{}", serde_json::to_string(&cardano_db_message)?);
57 } else {
58 let cardano_db_table = vec![
59 vec![
60 "Epoch".cell(),
61 format!("{}", &cardano_db_message.beacon.epoch).cell(),
62 ],
63 vec![
64 "Immutable File Number".cell(),
65 format!("{}", &cardano_db_message.beacon.immutable_file_number).cell(),
66 ],
67 vec!["Network".cell(), cardano_db_message.network.cell()],
68 vec!["Digest".cell(), cardano_db_message.digest.cell()],
69 vec![
70 "Size".cell(),
71 CardanoDbUtils::format_bytes_to_gigabytes(cardano_db_message.size).cell(),
72 ],
73 vec![
74 "Cardano node version".cell(),
75 cardano_db_message.cardano_node_version.cell(),
76 ],
77 vec![
78 "Location".cell(),
79 cardano_db_message.locations.join(",").cell(),
80 ],
81 vec![
82 "Created".cell(),
83 cardano_db_message.created_at.to_string().cell(),
84 ],
85 vec![
86 "Compression Algorithm".cell(),
87 format!("{}", &cardano_db_message.compression_algorithm).cell(),
88 ],
89 ]
90 .table();
91
92 print_stdout(cardano_db_table)?
93 }
94
95 Ok(())
96 }
97}