-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
ping.rs
52 lines (46 loc) · 1.51 KB
/
ping.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
use std::time::Instant;
use async_trait::async_trait;
use clap::Args;
use miette::{IntoDiagnostic, Result, WrapErr};
use oro_client::{self, OroClient};
use serde_json::Value;
use url::Url;
use crate::commands::OroCommand;
/// Ping the registry.
#[derive(Debug, Args)]
pub struct PingCmd {
#[arg(from_global)]
registry: Url,
#[arg(from_global)]
json: bool,
#[arg(from_global)]
emoji: bool,
}
#[async_trait]
impl OroCommand for PingCmd {
async fn execute(self) -> Result<()> {
let start = Instant::now();
let registry = self.registry;
tracing::info!("{}ping: {registry}", if self.emoji { "🗣️ " } else { "" });
let client = OroClient::new(registry.clone());
let payload = client.ping().await?;
let time = start.elapsed().as_micros() as f32 / 1000.0;
tracing::info!("{}pong: {time}ms", if self.emoji { "👂 " } else { "" });
if self.json {
let details: Value = serde_json::from_str(&payload)
.into_diagnostic()
.wrap_err("ping::deserialize")?;
let output = serde_json::to_string_pretty(&serde_json::json!({
"registry": registry.to_string(),
"time": time,
"details": details,
}))
.into_diagnostic()
.wrap_err("ping::serialize")?;
println!("{output}");
} else {
tracing::info!("{}payload: {payload}", if self.emoji { "📦 " } else { "" });
}
Ok(())
}
}