mithril_client_cli/commands/cardano_transaction/
snapshot_list.rs

1use clap::Parser;
2use cli_table::format::Justify;
3use cli_table::{print_stdout, Cell, Table};
4
5use crate::commands::{client_builder_with_fallback_genesis_key, SharedArgs};
6use crate::CommandContext;
7use mithril_client::MithrilResult;
8
9/// Cardano transaction snapshot list command
10#[derive(Parser, Debug, Clone)]
11pub struct CardanoTransactionSnapshotListCommand {
12    #[clap(flatten)]
13    shared_args: SharedArgs,
14}
15
16impl CardanoTransactionSnapshotListCommand {
17    /// Is JSON output enabled
18    pub fn is_json_output_enabled(&self) -> bool {
19        self.shared_args.json
20    }
21
22    /// Main command execution
23    pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
24        let params = context.config_parameters()?;
25        let client = client_builder_with_fallback_genesis_key(&params)?
26            .with_logger(context.logger().clone())
27            .build()?;
28        let lines = client.cardano_transaction().list_snapshots().await?;
29
30        if self.is_json_output_enabled() {
31            println!("{}", serde_json::to_string(&lines)?);
32        } else {
33            let lines = lines
34                .into_iter()
35                .map(|item| {
36                    vec![
37                        format!("{}", item.epoch).cell(),
38                        format!("{}", item.block_number).cell(),
39                        item.hash.cell(),
40                        item.certificate_hash.cell(),
41                        item.created_at.to_string().cell(),
42                    ]
43                })
44                .collect::<Vec<_>>()
45                .table()
46                .title(vec![
47                    "Epoch".cell(),
48                    "Block Number".cell(),
49                    "Hash".cell(),
50                    "Certificate Hash".cell(),
51                    "Created".cell().justify(Justify::Right),
52                ]);
53            print_stdout(lines)?;
54        }
55
56        Ok(())
57    }
58}