From b489c7da4dac477143ecd57a143471e8e6283bad Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Thu, 27 Oct 2022 21:10:29 +0200 Subject: [PATCH] opgpcard: Allow sign and decrypt to write to file - Sometimes, it is more convenient to give the target filename as an argument, instead of using pipes. - Add an optional argument -o/--output to opgpcard sign and opgpcard decrypt. --- tools/src/bin/opgpcard/commands/decrypt.rs | 7 ++++++- tools/src/bin/opgpcard/commands/sign.rs | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tools/src/bin/opgpcard/commands/decrypt.rs b/tools/src/bin/opgpcard/commands/decrypt.rs index 462cba2..830809c 100644 --- a/tools/src/bin/opgpcard/commands/decrypt.rs +++ b/tools/src/bin/opgpcard/commands/decrypt.rs @@ -36,6 +36,10 @@ pub struct DecryptCommand { /// Input file (stdin if unset) #[clap(name = "input")] input: Option, + + /// Output file (stdout if unset) + #[clap(name = "output", long = "output", short = 'o')] + pub output: Option, } pub fn decrypt(command: DecryptCommand) -> Result<(), Box> { @@ -59,7 +63,8 @@ pub fn decrypt(command: DecryptCommand) -> Result<(), Box let db = DecryptorBuilder::from_reader(input)?; let mut decryptor = db.with_policy(&p, None, d)?; - std::io::copy(&mut decryptor, &mut std::io::stdout())?; + let mut sink = util::open_or_stdout(command.output.as_deref())?; + std::io::copy(&mut decryptor, &mut sink)?; Ok(()) } diff --git a/tools/src/bin/opgpcard/commands/sign.rs b/tools/src/bin/opgpcard/commands/sign.rs index f891b61..e00e9c1 100644 --- a/tools/src/bin/opgpcard/commands/sign.rs +++ b/tools/src/bin/opgpcard/commands/sign.rs @@ -41,11 +41,20 @@ pub struct SignCommand { /// Input file (stdin if unset) #[clap(name = "input")] pub input: Option, + + /// Output file (stdout if unset) + #[clap(name = "output", long = "output", short = 'o')] + pub output: Option, } pub fn sign(command: SignCommand) -> Result<(), Box> { if command.detached { - sign_detached(&command.ident, command.user_pin, command.input.as_deref()) + sign_detached( + &command.ident, + command.user_pin, + command.input.as_deref(), + command.output.as_deref(), + ) } else { Err(anyhow::anyhow!("Only detached signatures are supported for now").into()) } @@ -55,6 +64,7 @@ pub fn sign_detached( ident: &str, pin_file: Option, input: Option<&Path>, + output: Option<&Path>, ) -> Result<(), Box> { let mut input = util::open_or_stdin(input)?; @@ -71,7 +81,9 @@ pub fn sign_detached( let mut sign = util::verify_to_sign(&mut card, user_pin.as_deref())?; let s = sign.signer(&|| println!("Touch confirmation needed for signing"))?; - let message = Armorer::new(Message::new(std::io::stdout())).build()?; + let sink = util::open_or_stdout(output)?; + + let message = Armorer::new(Message::new(sink)).build()?; let mut signer = Signer::new(message, s).detached().build()?; std::io::copy(&mut input, &mut signer)?;