diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2022-07-01 14:08:18 +0200 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2022-07-05 13:57:05 +0200 |
commit | 1fc284d0219b5923d7bfee6689f0716fb979c28b (patch) | |
tree | 3fc24fb35c0ec7b12d4611c68becf13e1b5d0470 | |
parent | 11b994974df8c44a3b90c488ac47a6e7df5c992c (diff) |
sq: Adapt sq wkd to clap3's derive API.
-rw-r--r-- | sq/src/commands/net.rs | 53 | ||||
-rw-r--r-- | sq/src/sq.rs | 6 | ||||
-rw-r--r-- | sq/src/sq_cli.rs | 6 |
3 files changed, 29 insertions, 36 deletions
diff --git a/sq/src/commands/net.rs b/sq/src/commands/net.rs index 457298f5..8865af1e 100644 --- a/sq/src/commands/net.rs +++ b/sq/src/commands/net.rs @@ -30,16 +30,8 @@ use crate::{ use crate::sq_cli::KeyserverCommand; use crate::sq_cli::KeyserverSubcommands; - -fn parse_network_policy(m: &clap::ArgMatches) -> net::Policy { - match m.value_of("policy").expect("has default value") { - "offline" => net::Policy::Offline, - "anonymized" => net::Policy::Anonymized, - "encrypted" => net::Policy::Encrypted, - "insecure" => net::Policy::Insecure, - _ => unreachable!(), - } -} +use crate::sq_cli::WkdCommand; +use crate::sq_cli::WkdSubcommands; pub fn dispatch_keyserver(config: Config, c: KeyserverCommand) -> Result<()> { let network_policy = c.policy.into(); @@ -97,32 +89,32 @@ pub fn dispatch_keyserver(config: Config, c: KeyserverCommand) -> Result<()> { Ok(()) } -pub fn dispatch_wkd(config: Config, m: &clap::ArgMatches) -> Result<()> { - let network_policy = parse_network_policy(m); +pub fn dispatch_wkd(config: Config, c: WkdCommand) -> Result<()> { + let network_policy: net::Policy = c.policy.into(); let rt = tokio::runtime::Builder::new_current_thread() .enable_io() .enable_time() .build()?; - match m.subcommand() { - Some(("url", m)) => { - let email_address = m.value_of("input").unwrap(); + match c.subcommand { + WkdSubcommands::Url(c) => { + let email_address = c.email_address; let wkd_url = wkd::Url::from(email_address)?; let url = wkd_url.to_url(None)?; println!("{}", url); }, - Some(("direct-url", m)) => { - let email_address = m.value_of("input").unwrap(); + WkdSubcommands::DirectUrl(c) => { + let email_address = c.email_address; let wkd_url = wkd::Url::from(email_address)?; let url = wkd_url.to_url(wkd::Variant::Direct)?; println!("{}", url); }, - Some(("get", m)) => { + WkdSubcommands::Get(c) => { // Check that the policy allows https. network_policy.assert(net::Policy::Encrypted)?; - let email_address = m.value_of("input").unwrap(); + let email_address = c.email_address; // XXX: EmailAddress could be created here to // check it's a valid email address, print the error to // stderr and exit. @@ -137,17 +129,15 @@ pub fn dispatch_wkd(config: Config, m: &clap::ArgMatches) -> Result<()> { // But to keep the parallelism with `store export` and `keyserver get`, // The output is armored if not `--binary` option is given. let mut output = - config.create_or_stdout_safe(m.value_of("output"))?; - serialize_keyring(&mut output, &certs, - m.is_present("binary"))?; + config.create_or_stdout_safe(c.output.as_deref())?; + serialize_keyring(&mut output, &certs, c.binary)?; }, - Some(("generate", m)) => { - let domain = m.value_of("domain").unwrap(); - let skip = m.is_present("skip"); - let f = open_or_stdin(m.value_of("input"))?; - let base_path = - m.value_of("base-directory").expect("required"); - let variant = if m.is_present("direct-method") { + WkdSubcommands::Generate(c) => { + let domain = c.domain; + let skip = c.skip; + let f = open_or_stdin(c.input.as_deref())?; + let base_path = c.base_directory; + let variant = if c.direct_method { wkd::Variant::Direct } else { wkd::Variant::Advanced @@ -162,8 +152,8 @@ pub fn dispatch_wkd(config: Config, m: &clap::ArgMatches) -> Result<()> { e @ Err(_) if !skip => e?, _ => continue, }; - if wkd::cert_contains_domain_userid(domain, &vc) { - wkd::insert(&base_path, domain, variant, &vc) + if wkd::cert_contains_domain_userid(&domain, &vc) { + wkd::insert(&base_path, &domain, variant, &vc) .context(format!("Failed to generate the WKD in \ {}.", base_path))?; } else if !skip { @@ -174,7 +164,6 @@ pub fn dispatch_wkd(config: Config, m: &clap::ArgMatches) -> Result<()> { } } }, - _ => unreachable!(), } Ok(()) diff --git a/sq/src/sq.rs b/sq/src/sq.rs index fa25bac6..66d2245b 100644 --- a/sq/src/sq.rs +++ b/sq/src/sq.rs @@ -730,7 +730,11 @@ fn main() -> Result<()> { Some(("revoke", m)) => commands::revoke::dispatch(config, m)?, - Some(("wkd", m)) => commands::net::dispatch_wkd(config, m)?, + Some(("wkd", m)) => { + use clap::FromArgMatches; + let command = sq_cli::WkdCommand::from_arg_matches(m)?; + commands::net::dispatch_wkd(config, command)? + }, Some(("certify", m)) => { commands::certify::certify(config, m)?; diff --git a/sq/src/sq_cli.rs b/sq/src/sq_cli.rs index d54f3ccd..c5588a9e 100644 --- a/sq/src/sq_cli.rs +++ b/sq/src/sq_cli.rs @@ -2053,7 +2053,7 @@ pub struct WkdUrlCommand { value_name = "ADDRESS", help = "Queries for ADDRESS", )] - pub input: String, + pub email_address: String, } #[derive(Debug, Args)] @@ -2065,7 +2065,7 @@ pub struct WkdDirectUrlCommand { value_name = "ADDRESS", help = "Queries for ADDRESS", )] - pub input: String, + pub email_address: String, } #[derive(Debug, Args)] @@ -2077,7 +2077,7 @@ pub struct WkdGetCommand { value_name = "ADDRESS", help = "Queries a cert for ADDRESS", )] - pub input: String, + pub email_address: String, #[clap( short = 'B', long, |