From c8db48074625680030fd8364097cc1a68f852b9d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 27 Dec 2018 15:55:03 +0100 Subject: Implement the pws command This patch adds the basic structure for the pws command that can be used to access the password safe on the Nitrokey Pro and Nitrokey Storage. --- nitrocli/src/args.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) (limited to 'nitrocli/src/args.rs') diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index f00ac2a..df7f56c 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -34,6 +34,7 @@ pub enum Command { Config, Otp, Pin, + Pws, Status, Storage, } @@ -45,6 +46,7 @@ impl Command { Command::Config => config(args), Command::Otp => otp(args), Command::Pin => pin(args), + Command::Pws => pws(args), Command::Status => status(args), Command::Storage => storage(args), } @@ -60,6 +62,7 @@ impl fmt::Display for Command { Command::Config => "config", Command::Otp => "otp", Command::Pin => "pin", + Command::Pws => "pws", Command::Status => "status", Command::Storage => "storage", } @@ -75,6 +78,7 @@ impl str::FromStr for Command { "config" => Ok(Command::Config), "otp" => Ok(Command::Otp), "pin" => Ok(Command::Pin), + "pws" => Ok(Command::Pws), "status" => Ok(Command::Status), "storage" => Ok(Command::Storage), _ => Err(()), @@ -320,6 +324,42 @@ impl str::FromStr for PinCommand { } } +#[derive(Debug)] +enum PwsCommand { + Get, +} + +impl PwsCommand { + fn execute(&self, _args: Vec) -> Result<()> { + match *self { + PwsCommand::Get => Err(Error::Error("Not implemented".to_string())), + } + } +} + +impl fmt::Display for PwsCommand { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match *self { + PwsCommand::Get => "get", + } + ) + } +} + +impl str::FromStr for PwsCommand { + type Err = (); + + fn from_str(s: &str) -> result::Result { + match s { + "get" => Ok(PwsCommand::Get), + _ => Err(()), + } + } +} + fn parse(parser: &argparse::ArgumentParser<'_>, args: Vec) -> Result<()> { if let Err(err) = parser.parse(args, &mut io::stdout(), &mut io::stderr()) { Err(Error::ArgparseError(err)) @@ -747,6 +787,30 @@ fn pin_unblock(args: Vec) -> Result<()> { commands::pin_unblock() } +/// Execute a PWS subcommand. +fn pws(args: Vec) -> Result<()> { + let mut subcommand = PwsCommand::Get; + let mut subargs = vec![]; + let mut parser = argparse::ArgumentParser::new(); + parser.set_description("Accesses the password safe"); + let _ = parser.refer(&mut subcommand).required().add_argument( + "subcommand", + argparse::Store, + "The subcommand to execute (get)", + ); + let _ = parser.refer(&mut subargs).add_argument( + "arguments", + argparse::List, + "The arguments for the subcommand", + ); + parser.stop_on_first_argument(true); + parse(&parser, args)?; + drop(parser); + + subargs.insert(0, format!("nitrocli pws {}", subcommand)); + subcommand.execute(subargs) +} + /// 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)> { @@ -757,7 +821,7 @@ fn parse_arguments(args: Vec) -> Result<(Command, Vec)> { let _ = parser.refer(&mut command).required().add_argument( "command", argparse::Store, - "The command to execute (config|otp|pin|status|storage)", + "The command to execute (config|otp|pin|pws|status|storage)", ); let _ = parser.refer(&mut subargs).add_argument( "arguments", -- cgit v1.2.1