From 1d04723bd7221b7daf5a1f28b584838133a8e5db Mon Sep 17 00:00:00 2001 From: aaron-jack-manning Date: Tue, 23 Aug 2022 21:05:28 +1000 Subject: [PATCH] rename vaults option --- src/config.rs | 26 ++++++++++++++++++++++++++ src/main.rs | 9 +++++++++ 2 files changed, 35 insertions(+) diff --git a/src/config.rs b/src/config.rs index 2085cc0..fca2dd8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -41,6 +41,32 @@ impl Config { self.vaults.iter().any(|(_, p)| p == path) } + pub fn rename_vault(&mut self, old_name : &String, new_name : String) -> Result<(), error::Error> { + let mut to_change = None; + + for (name, _) in &mut self.vaults { + if *name == new_name { + return Err(error::Error::Generic(format!("A vault named {} already exists", colour::vault(&new_name)))); + } + + if name == old_name { + to_change = Some(name); + } + } + + match to_change { + Some(name) => { + *name = new_name; + Ok(()) + }, + None => { + Err(error::Error::Generic(format!("No vault named {} exists", colour::vault(&old_name)))) + } + } + + + } + /// Adds the vault to the configuration. pub fn add(&mut self, name : String, path : path::PathBuf) { debug_assert!(!self.contains_name(&name)); diff --git a/src/main.rs b/src/main.rs index d2c506e..78aa0a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,6 +125,11 @@ enum VaultCommand { }, /// Lists all configured vaults. List, + /// For renaming an already set up vault. + Rename { + old_name : String, + new_name : String, + } } fn main() { @@ -170,6 +175,10 @@ fn program() -> Result<(), error::Error> { List => { config.list_vaults()?; }, + Rename { old_name, new_name } => { + config.rename_vault(&old_name, new_name.clone())?; + println!("Renamed vault {} to {}", colour::vault(&old_name), colour::vault(&new_name)); + } } } else if let Config(command) = command {