Skip to content

Commit

Permalink
Use strings instead of Command variants
Browse files Browse the repository at this point in the history
In one of the next patches, we will add fields to some Command variants
to be able to use them with structopt.  Then we will no longer be able
to instantiate them directly, so we replace these instances for the
transition.

This patch also removes the cmd_help! macro that is no longer needed.
  • Loading branch information
robinkrahl authored and d-e-s-o committed Jan 7, 2020
1 parent 31a0cb9 commit d7f1557
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 42 deletions.
13 changes: 0 additions & 13 deletions src/arg_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,6 @@ macro_rules! fmt_enum {
}};
}

/// A macro for generating the help text for a command/subcommand. The
/// argument is the variable representing the command (which in turn is
/// an enum).
/// Note that the name of this variable is embedded into the help text!
macro_rules! cmd_help {
( $cmd:ident ) => {
format!(
concat!("The ", stringify!($cmd), " to execute ({})"),
fmt_enum!($cmd)
)
};
}

#[cfg(test)]
mod tests {
Enum! {Command, [
Expand Down
44 changes: 15 additions & 29 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Enum! {UnencryptedVolumeMode, [
/// Execute an unencrypted subcommand.
fn unencrypted(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut subcommand = UnencryptedCommand::Set;
let help = cmd_help!(subcommand);
let help = "".to_string();
let mut subargs = vec![];
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Interacts with the device's unencrypted volume");
Expand All @@ -288,12 +288,7 @@ fn unencrypted(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {

subargs.insert(
0,
format!(
"{} {} {}",
crate::NITROCLI,
Command::Unencrypted,
subcommand,
),
format!("{} {} {}", crate::NITROCLI, "unencrypted", subcommand,),
);
subcommand.execute(ctx, subargs)
}
Expand Down Expand Up @@ -322,7 +317,7 @@ Command! {EncryptedCommand, [
/// Execute an encrypted subcommand.
fn encrypted(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut subcommand = EncryptedCommand::Open;
let help = cmd_help!(subcommand);
let help = "".to_string();
let mut subargs = vec![];
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Interacts with the device's encrypted volume");
Expand All @@ -341,7 +336,7 @@ fn encrypted(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {

subargs.insert(
0,
format!("{} {} {}", crate::NITROCLI, Command::Encrypted, subcommand),
format!("{} {} {}", crate::NITROCLI, "encrypted", subcommand),
);
subcommand.execute(ctx, subargs)
}
Expand Down Expand Up @@ -373,7 +368,7 @@ Command! {HiddenCommand, [
/// Execute a hidden subcommand.
fn hidden(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut subcommand = HiddenCommand::Open;
let help = cmd_help!(subcommand);
let help = "".to_string();
let mut subargs = vec![];
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Interacts with the device's hidden volume");
Expand All @@ -392,7 +387,7 @@ fn hidden(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {

subargs.insert(
0,
format!("{} {} {}", crate::NITROCLI, Command::Hidden, subcommand),
format!("{} {} {}", crate::NITROCLI, "hidden", subcommand),
);
subcommand.execute(ctx, subargs)
}
Expand Down Expand Up @@ -444,7 +439,7 @@ fn hidden_close(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
/// Execute a config subcommand.
fn config(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut subcommand = ConfigCommand::Get;
let help = cmd_help!(subcommand);
let help = "".to_string();
let mut subargs = vec![];
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Reads or writes the device configuration");
Expand All @@ -463,7 +458,7 @@ fn config(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {

subargs.insert(
0,
format!("{} {} {}", crate::NITROCLI, Command::Config, subcommand),
format!("{} {} {}", crate::NITROCLI, "config", subcommand),
);
subcommand.execute(ctx, subargs)
}
Expand Down Expand Up @@ -556,7 +551,7 @@ fn lock(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
/// Execute an OTP subcommand.
fn otp(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut subcommand = OtpCommand::Get;
let help = cmd_help!(subcommand);
let help = "".to_string();
let mut subargs = vec![];
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Accesses one-time passwords");
Expand All @@ -573,10 +568,7 @@ fn otp(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
parser.stop_on_first_argument(true);
parse(ctx, parser, args)?;

subargs.insert(
0,
format!("{} {} {}", crate::NITROCLI, Command::Otp, subcommand),
);
subargs.insert(0, format!("{} {} {}", crate::NITROCLI, "otp", subcommand));
subcommand.execute(ctx, subargs)
}

Expand Down Expand Up @@ -725,7 +717,7 @@ fn otp_status(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
/// Execute a PIN subcommand.
fn pin(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut subcommand = PinCommand::Clear;
let help = cmd_help!(subcommand);
let help = "".to_string();
let mut subargs = vec![];
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Manages the Nitrokey PINs");
Expand All @@ -742,10 +734,7 @@ fn pin(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
parser.stop_on_first_argument(true);
parse(ctx, parser, args)?;

subargs.insert(
0,
format!("{} {} {}", crate::NITROCLI, Command::Pin, subcommand),
);
subargs.insert(0, format!("{} {} {}", crate::NITROCLI, "pin", subcommand));
subcommand.execute(ctx, subargs)
}

Expand Down Expand Up @@ -786,7 +775,7 @@ fn pin_unblock(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
fn pws(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut subcommand = PwsCommand::Get;
let mut subargs = vec![];
let help = cmd_help!(subcommand);
let help = "".to_string();
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Accesses the password safe");
let _ =
Expand All @@ -802,10 +791,7 @@ fn pws(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
parser.stop_on_first_argument(true);
parse(ctx, parser, args)?;

subargs.insert(
0,
format!("{} {} {}", crate::NITROCLI, Command::Pws, subcommand),
);
subargs.insert(0, format!("{} {} {}", crate::NITROCLI, "pws", subcommand));
subcommand.execute(ctx, subargs)
}

Expand Down Expand Up @@ -923,7 +909,7 @@ pub(crate) fn handle_arguments(ctx: &mut RunCtx<'_>, args: Vec<String>) -> Resul
);
let mut verbosity = 0;
let mut command = Command::Status;
let cmd_help = cmd_help!(command);
let cmd_help = "".to_string();
let mut subargs = vec![];
let mut parser = argparse::ArgumentParser::new();
let _ = parser.refer(&mut version).add_option(
Expand Down

0 comments on commit d7f1557

Please sign in to comment.