From 0d6fcbbd110232a71791deb46711ef97370514a7 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sat, 28 Sep 2024 10:03:24 +0000 Subject: [PATCH] change passeord --- src/main.rs | 45 ++++++++++++++++++++++++++++++--------------- src/storage.rs | 7 +++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1f8f1e4..aba0233 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,10 @@ enum Commands { Edit(ItemIdArgs), /// Delete item - Delete(ItemIdArgs) + Delete(ItemIdArgs), + + /// Set new passphrase + Password } @@ -121,10 +124,7 @@ impl MPS { Ok(ps) } - fn init() -> io::Result<()> { - if Storage::is_inited()? { - return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); - } + fn read_new_password() -> io::Result { let passphrase; loop { match MPS::prompt_new_password() { @@ -138,8 +138,15 @@ impl MPS { } } } - Storage::init(passphrase)?; - Ok(()) + Ok(passphrase) + } + + fn init() -> io::Result<()> { + if Storage::is_inited()? { + return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); + } + let passphrase = MPS::read_new_password()?; + Storage::init(passphrase) } pub fn login(&mut self) -> io::Result<()> { @@ -156,6 +163,14 @@ impl MPS { Ok(()) } + pub fn password(&mut self) -> io::Result<()> { + self.login()?; + let st = self.storage.as_ref().unwrap(); + let passphrase = MPS::read_new_password()?; + let new_st = st.new_from_passphrase(&passphrase); + new_st.dump() + } + fn list(&mut self) -> io:: Result<()> { self.login()?; let ids = self.storage.as_ref().unwrap().ids(); @@ -176,8 +191,7 @@ impl MPS { self.login()?; let st = self.storage.as_ref().unwrap(); let item = st.get(id); - editor::open_to_show(&item.content)?; - Ok(()) + editor::open_to_show(&item.content) } fn add(&mut self, id: &String) -> io::Result<()> { @@ -196,8 +210,7 @@ impl MPS { // set empty string because there is no content yet let content = editor::open_to_edit(&String::from(""))?; st.add(Item::from(id.clone(), content)); - st.dump()?; - Ok(()) + st.dump() } /// Edit item, id need not to exist @@ -218,8 +231,7 @@ impl MPS { let new_content = editor::open_to_edit(&item.content)?; item.content = new_content; st.update(item); - st.dump()?; - Ok(()) + st.dump() } // Delete item by id, is must exist @@ -227,8 +239,7 @@ impl MPS { self.login()?; let st = self.storage.as_mut().unwrap(); st.remove(id); - st.dump()?; - Ok(()) + st.dump() } /// Resolve id by ItemArgs. @@ -292,6 +303,10 @@ fn run_command() -> io::Result<()> { let id = mps.item_id_by_item_id_args(args, true)?; mps.delete(&id)?; } + Some(Commands::Password) => { + let mut mps = MPS::new(); + mps.password()?; + } None => { match Storage::check_installed() { Ok(()) => (), diff --git a/src/storage.rs b/src/storage.rs index 984a6a2..5abacae 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -242,5 +242,12 @@ impl Storage { Ok(()) } + pub fn new_from_passphrase(&self, passphrase: &String) -> Storage { + return Storage { + encoder: Encoder::from(passphrase), + items: self.items.clone() + } + } + }