-
Notifications
You must be signed in to change notification settings - Fork 34
/
show.rs
92 lines (83 loc) · 3.21 KB
/
show.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::{path::Path, sync::Arc};
use clap::Parser;
use cli_table::{print_stdout, WithTitle};
use config::{builder::DefaultState, ConfigBuilder};
use mithril_common::{
api_version::APIVersionProvider, certificate_chain::MithrilCertificateVerifier,
digesters::CardanoImmutableDigester, entities::Snapshot, StdError,
};
use crate::{
aggregator_client::{AggregatorHTTPClient, CertificateClient, SnapshotClient},
services::{MithrilClientSnapshotService, SnapshotService},
Config, SnapshotFieldItem,
};
/// Clap command to show a given snapshot
#[derive(Parser, Debug, Clone)]
pub struct SnapshotShowCommand {
/// Enable JSON output.
#[clap(long)]
json: bool,
/// Snapshot digest
digest: String,
}
impl SnapshotShowCommand {
/// Snapshot Show command
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), StdError> {
let config: Config = config_builder
.build()
.map_err(|e| format!("configuration build error: {e}"))?
.try_deserialize()
.map_err(|e| format!("configuration deserialize error: {e}"))?;
let snapshot_service = {
let http_client = Arc::new(AggregatorHTTPClient::new(
&config.aggregator_endpoint,
APIVersionProvider::compute_all_versions_sorted()?,
));
MithrilClientSnapshotService::new(
Arc::new(SnapshotClient::new(http_client.clone())),
Arc::new(CertificateClient::new(http_client)),
Arc::new(MithrilCertificateVerifier::new(slog_scope::logger())),
Arc::new(CardanoImmutableDigester::new(
// the digester needs a path so we pass an empty path since we do not need the digester here
Path::new(""),
None,
slog_scope::logger(),
)),
)
};
let snapshot = snapshot_service.show(&self.digest).await?;
if self.json {
println!("{}", serde_json::to_string(&snapshot)?);
} else {
print_stdout(convert_to_field_items(&snapshot).with_title()).unwrap();
}
Ok(())
}
}
/// Convert Snapshot to SnapshotFieldItems routine
fn convert_to_field_items(snapshot: &Snapshot) -> Vec<SnapshotFieldItem> {
let mut field_items = vec![
SnapshotFieldItem::new("Epoch".to_string(), format!("{}", snapshot.beacon.epoch)),
SnapshotFieldItem::new(
"Immutable File Number".to_string(),
format!("{}", snapshot.beacon.immutable_file_number),
),
SnapshotFieldItem::new("Network".to_string(), snapshot.beacon.network.to_owned()),
SnapshotFieldItem::new("Digest".to_string(), snapshot.digest.to_string()),
SnapshotFieldItem::new("Size".to_string(), format!("{}", snapshot.size)),
];
for (idx, location) in snapshot.locations.iter().enumerate() {
field_items.push(SnapshotFieldItem::new(
format!("Location {}", idx + 1),
location.to_string(),
));
}
field_items.push(SnapshotFieldItem::new(
"Created".to_string(),
snapshot.created_at.to_string(),
));
field_items
}