Skip to content

Commit

Permalink
Merge config_set function into commands.rs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d-e-s-o committed Apr 11, 2020
1 parent eae8b2a commit 93c6054
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/arg_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
15 changes: 0 additions & 15 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::io;
use std::result;

use crate::arg_defs;
use crate::commands;
use crate::error::Error;
use crate::RunCtx;

Expand Down Expand Up @@ -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<String>) -> Result<()> {
use structopt::StructOpt;
Expand Down
21 changes: 13 additions & 8 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
capslock: arg_defs::ConfigOption<u8>,
scrollock: arg_defs::ConfigOption<u8>,
user_password: Option<bool>,
) -> 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
Expand All @@ -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)
Expand Down

0 comments on commit 93c6054

Please sign in to comment.