Skip to content

Commit

Permalink
linkd: Flatten key related args
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Simmerl <[email protected]>
  • Loading branch information
xla committed Sep 7, 2021
1 parent cd4257e commit a6b5f7a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
45 changes: 31 additions & 14 deletions linkd/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions linkd/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ where
match args.signer {
args::Signer::SshAgent => keys::signer_ssh::<S>(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??;
Expand All @@ -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(), &())?
Expand Down
21 changes: 15 additions & 6 deletions test/src/test/unit/linkd/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
);
Expand All @@ -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()
}
);
Expand All @@ -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()
}
);
Expand Down

0 comments on commit a6b5f7a

Please sign in to comment.