Skip to content
Snippets Groups Projects
Commit d385f7f7 authored by dvermd's avatar dvermd
Browse files

[fix] core:commands: #108 rework keys cli options

parent 8ad244ae
No related branches found
No related tags found
1 merge request!252Resolve "Keys clean subcommand : Indicate to Structopt that the user must indicate which keypair to delete"
...@@ -1031,6 +1031,7 @@ name = "durs-core" ...@@ -1031,6 +1031,7 @@ name = "durs-core"
version = "0.3.0-dev" version = "0.3.0-dev"
dependencies = [ dependencies = [
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-currency-params 0.2.0", "dubp-currency-params 0.2.0",
......
...@@ -71,16 +71,46 @@ pub fn modify_member_keys( ...@@ -71,16 +71,46 @@ pub fn modify_member_keys(
key_pairs key_pairs
} }
/// Clear keys command /// Ask user for confirmation and Clear keys command
pub fn clear_keys(network: bool, member: bool, mut key_pairs: DuniterKeyPairs) -> DuniterKeyPairs { pub fn clear_keys(network: bool, member: bool, key_pairs: DuniterKeyPairs) -> DuniterKeyPairs {
inner_clear_keys(
if network { if network {
key_pairs.network_keypair = generate_random_keypair(KeysAlgo::Ed25519); match question_prompt("Clear your network keypair?", &["y", "n"]) {
Ok(answer) if answer == "y" => {
println!("Generating a new network keypair!");
true
}
_ => false,
} }
} else {
false
},
if member { if member {
key_pairs.member_keypair = None; match question_prompt("Clear your member keypair?", &["y", "n"]) {
Ok(answer) if answer == "y" => {
println!("Deleting member keypair!");
true
}
_ => false,
}
} else {
false
},
key_pairs,
)
}
/// Private function to Clear keys
fn inner_clear_keys(
network: bool,
member: bool,
mut key_pairs: DuniterKeyPairs,
) -> DuniterKeyPairs {
if network {
key_pairs.network_keypair = generate_random_keypair(KeysAlgo::Ed25519);
} }
if !network && !member { if member {
println!("No key was cleared. Please specify a key to clear.") key_pairs.member_keypair = None
} }
key_pairs key_pairs
} }
...@@ -111,7 +141,7 @@ pub fn save_keypairs( ...@@ -111,7 +141,7 @@ pub fn save_keypairs(
Ok(()) Ok(())
} }
fn question_prompt(question: &str, answers: Vec<String>) -> Result<String, WizardError> { fn question_prompt<'a>(question: &str, answers: &[&'a str]) -> Result<&'a str, WizardError> {
let mut buf = String::new(); let mut buf = String::new();
println!("{} ({}):", question, answers.join("/")); println!("{} ({}):", question, answers.join("/"));
...@@ -119,9 +149,9 @@ fn question_prompt(question: &str, answers: Vec<String>) -> Result<String, Wizar ...@@ -119,9 +149,9 @@ fn question_prompt(question: &str, answers: Vec<String>) -> Result<String, Wizar
match res { match res {
Ok(_) => { Ok(_) => {
let answer = answers.into_iter().find(|x| x == buf.trim()); let answer = answers.iter().find(|x| **x == buf.trim());
match answer { match answer {
Some(value) => Ok(value), Some(&value) => Ok(value),
None => Err(WizardError::Canceled), None => Err(WizardError::Canceled),
} }
} }
...@@ -149,18 +179,12 @@ fn salt_password_prompt() -> Result<KeyPairEnum, WizardError> { ...@@ -149,18 +179,12 @@ fn salt_password_prompt() -> Result<KeyPairEnum, WizardError> {
/// The wizard key function /// The wizard key function
pub fn key_wizard(mut key_pairs: DuniterKeyPairs) -> Result<DuniterKeyPairs, WizardError> { pub fn key_wizard(mut key_pairs: DuniterKeyPairs) -> Result<DuniterKeyPairs, WizardError> {
let mut answer = question_prompt( let mut answer = question_prompt("Modify your network keypair?", &["y", "n"])?;
"Modify your network keypair?",
vec!["y".to_string(), "n".to_string()],
)?;
if answer == "y" { if answer == "y" {
key_pairs.network_keypair = salt_password_prompt()?; key_pairs.network_keypair = salt_password_prompt()?;
} }
answer = question_prompt( answer = question_prompt("Modify your member keypair?", &["y", "n", "d"])?;
"Modify your member keypair?",
vec!["y".to_string(), "n".to_string(), "d".to_string()],
)?;
if answer == "y" { if answer == "y" {
key_pairs.member_keypair = Some(salt_password_prompt()?); key_pairs.member_keypair = Some(salt_password_prompt()?);
} else if answer == "d" { } else if answer == "d" {
...@@ -281,7 +305,7 @@ mod tests { ...@@ -281,7 +305,7 @@ mod tests {
.expect("conf : keypairs file : fail to parse network_pub !"), .expect("conf : keypairs file : fail to parse network_pub !"),
})), })),
}; };
let result_key_pairs = clear_keys(true, false, key_pairs); let result_key_pairs = inner_clear_keys(true, false, key_pairs);
// We expect network key to be reset to a new random key // We expect network key to be reset to a new random key
assert_ne!( assert_ne!(
result_key_pairs.network_keypair.public_key(), result_key_pairs.network_keypair.public_key(),
...@@ -337,7 +361,7 @@ mod tests { ...@@ -337,7 +361,7 @@ mod tests {
.expect("conf : keypairs file : fail to parse network_pub !"), .expect("conf : keypairs file : fail to parse network_pub !"),
})), })),
}; };
let result_key_pairs = clear_keys(false, true, key_pairs); let result_key_pairs = inner_clear_keys(false, true, key_pairs);
// We expect network key not to change // We expect network key not to change
assert_eq!( assert_eq!(
result_key_pairs.network_keypair.public_key(), result_key_pairs.network_keypair.public_key(),
......
...@@ -11,6 +11,7 @@ path = "src/lib.rs" ...@@ -11,6 +11,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
clap = "2.33.0"
clear_on_drop = "0.2.3" clear_on_drop = "0.2.3"
dirs = "2.0.2" dirs = "2.0.2"
durs-bc = { path = "../../modules/blockchain/blockchain" } durs-bc = { path = "../../modules/blockchain/blockchain" }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
use crate::commands::DursExecutableCoreCommand; use crate::commands::DursExecutableCoreCommand;
use crate::errors::DursCoreError; use crate::errors::DursCoreError;
use crate::DursCore; use crate::DursCore;
use clap::arg_enum;
use clear_on_drop::clear::Clear; use clear_on_drop::clear::Clear;
use durs_conf::keys::*; use durs_conf::keys::*;
use durs_conf::DuRsConf; use durs_conf::DuRsConf;
...@@ -91,20 +92,33 @@ pub enum ModifySubCommand { ...@@ -91,20 +92,33 @@ pub enum ModifySubCommand {
NetworkSaltPassword(SaltPasswordOpt), NetworkSaltPassword(SaltPasswordOpt),
} }
arg_enum! {
/// KeyKind
#[derive(Debug, Copy, Clone, PartialEq)]
enum KeyKind {
MEMBER,
NETWORK,
ALL,
}
}
impl KeyKind {
/// Returns if key kind is member
pub fn is_member(self) -> bool {
self == KeyKind::MEMBER || self == KeyKind::ALL
}
/// Returns if key kind is network
pub fn is_network(self) -> bool {
self == KeyKind::NETWORK || self == KeyKind::ALL
}
}
#[derive(StructOpt, Debug, Copy, Clone)] #[derive(StructOpt, Debug, Copy, Clone)]
/// ClearOpt /// ClearOpt
pub struct ClearOpt { pub struct ClearOpt {
#[structopt(short = "m", long = "member")] /// Key to clear
/// True if we change member key #[structopt(possible_values = &KeyKind::variants(), case_insensitive = true)]
pub member: bool, key: KeyKind,
#[structopt(short = "n", long = "network")]
/// True if we change network key
pub network: bool,
#[structopt(short = "a", long = "all")]
/// True if we change member and network key
pub all: bool,
} }
#[derive(StructOpt, Debug, Clone)] #[derive(StructOpt, Debug, Clone)]
...@@ -169,8 +183,8 @@ impl DursExecutableCoreCommand for KeysOpt { ...@@ -169,8 +183,8 @@ impl DursExecutableCoreCommand for KeysOpt {
}, },
KeysSubCommand::Clear(clear_opt) => { KeysSubCommand::Clear(clear_opt) => {
let new_keypairs = clear_keys( let new_keypairs = clear_keys(
clear_opt.network || clear_opt.all, clear_opt.key.is_network(),
clear_opt.member || clear_opt.all, clear_opt.key.is_member(),
keypairs, keypairs,
); );
save_keypairs(profile_path, &keypairs_file, new_keypairs) save_keypairs(profile_path, &keypairs_file, new_keypairs)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment