From 93c605418568bf71bcc3be6956f07f9650b45fea Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sat, 11 Apr 2020 12:29:22 -0700 Subject: Merge config_set function into commands.rs As a next step moving us closer towards removing the args module, this change merges the config_set function into the existing function of the same name in the commands module. --- src/arg_defs.rs | 2 +- src/args.rs | 15 --------------- src/commands.rs | 21 +++++++++++++-------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/arg_defs.rs b/src/arg_defs.rs index fd296e4..133ef19 100644 --- a/src/arg_defs.rs +++ b/src/arg_defs.rs @@ -93,7 +93,7 @@ Command! {ConfigCommand, [ /// Prints the Nitrokey configuration Get => crate::commands::config_get, /// Changes the Nitrokey configuration - Set(ConfigSetArgs) => crate::args::config_set, + Set(ConfigSetArgs) => crate::commands::config_set, ]} #[derive(Debug, PartialEq, structopt::StructOpt)] diff --git a/src/args.rs b/src/args.rs index 3a1f2c8..d0b2a88 100644 --- a/src/args.rs +++ b/src/args.rs @@ -22,7 +22,6 @@ use std::io; use std::result; use crate::arg_defs; -use crate::commands; use crate::error::Error; use crate::RunCtx; @@ -68,20 +67,6 @@ impl<'io> Stdio for ExecCtx<'io> { } } -pub fn config_set(ctx: &mut ExecCtx<'_>, args: arg_defs::ConfigSetArgs) -> Result<()> { - let numlock = arg_defs::ConfigOption::try_from(args.no_numlock, args.numlock, "numlock")?; - let capslock = arg_defs::ConfigOption::try_from(args.no_capslock, args.capslock, "capslock")?; - let scrollock = arg_defs::ConfigOption::try_from(args.no_scrollock, args.scrollock, "scrollock")?; - let otp_pin = if args.otp_pin { - Some(true) - } else if args.no_otp_pin { - Some(false) - } else { - None - }; - commands::config_set(ctx, numlock, capslock, scrollock, otp_pin) -} - /// Parse the command-line arguments and execute the selected command. pub(crate) fn handle_arguments(ctx: &mut RunCtx<'_>, args: Vec) -> Result<()> { use structopt::StructOpt; diff --git a/src/commands.rs b/src/commands.rs index 55e6707..b40d552 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -579,13 +579,18 @@ pub fn config_get(ctx: &mut args::ExecCtx<'_>) -> Result<()> { } /// Write the Nitrokey configuration. -pub fn config_set( - ctx: &mut args::ExecCtx<'_>, - numlock: arg_defs::ConfigOption, - capslock: arg_defs::ConfigOption, - scrollock: arg_defs::ConfigOption, - user_password: Option, -) -> Result<()> { +pub fn config_set(ctx: &mut args::ExecCtx<'_>, args: arg_defs::ConfigSetArgs) -> Result<()> { + let numlock = arg_defs::ConfigOption::try_from(args.no_numlock, args.numlock, "numlock")?; + let capslock = arg_defs::ConfigOption::try_from(args.no_capslock, args.capslock, "capslock")?; + let scrollock = arg_defs::ConfigOption::try_from(args.no_scrollock, args.scrollock, "scrollock")?; + let otp_pin = if args.otp_pin { + Some(true) + } else if args.no_otp_pin { + Some(false) + } else { + None + }; + with_device(ctx, |ctx, device| { let mut device = authenticate_admin(ctx, device)?; let config = device @@ -595,7 +600,7 @@ pub fn config_set( numlock: numlock.or(config.numlock), capslock: capslock.or(config.capslock), scrollock: scrollock.or(config.scrollock), - user_password: user_password.unwrap_or(config.user_password), + user_password: otp_pin.unwrap_or(config.user_password), }; device .write_config(config) -- cgit v1.2.3