Have clap convert strings to KeyHandles

- Instead of doing the conversion from a string to a `KeyHandle`,
    have clap do it.

  - Fixes #98.

  - See #13.
This commit is contained in:
Neal H. Walfield 2023-11-13 14:22:04 +01:00
parent 65050be557
commit c554202a84
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3
5 changed files with 13 additions and 30 deletions

View File

@ -4,6 +4,7 @@ use clap::{ValueEnum, ArgGroup, Args, Parser, Subcommand};
use sequoia_openpgp as openpgp;
use openpgp::cert::CipherSuite as SqCipherSuite;
use openpgp::KeyHandle;
use openpgp::types::ReasonForRevocation as OpenPGPRevocationReason;
use crate::cli::KEY_VALIDITY_DURATION;
@ -814,8 +815,7 @@ pub struct AdoptCommand {
required(true),
help = "Adds the key or subkey KEY to the TARGET-KEY",
)]
// TODO Type should be KeyHandle, improve help
pub key: Vec<String>,
pub key: Vec<KeyHandle>,
#[clap(
long = "expire",
value_name = "KEY-EXPIRATION-TIME",
@ -1134,7 +1134,7 @@ material, then that key is used to sign the revocation certificate.",
"The subkey to revoke. This must either be the subkey's Key ID or its \
fingerprint.",
)]
pub subkey: String,
pub subkey: KeyHandle,
#[clap(
value_name = "REASON",

View File

@ -2,6 +2,9 @@ use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
use sequoia_openpgp as openpgp;
use openpgp::KeyHandle;
use super::types::ClapData;
use super::types::FileOrStdin;
use super::types::FileOrStdout;
@ -137,7 +140,7 @@ pub struct FilterCommand {
including those certificates that match the \
given fingerprint or key id.",
)]
pub handle: Vec<String>,
pub handle: Vec<KeyHandle>,
#[clap(
short = 'P',
long = "prune-certs",

View File

@ -33,19 +33,10 @@ pub fn adopt(config: Config, command: cli::key::AdoptCommand) -> Result<()>
Key<key::PublicParts, key::SubordinateRole>,
SignatureBuilder,
)>,
)> = vec![];
// Gather the Key IDs / Fingerprints and make sure they are valid.
for id in command.key {
let h = id.parse::<KeyHandle>()?;
if h.is_invalid() {
return Err(anyhow::anyhow!(
"Invalid Fingerprint or KeyID ('{:?}')",
id
));
}
wanted.push((h, None));
}
)> = command.key
.into_iter()
.map(|kh| (kh, None))
.collect::<Vec<_>>();
let null_policy = &openpgp::policy::NullPolicy::new();
let adoptee_policy: &dyn Policy = if command.allow_broken_crypto {

View File

@ -343,13 +343,8 @@ pub fn subkey_revoke(
let notations = parse_notations(command.notation)?;
let keyhandle: KeyHandle = command.subkey.parse().context(format!(
"Parsing {:?} as an OpenPGP fingerprint or Key ID",
command.subkey
))?;
let revocation = SubkeyRevocation::new(
&keyhandle,
&command.subkey,
cert,
secret,
&config.policy,

View File

@ -18,7 +18,6 @@ use openpgp::{
CertParser,
},
Fingerprint,
KeyHandle,
packet::{
UserID,
UserAttribute,
@ -83,15 +82,10 @@ pub fn dispatch(config: Config, c: keyring::Command) -> Result<()> {
let ua_predicate = |_ua: &UserAttribute| false;
let any_key_predicates = ! command.handle.is_empty();
let handles: Vec<KeyHandle> = {
use std::str::FromStr;
command.handle.iter().map(|h| KeyHandle::from_str(h))
.collect::<Result<_>>()?
};
let key_predicate = |key: &Key<_, _>| {
let mut keep = false;
for handle in &handles {
for handle in &command.handle {
keep |= handle.aliases(key.key_handle());
}