diff --git a/tools/src/commands/ssh.rs b/tools/src/commands/ssh.rs index 7999579..3fe49ee 100644 --- a/tools/src/commands/ssh.rs +++ b/tools/src/commands/ssh.rs @@ -22,6 +22,9 @@ pub struct SshCommand { help = "Identifier of the card to use" )] pub ident: Option, + + #[clap(long, help = "Only print the ssh public key")] + pub key_only: bool, } pub fn print_ssh( @@ -31,6 +34,8 @@ pub fn print_ssh( ) -> Result<()> { let mut output = output::Ssh::default(); + output.key_only(command.key_only); + let ident = command.ident; let backend = pick_card_for_reading(ident)?; diff --git a/tools/src/output/ssh.rs b/tools/src/output/ssh.rs index 5ba0467..da28d28 100644 --- a/tools/src/output/ssh.rs +++ b/tools/src/output/ssh.rs @@ -8,12 +8,17 @@ use crate::{OutputBuilder, OutputFormat, OutputVariant, OutputVersion}; #[derive(Debug, Default, Serialize)] pub struct Ssh { + key_only: bool, // only print ssh public key, in text mode ident: String, authentication_key_fingerprint: Option, ssh_public_key: Option, } impl Ssh { + pub fn key_only(&mut self, ssh_key_only: bool) { + self.key_only = ssh_key_only; + } + pub fn ident(&mut self, ident: String) { self.ident = ident; } @@ -27,16 +32,20 @@ impl Ssh { } fn text(&self) -> Result { - let mut s = format!("OpenPGP card {}\n\n", self.ident); + if !self.key_only { + let mut s = format!("OpenPGP card {}\n\n", self.ident); - if let Some(fp) = &self.authentication_key_fingerprint { - s.push_str(&format!("Authentication key fingerprint:\n{fp}\n\n")); - } - if let Some(key) = &self.ssh_public_key { - s.push_str(&format!("SSH public key:\n{key}\n")); - } + if let Some(fp) = &self.authentication_key_fingerprint { + s.push_str(&format!("Authentication key fingerprint:\n{fp}\n\n")); + } + if let Some(key) = &self.ssh_public_key { + s.push_str(&format!("SSH public key:\n{key}\n")); + } - Ok(s) + Ok(s) + } else { + Ok(self.ssh_public_key.clone().unwrap_or("".to_string())) + } } fn v1(&self) -> Result {