summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNora Widdecke <nora@sequoia-pgp.org>2022-06-02 17:29:44 +0200
committerNora Widdecke <nora@sequoia-pgp.org>2022-06-08 14:49:43 +0200
commit602fb1411b8715d927291e4ba9f7f3bdf3be2bef (patch)
tree69f5bff75202db95740adc52a3d26f4814741797
parente5e30e23f77ff9d9c74b7c2f377c8a471619fc15 (diff)
sq: Derive autocrypt subcommand.
- This is part of the effort of moving to clap3's derive API and profit from the added type safety.
-rw-r--r--sq/src/sq-usage.rs2
-rw-r--r--sq/src/sq_cli.rs208
2 files changed, 119 insertions, 91 deletions
diff --git a/sq/src/sq-usage.rs b/sq/src/sq-usage.rs
index e94be2ce..e80dcde5 100644
--- a/sq/src/sq-usage.rs
+++ b/sq/src/sq-usage.rs
@@ -1113,7 +1113,7 @@
//! -o, --output <FILE>
//! Writes to FILE or stdout if omitted
//!
-//! --prefer-encrypt <prefer-encrypt>
+//! --prefer-encrypt <PREFER-ENCRYPT>
//! Sets the prefer-encrypt attribute
//!
//! [default: nopreference]
diff --git a/sq/src/sq_cli.rs b/sq/src/sq_cli.rs
index 1b328612..2caf4bec 100644
--- a/sq/src/sq_cli.rs
+++ b/sq/src/sq_cli.rs
@@ -1,5 +1,5 @@
/// Command-line parser for sq.
-use clap::{Arg, ArgGroup, Command, ArgEnum, Args};
+use clap::{Arg, ArgGroup, Command, ArgEnum, Args, Subcommand};
use clap::{CommandFactory, Parser};
pub fn build() -> Command<'static> {
@@ -1618,95 +1618,7 @@ as being human readable."))
app
} else {
// With Autocrypt support.
- app.subcommand(
- Command::new("autocrypt")
- .display_order(400)
- .about("Communicates certificates using Autocrypt")
- .long_about(
-"Communicates certificates using Autocrypt
-
-Autocrypt is a standard for mail user agents to provide convenient
-end-to-end encryption of emails. This subcommand provides a limited
-way to produce and consume headers that are used by Autocrypt to
-communicate certificates between clients.
-
-See https://autocrypt.org/
-")
- .subcommand_required(true)
- .arg_required_else_help(true)
- .subcommand(
- Command::new("decode")
- .about("Reads Autocrypt-encoded certificates")
- .long_about(
-"Reads Autocrypt-encoded certificates
-
-Given an autocrypt header (or an key-gossip header), this command
-extracts the certificate encoded within it.
-
-The converse operation is \"sq autocrypt encode-sender\".
-")
- .after_help(
-"EXAMPLES:
-
-# Extract all certificates from a mail
-$ sq autocrypt decode autocrypt.eml
-")
- .arg(Arg::new("input")
- .value_name("FILE")
- .help("Reads from FILE or stdin if omitted"))
- .arg(Arg::new("output")
- .short('o').long("output").value_name("FILE")
- .help("Writes to FILE or stdout if omitted"))
- .arg(Arg::new("binary")
- .short('B').long("binary")
- .help("Emits binary data"))
- )
- .subcommand(
- Command::new("encode-sender")
- .about("Encodes a certificate into \
- an Autocrypt header")
- .long_about(
-"Encodes a certificate into an Autocrypt header
-
-A certificate can be encoded and included in a header of an email
-message. This command encodes the certificate, adds the senders email
-address (which must match the one used in the \"From\" header), and the
-senders \"prefer-encrypt\" state (see the Autocrypt spec for more
-information).
-
-The converse operation is \"sq autocrypt decode\".
-")
- .after_help(
-"EXAMPLES:
-
-# Encodes a certificate
-$ sq autocrypt encode-sender juliet.pgp
-
-# Encodes a certificate with an explicit sender address
-$ sq autocrypt encode-sender --email juliet@example.org juliet.pgp
-
-# Encodes a certificate while indicating the willingness to encrypt
-$ sq autocrypt encode-sender --prefer-encrypt mutual juliet.pgp
-")
- .arg(Arg::new("input")
- .value_name("FILE")
- .help("Reads from FILE or stdin if omitted"))
- .arg(Arg::new("output")
- .short('o').long("output").value_name("FILE")
- .help("Writes to FILE or stdout if omitted"))
- .arg(Arg::new("address")
- .long("email").value_name("ADDRESS")
- .help("Sets the address \
- [default: primary userid]"))
- .arg(Arg::new("prefer-encrypt")
- .long("prefer-encrypt")
- .possible_values(&["nopreference",
- "mutual"])
- .default_value("nopreference")
- .help("Sets the prefer-encrypt \
- attribute"))
- )
- )
+ app.subcommand(AutocryptCommand::command())
}
.subcommand(ArmorCommand::command())
.subcommand(DearmorCommand::command())
@@ -2023,3 +1935,119 @@ pub struct SignCommand {
// TODO: Also, no need for the Option
pub notation: Option<Vec<String>>,
}
+
+#[derive(Parser, Debug)]
+#[clap(name = "autocrypt", display_order(400))]
+#[clap(
+ about = "Communicates certificates using Autocrypt",
+ long_about =
+"Communicates certificates using Autocrypt
+
+Autocrypt is a standard for mail user agents to provide convenient
+end-to-end encryption of emails. This subcommand provides a limited
+way to produce and consume headers that are used by Autocrypt to
+communicate certificates between clients.
+
+See https://autocrypt.org/
+",
+ subcommand_required = true,
+ arg_required_else_help = true,
+)]
+pub struct AutocryptCommand {
+ #[clap(subcommand)]
+ subcommand: AutocryptSubCommands,
+}
+
+#[derive(Debug, Subcommand)]
+pub enum AutocryptSubCommands {
+ Decode(AutocyptDecodeCommand),
+
+ EncodeSender(AutocyptEncodeSenderCommand),
+}
+#[derive(Debug, Args)]
+#[clap(
+ about = "Reads Autocrypt-encoded certificates",
+ long_about =
+"Reads Autocrypt-encoded certificates
+
+Given an autocrypt header (or an key-gossip header), this command
+extracts the certificate encoded within it.
+
+The converse operation is \"sq autocrypt encode-sender\".
+",
+ after_help =
+"EXAMPLES:
+
+# Extract all certificates from a mail
+$ sq autocrypt decode autocrypt.eml
+",
+)]
+pub struct AutocyptDecodeCommand {
+ #[clap(flatten)]
+ pub io: IoArgs,
+ #[clap(
+ short = 'B',
+ long,
+ help = "Emits binary data",
+ )]
+ pub binary: bool,
+}
+
+//#[derive(Subcommand)]
+#[derive(Debug, Args)]
+#[clap(
+ name = "encode-sender",
+ about = "Encodes a certificate into an Autocrypt header",
+ long_about =
+"Encodes a certificate into an Autocrypt header
+
+A certificate can be encoded and included in a header of an email
+message. This command encodes the certificate, adds the senders email
+address (which must match the one used in the \"From\" header), and the
+senders \"prefer-encrypt\" state (see the Autocrypt spec for more
+information).
+
+The converse operation is \"sq autocrypt decode\".
+",
+ after_help =
+"EXAMPLES:
+
+# Encodes a certificate
+$ sq autocrypt encode-sender juliet.pgp
+
+# Encodes a certificate with an explicit sender address
+$ sq autocrypt encode-sender --email juliet@example.org juliet.pgp
+
+# Encodes a certificate while indicating the willingness to encrypt
+$ sq autocrypt encode-sender --prefer-encrypt mutual juliet.pgp
+",
+)]
+pub struct AutocyptEncodeSenderCommand {
+ #[clap(flatten)]
+ pub io: IoArgs,
+ // TODO the help message looks like "primary userid" might be the default
+ // email. clarify
+ #[clap(
+ long = "email",
+ value_name = "ADDRESS",
+ help = "Sets the address [default: primary userid]",
+ )]
+ pub address: Option<String>,
+ #[clap(
+ long = "prefer-encrypt",
+ value_name = "PREFER-ENCRYPT",
+ default_value_t = PreferEncryptArgs::NoPreference,
+ help = "Sets the prefer-encrypt attribute",
+ arg_enum,
+ )]
+ pub prefer_encrypt: PreferEncryptArgs,
+
+}
+
+#[derive(ArgEnum)]
+#[derive(Debug, Clone)]
+pub enum PreferEncryptArgs {
+ #[clap(name = "nopreference")]
+ NoPreference,
+ Mutual
+}