Skip to content

Commit 758f025

Browse files
committed
make async tedge cert show
Signed-off-by: Didier Wenzek <[email protected]>
1 parent 1c70c41 commit 758f025

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed
+31-19
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
1-
use super::error::CertError;
2-
use crate::command::Command;
1+
use crate::command::CommandAsync;
32
use crate::log::MaybeFancy;
4-
3+
use anyhow::Context;
54
use camino::Utf8PathBuf;
65
use certificate::PemCertificate;
6+
use tokio::io::AsyncWriteExt;
7+
8+
macro_rules! print_async {
9+
($out:expr, $fmt:literal) => (
10+
let _ = $out.write_all($fmt.as_bytes()).await;
11+
);
12+
($out:expr, $fmt:literal, $($arg:tt)*) => (
13+
let _ = $out.write_all(format!($fmt, $($arg)*).as_bytes()).await;
14+
);
15+
}
716

817
/// Show the device certificate, if any
918
pub struct ShowCertCmd {
1019
/// The path where the device certificate will be stored
1120
pub cert_path: Utf8PathBuf,
1221
}
1322

14-
impl Command for ShowCertCmd {
23+
#[async_trait::async_trait]
24+
impl CommandAsync for ShowCertCmd {
1525
fn description(&self) -> String {
1626
"show the device certificate".into()
1727
}
1828

19-
fn execute(&self) -> Result<(), MaybeFancy<anyhow::Error>> {
20-
self.show_certificate()?;
29+
async fn execute(&self) -> Result<(), MaybeFancy<anyhow::Error>> {
30+
self.show_certificate().await?;
2131
Ok(())
2232
}
2333
}
2434

2535
impl ShowCertCmd {
26-
fn show_certificate(&self) -> Result<(), CertError> {
27-
let pem = PemCertificate::from_pem_file(&self.cert_path).map_err(|err| match err {
28-
certificate::CertificateError::IoError { error, .. } => {
29-
CertError::IoError(error).cert_context(self.cert_path.clone())
30-
}
31-
from => CertError::CertificateError(from),
32-
})?;
36+
pub async fn show_certificate(&self) -> Result<(), anyhow::Error> {
37+
let cert_path = &self.cert_path;
38+
let cert = tokio::fs::read_to_string(cert_path)
39+
.await
40+
.with_context(|| format!("reading certificate from {cert_path}"))?;
41+
let pem = PemCertificate::from_pem_string(&cert)
42+
.with_context(|| format!("decoding certificate from {cert_path}"))?;
3343

34-
println!("Device certificate: {}", self.cert_path);
35-
println!("Subject: {}", pem.subject()?);
36-
println!("Issuer: {}", pem.issuer()?);
37-
println!("Valid from: {}", pem.not_before()?);
38-
println!("Valid up to: {}", pem.not_after()?);
39-
println!("Thumbprint: {}", pem.thumbprint()?);
44+
let mut stdout = tokio::io::stdout();
45+
print_async!(stdout, "Device certificate: {}\n", self.cert_path);
46+
print_async!(stdout, "Subject: {}\n", pem.subject()?);
47+
print_async!(stdout, "Issuer: {}\n", pem.issuer()?);
48+
print_async!(stdout, "Valid from: {}\n", pem.not_before()?);
49+
print_async!(stdout, "Valid up to: {}\n", pem.not_after()?);
50+
print_async!(stdout, "Thumbprint: {}\n", pem.thumbprint()?);
51+
let _ = stdout.flush().await;
4052
Ok(())
4153
}
4254
}

crates/core/tedge/tests/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ mod tests {
119119
show_cmd
120120
.assert()
121121
.failure()
122-
.stderr(predicate::str::contains("Missing file"));
122+
.stderr(predicate::str::contains("No such file"));
123123

124124
// The remove command also removed the device id from the config
125125
get_device_id_cmd

0 commit comments

Comments
 (0)