From 72350424b03836129429630132728d025ffcdcce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Lehmann?= Date: Sun, 4 Feb 2024 08:48:06 +0100 Subject: [PATCH] add a -p option in keygen to save the public key --- README.md | 3 +-- doc/CommandLineHelp.md | 1 + src/cli.rs | 4 ++++ src/keygen.rs | 8 ++++++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 30a1a10..1337667 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ $ yage --completion fish > ~/.config/fish/completions/yage.fish First generate a new age key pair: ```sh -$ yage keygen -o prod.key +$ yage keygen -o prod.key -p prod.pub Public key: age15eesfkh778yljxzgwdq5vaqmmchg5py480vplsymzzqf0dwe5gnqrexdq6 ``` @@ -112,7 +112,6 @@ AGE-SECRET-KEY-1EZEU9RUTW3K5GV98ER6RHMS73QJNQ37ARWG6MWHXM4JP8FVD3A9QK2DD70 The public key could be committed to a git repository: ```sh -$ yage pubkey -K prod.key -o prod.pub $ git add prod.pub $ git commit -m "Add prod public key" ``` diff --git a/doc/CommandLineHelp.md b/doc/CommandLineHelp.md index f8a3a1e..a42773d 100644 --- a/doc/CommandLineHelp.md +++ b/doc/CommandLineHelp.md @@ -160,6 +160,7 @@ The key is written in the age format, which is compatible with the age tool. * `-o`, `--output ` — The output path to the private key file Default value: `-` +* `-p`, `--public ` — The output path to the public key file diff --git a/src/cli.rs b/src/cli.rs index d770e3b..7e1f273 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -41,6 +41,10 @@ pub struct KeygenArgs { /// The private key is written to the standard output by default. #[clap(short, long, default_value = "-")] pub output: PathBuf, + + /// The output path to the public key file + #[clap(short, long)] + pub public: Option, } /// Convert private age keys to their public key diff --git a/src/keygen.rs b/src/keygen.rs index 70c3ea9..2f90c15 100644 --- a/src/keygen.rs +++ b/src/keygen.rs @@ -6,9 +6,13 @@ use crate::error::{IOResultExt, Result}; use crate::util::stdout_or_file; pub fn keygen(args: &KeygenArgs) -> Result<()> { - let mut output = stdout_or_file(&args.output)?; let key = Identity::generate(); - info!("Public key: {}", key.to_public()); + let mut output = stdout_or_file(&args.output)?; writeln!(output, "{}", key.to_string().expose_secret()).path_ctx(&args.output)?; + info!("Public key: {}", key.to_public()); + if let Some(ref public) = args.public { + let mut output = stdout_or_file(public)?; + writeln!(output, "{}", key.to_public()).path_ctx(public)?; + } Ok(()) }