rename vaults option

This commit is contained in:
aaron-jack-manning 2022-08-23 21:05:28 +10:00
parent 0aea904fdd
commit 1d04723bd7
2 changed files with 35 additions and 0 deletions

View File

@ -41,6 +41,32 @@ impl Config {
self.vaults.iter().any(|(_, p)| p == path) 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. /// Adds the vault to the configuration.
pub fn add(&mut self, name : String, path : path::PathBuf) { pub fn add(&mut self, name : String, path : path::PathBuf) {
debug_assert!(!self.contains_name(&name)); debug_assert!(!self.contains_name(&name));

View File

@ -125,6 +125,11 @@ enum VaultCommand {
}, },
/// Lists all configured vaults. /// Lists all configured vaults.
List, List,
/// For renaming an already set up vault.
Rename {
old_name : String,
new_name : String,
}
} }
fn main() { fn main() {
@ -170,6 +175,10 @@ fn program() -> Result<(), error::Error> {
List => { List => {
config.list_vaults()?; 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 { else if let Config(command) = command {