aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-12-25 10:39:06 +0100
committerDaniel Mueller <deso@posteo.net>2018-12-27 11:12:59 -0800
commitf3c13434e91eea3bcceaa95b86bfcd0db1ec56d4 (patch)
tree393e4193e28e1bb5b9d5e9ca35d205f73c5dc3b9
parent57e8db75c69cc3831b9c5843c95fcd02afe1ee9b (diff)
downloadnitrocli-f3c13434e91eea3bcceaa95b86bfcd0db1ec56d4.tar.gz
nitrocli-f3c13434e91eea3bcceaa95b86bfcd0db1ec56d4.tar.bz2
Implement the config get subcommand
This change implements the config get subcommand. The subcommand reads the device configuration and prints it.
-rw-r--r--nitrocli/src/args.rs13
-rw-r--r--nitrocli/src/commands.rs28
2 files changed, 39 insertions, 2 deletions
diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs
index 253d5a0..298b157 100644
--- a/nitrocli/src/args.rs
+++ b/nitrocli/src/args.rs
@@ -91,9 +91,9 @@ enum ConfigCommand {
}
impl ConfigCommand {
- fn execute(&self, _args: Vec<String>) -> Result<()> {
+ fn execute(&self, args: Vec<String>) -> Result<()> {
match *self {
- ConfigCommand::Get => Err(Error::Error("Not implemented".to_string())),
+ ConfigCommand::Get => config_get(args),
}
}
}
@@ -308,6 +308,15 @@ fn config(args: Vec<String>) -> Result<()> {
subcommand.execute(subargs)
}
+/// Read the Nitrokey configuration.
+fn config_get(args: Vec<String>) -> Result<()> {
+ let mut parser = argparse::ArgumentParser::new();
+ parser.set_description("Prints the Nitrokey configuration");
+ parse(&parser, args)?;
+
+ commands::config_get()
+}
+
/// Execute an OTP subcommand.
fn otp(args: Vec<String>) -> Result<()> {
let mut subcommand = OtpCommand::Get;
diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs
index 528fa99..8ce1076 100644
--- a/nitrocli/src/commands.rs
+++ b/nitrocli/src/commands.rs
@@ -17,6 +17,7 @@
// * along with this program. If not, see <http://www.gnu.org/licenses/>. *
// *************************************************************************
+use std::fmt;
use std::result;
use nitrokey::ConfigureOtp;
@@ -276,6 +277,33 @@ pub fn clear() -> Result<()> {
Ok(())
}
+/// Return a String representation of the given Option.
+fn format_option<T: fmt::Display>(option: Option<T>) -> String {
+ match option {
+ Some(value) => format!("{}", value),
+ None => "not set".to_string(),
+ }
+}
+
+/// Read the Nitrokey configuration.
+pub fn config_get() -> Result<()> {
+ let config = get_device()?
+ .get_config()
+ .map_err(|err| get_error("Could not get configuration", &err))?;
+ println!(
+ r#"Config:
+ numlock binding: {nl}
+ capslock binding: {cl}
+ scrollock binding: {sl}
+ require user PIN for OTP: {otp}"#,
+ nl = format_option(config.numlock),
+ cl = format_option(config.capslock),
+ sl = format_option(config.scrollock),
+ otp = config.user_password,
+ );
+ Ok(())
+}
+
fn get_otp<T: GenerateOtp>(slot: u8, algorithm: args::OtpAlgorithm, device: &T) -> Result<String> {
match algorithm {
args::OtpAlgorithm::Hotp => device.get_hotp_code(slot),