From 6a69bbf736966c493cead716026db92c5b44962a Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 27 Dec 2018 17:27:08 +0100 Subject: Implement the pws clear subcommand This patch implements the pws clear command which allows the user to clear a slot in the password safe. --- nitrocli/src/args.rs | 22 +++++++++++++++++++++- nitrocli/src/commands.rs | 9 +++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index 9982d0f..28ccb1f 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -326,6 +326,7 @@ impl str::FromStr for PinCommand { #[derive(Debug)] enum PwsCommand { + Clear, Get, Set, } @@ -333,6 +334,7 @@ enum PwsCommand { impl PwsCommand { fn execute(&self, args: Vec) -> Result<()> { match *self { + PwsCommand::Clear => pws_clear(args), PwsCommand::Get => pws_get(args), PwsCommand::Set => pws_set(args), } @@ -345,6 +347,7 @@ impl fmt::Display for PwsCommand { f, "{}", match *self { + PwsCommand::Clear => "clear", PwsCommand::Get => "get", PwsCommand::Set => "set", } @@ -357,6 +360,7 @@ impl str::FromStr for PwsCommand { fn from_str(s: &str) -> result::Result { match s { + "clear" => Ok(PwsCommand::Clear), "get" => Ok(PwsCommand::Get), "set" => Ok(PwsCommand::Set), _ => Err(()), @@ -800,7 +804,7 @@ fn pws(args: Vec) -> Result<()> { let _ = parser.refer(&mut subcommand).required().add_argument( "subcommand", argparse::Store, - "The subcommand to execute (get|set)", + "The subcommand to execute (clear|get|set)", ); let _ = parser.refer(&mut subargs).add_argument( "arguments", @@ -889,6 +893,22 @@ fn pws_set(args: Vec) -> Result<()> { commands::pws_set(slot, &name, &login, &password) } +/// Clear a PWS slot. +fn pws_clear(args: Vec) -> Result<()> { + let mut slot: u8 = 0; + let mut parser = argparse::ArgumentParser::new(); + parser.set_description("Clears a password safe slot"); + let _ = parser.refer(&mut slot).required().add_argument( + "slot", + argparse::Store, + "The PWS slot to clear", + ); + parse(&parser, args)?; + drop(parser); + + commands::pws_clear(slot) +} + /// Parse the command-line arguments and return the selected command and /// the remaining arguments for the command. fn parse_arguments(args: Vec) -> Result<(Command, Vec)> { diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index e1c88c8..4deb1a7 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -593,6 +593,15 @@ pub fn pws_set(slot: u8, name: &str, login: &str, password: &str) -> Result<()> .map_err(|err| get_error("Could not write PWS slot", &err)) } +/// Clear a PWS slot. +pub fn pws_clear(slot: u8) -> Result<()> { + let device = get_device()?; + let pws = get_password_safe(&device)?; + pws + .erase_slot(slot) + .map_err(|err| get_error("Could not clear PWS slot", &err)) +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3