From c3b9df0dfa9ef2de2a800c4fbc5880a49da0d9bb Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Thu, 27 Dec 2018 20:03:47 -0800 Subject: Make 'open' and 'close' subcommands of new 'storage' command Upon their inception, the 'open' and 'close' commands were pretty much the only relevant commands the program provided and it made sense to have them reside in the root namespace. By now we support more commands and have started to structure them in a more hierarchical fashion. To go with the flow, this change introduces a new 'storage' command and makes the existing 'open' and 'close' commands subcommands of it. We chose the name 'storage' (over, say, 'volume') because we plan to move the printing of the storage related status from the 'status' root level command into a subcommand within 'storage'. --- nitrocli/src/args.rs | 88 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 14 deletions(-) (limited to 'nitrocli/src/args.rs') diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index 36da560..d7a6d25 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -31,11 +31,10 @@ type Result = result::Result; #[derive(Debug)] pub enum Command { Clear, - Close, Config, - Open, Otp, Status, + Storage, } impl Command { @@ -43,11 +42,10 @@ impl Command { pub fn execute(&self, args: Vec) -> Result<()> { match *self { Command::Clear => clear(args), - Command::Close => close(args), Command::Config => config(args), - Command::Open => open(args), Command::Otp => otp(args), Command::Status => status(args), + Command::Storage => storage(args), } } } @@ -59,11 +57,10 @@ impl fmt::Display for Command { "{}", match *self { Command::Clear => "clear", - Command::Close => "close", Command::Config => "config", - Command::Open => "open", Command::Otp => "otp", Command::Status => "status", + Command::Storage => "storage", } ) } @@ -75,11 +72,10 @@ impl str::FromStr for Command { fn from_str(s: &str) -> result::Result { match s { "clear" => Ok(Command::Clear), - "close" => Ok(Command::Close), "config" => Ok(Command::Config), - "open" => Ok(Command::Open), "otp" => Ok(Command::Otp), "status" => Ok(Command::Status), + "storage" => Ok(Command::Storage), _ => Err(()), } } @@ -297,24 +293,88 @@ fn status(args: Vec) -> Result<()> { } /// Open the encrypted volume on the nitrokey. -fn open(args: Vec) -> Result<()> { +fn storage_open(args: Vec) -> Result<()> { let mut parser = argparse::ArgumentParser::new(); parser.set_description("Opens the encrypted volume on a Nitrokey Storage"); parse(&parser, args)?; - commands::open() + commands::storage_open() } /// Close the previously opened encrypted volume. -fn close(args: Vec) -> Result<()> { +fn storage_close(args: Vec) -> Result<()> { let mut parser = argparse::ArgumentParser::new(); parser.set_description("Closes the encrypted volume on a Nitrokey Storage"); parse(&parser, args)?; - commands::close() + commands::storage_close() +} + +#[derive(Debug)] +enum StorageCommand { + Close, + Open, +} + +impl StorageCommand { + fn execute(&self, args: Vec) -> Result<()> { + match *self { + StorageCommand::Close => storage_close(args), + StorageCommand::Open => storage_open(args), + } + } +} + +impl fmt::Display for StorageCommand { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match *self { + StorageCommand::Close => "close", + StorageCommand::Open => "open", + } + ) + } +} + +impl str::FromStr for StorageCommand { + type Err = (); + + fn from_str(s: &str) -> result::Result { + match s { + "close" => Ok(StorageCommand::Close), + "open" => Ok(StorageCommand::Open), + _ => Err(()), + } + } +} + +/// Execute a storage subcommand. +fn storage(args: Vec) -> Result<()> { + let mut subcommand = StorageCommand::Open; + let mut subargs = vec![]; + let mut parser = argparse::ArgumentParser::new(); + parser.set_description("Interacts with the device's storage"); + let _ = parser.refer(&mut subcommand).required().add_argument( + "subcommand", + argparse::Store, + "The subcommand to execute (open|close)", + ); + 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 storage {}", subcommand)); + subcommand.execute(subargs) } -/// Clear the PIN stored when opening the nitrokey's encrypted volume. +/// Clear the PIN as cached by various other commands. fn clear(args: Vec) -> Result<()> { let mut parser = argparse::ArgumentParser::new(); parser.set_description("Clears the cached passphrases"); @@ -584,7 +644,7 @@ fn parse_arguments(args: Vec) -> Result<(Command, Vec)> { let _ = parser.refer(&mut command).required().add_argument( "command", argparse::Store, - "The command to execute (clear|close|config|open|otp|status)", + "The command to execute (clear|config|otp|status|storage)", ); let _ = parser.refer(&mut subargs).add_argument( "arguments", -- cgit v1.2.1