From 2feed7e2e9367c935aeb669daab66a63cff2f6c0 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 3 May 2020 16:01:26 -0400 Subject: add command to clear a config setting --- CHANGELOG.md | 1 + src/bin/rbw/commands.rs | 17 +++++++++++++++++ src/bin/rbw/main.rs | 14 ++++++++++++++ src/config.rs | 2 +- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d560034..7b76d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Multi-server support - you can now switch between multiple different bitwarden servers with `rbw config set base_url` without needing to redownload the password database each time. +* `rbw config unset` to reset configuration items back to the default ### Fixed diff --git a/src/bin/rbw/commands.rs b/src/bin/rbw/commands.rs index a6e98c7..a2e9f9d 100644 --- a/src/bin/rbw/commands.rs +++ b/src/bin/rbw/commands.rs @@ -75,6 +75,23 @@ pub fn config_set(key: &str, value: &str) -> anyhow::Result<()> { Ok(()) } +pub fn config_unset(key: &str) -> anyhow::Result<()> { + let mut config = rbw::config::Config::load() + .unwrap_or_else(|_| rbw::config::Config::new()); + match key { + "email" => config.email = None, + "base_url" => config.base_url = None, + "identity_url" => config.identity_url = None, + "lock_timeout" => { + config.lock_timeout = rbw::config::default_lock_timeout() + } + _ => return Err(anyhow::anyhow!("invalid config key: {}", key)), + } + config.save().context("failed to save config file")?; + + Ok(()) +} + pub fn login() -> anyhow::Result<()> { ensure_agent()?; crate::actions::login()?; diff --git a/src/bin/rbw/main.rs b/src/bin/rbw/main.rs index f763fe9..e2f2e85 100644 --- a/src/bin/rbw/main.rs +++ b/src/bin/rbw/main.rs @@ -37,6 +37,15 @@ fn main() { "Value to set the configuration option to", ), ), + ) + .subcommand( + clap::SubCommand::with_name("unset") + .about("Reset a configuration option to its default") + .arg( + clap::Arg::with_name("key") + .required(true) + .help("Configuration key to unset"), + ), ), ) .subcommand( @@ -271,6 +280,11 @@ fn main() { ssmatches.value_of("value").unwrap(), ) .context("config set"), + // this unwrap is fine because key is marked .required(true) + ("unset", Some(ssmatches)) => { + commands::config_unset(ssmatches.value_of("key").unwrap()) + .context("config unset") + } _ => { eprintln!("{}", smatches.usage()); std::process::exit(1); diff --git a/src/config.rs b/src/config.rs index 7bbb928..caa27a6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,7 +12,7 @@ pub struct Config { pub lock_timeout: u64, } -fn default_lock_timeout() -> u64 { +pub fn default_lock_timeout() -> u64 { 3600 } -- cgit v1.2.3-54-g00ecf