From fafbf4a1bf635d5eea049cf561fbf8aa293c8e2b Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sat, 11 Apr 2020 13:23:52 -0700 Subject: Merge remaining bits of args.rs into main.rs This change removes the args module by moving all remaining functionality in it into main.rs. The result is arguably a nice consolidation of all context related definitions in a single module. --- src/main.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 89c9d67..a2c4f48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,6 @@ mod redefine; mod arg_util; mod arg_defs; -mod args; mod commands; mod error; mod pinentry; @@ -93,6 +92,88 @@ const NITROCLI_NEW_USER_PIN: &str = "NITROCLI_NEW_USER_PIN"; const NITROCLI_PASSWORD: &str = "NITROCLI_PASSWORD"; const NITROCLI_NO_CACHE: &str = "NITROCLI_NO_CACHE"; +trait Stdio { + fn stdio(&mut self) -> (&mut dyn io::Write, &mut dyn io::Write); +} + +impl<'io> Stdio for RunCtx<'io> { + fn stdio(&mut self) -> (&mut dyn io::Write, &mut dyn io::Write) { + (self.stdout, self.stderr) + } +} + +impl Stdio for (&mut W, &mut W) +where + W: io::Write, +{ + fn stdio(&mut self) -> (&mut dyn io::Write, &mut dyn io::Write) { + (self.0, self.1) + } +} + +/// A command execution context that captures additional data pertaining +/// the command execution. +#[allow(missing_debug_implementations)] +pub struct ExecCtx<'io> { + /// The Nitrokey model to use. + pub model: Option, + /// See `RunCtx::stdout`. + pub stdout: &'io mut dyn io::Write, + /// See `RunCtx::stderr`. + pub stderr: &'io mut dyn io::Write, + /// See `RunCtx::admin_pin`. + pub admin_pin: Option, + /// See `RunCtx::user_pin`. + pub user_pin: Option, + /// See `RunCtx::new_admin_pin`. + pub new_admin_pin: Option, + /// See `RunCtx::new_user_pin`. + pub new_user_pin: Option, + /// See `RunCtx::password`. + pub password: Option, + /// See `RunCtx::no_cache`. + pub no_cache: bool, + /// The verbosity level to use for logging. + pub verbosity: u64, +} + +impl<'io> Stdio for ExecCtx<'io> { + fn stdio(&mut self) -> (&mut dyn io::Write, &mut dyn io::Write) { + (self.stdout, self.stderr) + } +} + +/// Parse the command-line arguments and execute the selected command. +fn handle_arguments(ctx: &mut RunCtx<'_>, args: Vec) -> Result<()> { + use structopt::StructOpt; + + match arg_defs::Args::from_iter_safe(args.iter()) { + Ok(args) => { + let mut ctx = ExecCtx { + model: args.model, + stdout: ctx.stdout, + stderr: ctx.stderr, + admin_pin: ctx.admin_pin.take(), + user_pin: ctx.user_pin.take(), + new_admin_pin: ctx.new_admin_pin.take(), + new_user_pin: ctx.new_user_pin.take(), + password: ctx.password.take(), + no_cache: ctx.no_cache, + verbosity: args.verbose.into(), + }; + args.cmd.execute(&mut ctx) + } + Err(err) => { + if err.use_stderr() { + Err(err.into()) + } else { + println!(ctx, "{}", err.message)?; + Ok(()) + } + } + } +} + /// The context used when running the program. pub(crate) struct RunCtx<'io> { /// The `Write` object used as standard output throughout the program. @@ -118,7 +199,7 @@ pub(crate) struct RunCtx<'io> { } fn run<'ctx, 'io: 'ctx>(ctx: &'ctx mut RunCtx<'io>, args: Vec) -> i32 { - match args::handle_arguments(ctx, args) { + match handle_arguments(ctx, args) { Ok(()) => 0, Err(err) => { let _ = eprintln!(ctx, "{}", err); -- cgit v1.2.1