|
1 |
| -use super::error::CertError; |
2 |
| -use crate::command::Command; |
| 1 | +use crate::command::CommandAsync; |
3 | 2 | use crate::log::MaybeFancy;
|
4 |
| - |
| 3 | +use anyhow::Context; |
5 | 4 | use camino::Utf8PathBuf;
|
6 | 5 | 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 | +} |
7 | 16 |
|
8 | 17 | /// Show the device certificate, if any
|
9 | 18 | pub struct ShowCertCmd {
|
10 | 19 | /// The path where the device certificate will be stored
|
11 | 20 | pub cert_path: Utf8PathBuf,
|
12 | 21 | }
|
13 | 22 |
|
14 |
| -impl Command for ShowCertCmd { |
| 23 | +#[async_trait::async_trait] |
| 24 | +impl CommandAsync for ShowCertCmd { |
15 | 25 | fn description(&self) -> String {
|
16 | 26 | "show the device certificate".into()
|
17 | 27 | }
|
18 | 28 |
|
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?; |
21 | 31 | Ok(())
|
22 | 32 | }
|
23 | 33 | }
|
24 | 34 |
|
25 | 35 | 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}"))?; |
33 | 43 |
|
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; |
40 | 52 | Ok(())
|
41 | 53 | }
|
42 | 54 | }
|
0 commit comments