Skip to content
Snippets Groups Projects

Resolve "Keys clean subcommand : Indicate to Structopt that the user must indicate which keypair to delete"

@@ -21,6 +21,9 @@ use crate::DursCore;
use clear_on_drop::clear::Clear;
use durs_conf::keys::*;
use durs_conf::DuRsConf;
use std::error;
use std::fmt;
use std::str::FromStr;
#[derive(StructOpt, Debug, Clone)]
#[structopt(
@@ -94,17 +97,57 @@ pub enum ModifySubCommand {
#[derive(StructOpt, Debug, Copy, Clone)]
/// ClearOpt
pub struct ClearOpt {
#[structopt(short = "m", long = "member")]
/// True if we change member key
pub member: bool,
/// Clear key [MEMBER, NETWORK, ALL]
key: KeyKind,
}
#[derive(Debug, Copy, Clone, PartialEq)]
/// KeyKind
pub enum KeyKind {
/// Member key
MEMBER,
/// Network key
NETWORK,
/// Both Member and Network keys
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
}
}
impl FromStr for KeyKind {
type Err = ParseKeyKindError;
fn from_str(level: &str) -> Result<KeyKind, Self::Err> {
match level.to_ascii_uppercase().as_str() {
"MEMBER" => Ok(KeyKind::MEMBER),
"NETWORK" => Ok(KeyKind::NETWORK),
"ALL" => Ok(KeyKind::ALL),
_ => Err(ParseKeyKindError(())),
}
}
}
/// The type returned by [`from_str`] when the string doesn't match any of the clear kinds.
///
/// [`from_str`]: https://doc.rust-lang.org/std/str/trait.FromStr.html#tymethod.from_str
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ParseKeyKindError(());
#[structopt(short = "n", long = "network")]
/// True if we change network key
pub network: bool,
impl fmt::Display for ParseKeyKindError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("attempt to convert a string that doesn't match a kind of keys")
}
}
#[structopt(short = "a", long = "all")]
/// True if we change member and network key
pub all: bool,
impl error::Error for ParseKeyKindError {
fn description(&self) -> &str {
"attempt to convert a string that doesn't match a kind of keys"
}
}
#[derive(StructOpt, Debug, Clone)]
@@ -169,8 +212,8 @@ impl DursExecutableCoreCommand for KeysOpt {
},
KeysSubCommand::Clear(clear_opt) => {
let new_keypairs = clear_keys(
clear_opt.network || clear_opt.all,
clear_opt.member || clear_opt.all,
clear_opt.key.is_network(),
clear_opt.key.is_member(),
keypairs,
);
save_keypairs(profile_path, &keypairs_file, new_keypairs)
Loading