From a6b5f7a7c0631fb06b6c50cf41486feeb4d5e722 Mon Sep 17 00:00:00 2001 From: Alexander Simmerl Date: Tue, 7 Sep 2021 17:56:49 +1000 Subject: [PATCH] linkd: Flatten key related args Signed-off-by: Alexander Simmerl --- linkd/src/args.rs | 45 ++++++++++++++++++++++---------- linkd/src/cfg.rs | 6 ++--- test/src/test/unit/linkd/args.rs | 21 ++++++++++----- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/linkd/src/args.rs b/linkd/src/args.rs index 85ed78181..858531cdc 100644 --- a/linkd/src/args.rs +++ b/linkd/src/args.rs @@ -25,20 +25,8 @@ pub struct Args { #[structopt(long, default_value)] pub signer: Signer, - /// Location of the key file on disk. - #[structopt( - long, - default_value = "", - parse(from_str), - required_if("key-source", "file") - )] - pub key_file_path: PathBuf, - /// Format of the key input data. - #[structopt(long, default_value, required_if("signer", "key"))] - pub key_format: KeyFormat, - /// Specifies from which source the secret should be read. - #[structopt(long, default_value, required_if("signer", "key"))] - pub key_source: KeySource, + #[structopt(flatten)] + pub key: KeyArgs, } fn parse_rad_home(src: &str) -> RadHome { @@ -85,6 +73,35 @@ impl FromStr for Signer { } } +#[derive(Debug, Default, Eq, PartialEq, StructOpt)] +pub struct KeyArgs { + /// Location of the key file on disk. + #[structopt( + long = "key-file-path", + name = "key-file-path", + default_value = "", + parse(from_str), + required_if("key-source", "file") + )] + pub file_path: PathBuf, + /// Format of the key input data. + #[structopt( + long = "key-format", + name = "key-format", + default_value, + required_if("signer", "key") + )] + pub format: KeyFormat, + /// Specifies from which source the secret should be read. + #[structopt( + long = "key-source", + name = "key-source", + default_value, + required_if("signer", "key") + )] + pub source: KeySource, +} + #[derive(Debug, Eq, PartialEq, StructOpt)] pub enum KeyFormat { Base64, diff --git a/linkd/src/cfg.rs b/linkd/src/cfg.rs index eebc746f8..47fa4e606 100644 --- a/linkd/src/cfg.rs +++ b/linkd/src/cfg.rs @@ -113,9 +113,9 @@ where match args.signer { args::Signer::SshAgent => keys::signer_ssh::(profile).await.map_err(Error::from), args::Signer::Key => { - let bytes = match args.key_source { + let bytes = match args.key.source { args::KeySource::File => { - let mut file = File::open(args.key_file_path).await?; + let mut file = File::open(args.key.file_path).await?; let mut bytes = vec![]; timeout(Duration::from_secs(5), file.read_to_end(&mut bytes)).await??; @@ -129,7 +129,7 @@ where }, }; - let key = match args.key_format { + let key = match args.key.format { args::KeyFormat::Base64 => { let bs = base64::decode(bytes)?; SecretKey::from_bytes_and_meta(bs.into(), &())? diff --git a/test/src/test/unit/linkd/args.rs b/test/src/test/unit/linkd/args.rs index d71d71f9f..5b1503466 100644 --- a/test/src/test/unit/linkd/args.rs +++ b/test/src/test/unit/linkd/args.rs @@ -90,8 +90,11 @@ fn signer_key_file() -> Result<()> { parsed, Args { signer: args::Signer::Key, - key_source: args::KeySource::File, - key_file_path: PathBuf::from("~/.config/radicle/secret.key"), + key: args::KeyArgs { + source: args::KeySource::File, + file_path: PathBuf::from("~/.config/radicle/secret.key"), + ..Default::default() + }, ..Default::default() } ); @@ -109,9 +112,12 @@ fn signer_key_file() -> Result<()> { parsed, Args { signer: args::Signer::Key, - key_format: args::KeyFormat::Base64, - key_source: args::KeySource::File, - key_file_path: PathBuf::from("~/.config/radicle/secret.seed"), + key: args::KeyArgs { + format: args::KeyFormat::Base64, + source: args::KeySource::File, + file_path: PathBuf::from("~/.config/radicle/secret.seed"), + ..Default::default() + }, ..Default::default() } ); @@ -133,7 +139,10 @@ fn signer_key_stdin() -> Result<()> { parsed, Args { signer: args::Signer::Key, - key_source: args::KeySource::Stdin, + key: args::KeyArgs { + source: args::KeySource::Stdin, + ..Default::default() + }, ..Default::default() } );