From fd3914dacbb320cecf7f5ea27de25f5c5bc38919 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Tue, 3 Jan 2023 11:58:48 +0100 Subject: [PATCH] Renamed the dump command to save, added command dump to dump the db to stdout. --- hel/src/commands.rs | 12 +++++++++--- hel/src/parser.rs | 7 ++++--- hel/src/structs.rs | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/hel/src/commands.rs b/hel/src/commands.rs index 86e6243..bd79e93 100644 --- a/hel/src/commands.rs +++ b/hel/src/commands.rs @@ -303,14 +303,20 @@ impl<'a> LKEval<'a> { } }; if output.len() > 0 { - out.e(format!("Passwords dumped to command {} and got following output:", cmd)); + out.e(format!("Passwords saved to command {} and got following output:", cmd)); out.o(output); } else { - out.o(format!("Passwords dumped to command {}", cmd)); + out.o(format!("Passwords saved to command {}", cmd)); } + } else if script.trim() == "-" { + let mut vals = (&self.state.borrow().db).values().map(|v| v.clone()).collect::>(); + vals.sort_by(|a, b| a.borrow().name.cmp(&b.borrow().name)); + for pwd in vals { + out.o(format!("add {}", pwd.borrow().to_string())) + }; } else { match save_dump(&self.state.borrow().db, &script) { - Ok(()) => out.o(format!("Passwords dumped to {}", script)), + Ok(()) => out.o(format!("Passwords saved to file {}", script)), Err(e) => out.e(format!("error: failed to dump passswords to {}: {}", script, e.to_string())), }; } diff --git a/hel/src/parser.rs b/hel/src/parser.rs index 5595b01..b197516 100644 --- a/hel/src/parser.rs +++ b/hel/src/parser.rs @@ -8,7 +8,7 @@ use std::{cell::RefCell, rc::Rc}; peg::parser! { pub grammar command_parser() for str { pub rule cmd() -> Command<'input> = c:(info_cmd_list() / mod_cmd_list() / enc_cmd_list() / asides_cmd_list()) { c } - pub rule info_cmd_list() -> Command<'input> = space()* c:(ls_cmd() / ld_cmd() / pb_cmd() / dump_cmd() / dump_def_cmd()) { c } + pub rule info_cmd_list() -> Command<'input> = space()* c:(ls_cmd() / ld_cmd() / pb_cmd() / save_cmd() / save_def_cmd() / dump_cmd()) { c } pub rule mod_cmd_list() -> Command<'input> = space()* c:(add_cmd() / leave_cmd() / mv_cmd() / rm_cmd() / comment_cmd ()) { c } pub rule asides_cmd_list() -> Command<'input> = space()* c:(help_cmd() / source_cmd() / quit_cmd() / noop_cmd() / error_cmd()) { c } pub rule enc_cmd_list() -> Command<'input> = space()* c:(enc_cmd() / gen_cmd() / pass_cmd() / unpass_cmd() / correct_cmd() / uncorrect_cmd()) { c } @@ -83,8 +83,9 @@ peg::parser! { rule help_cmd() -> Command<'input> = "help" { Command::Help } rule quit_cmd() -> Command<'input> = "quit" { Command::Quit } rule pb_cmd() -> Command<'input> = "pb" _ e:$(([' '..='~'])+) { Command::PasteBuffer(e.to_string()) } - rule dump_cmd() -> Command<'input> = "dump" _ s:$(([' '..='~'])+) { Command::Dump(Some(s.to_string())) } - rule dump_def_cmd() -> Command<'input> = "dump" { Command::Dump(None) } + rule save_cmd() -> Command<'input> = "save" _ s:$(([' '..='~'])+) { Command::Dump(Some(s.to_string())) } + rule save_def_cmd() -> Command<'input> = "save" { Command::Dump(None) } + rule dump_cmd() -> Command<'input> = "dump" { Command::Dump(Some("-".to_string())) } rule source_cmd() -> Command<'input> = "source" _ s:$(([' '..='~'])+) { Command::Source(s.to_string()) } rule ls_cmd() -> Command<'input> = "ls" f:comment()? { Command::Ls(f.unwrap_or(".".to_string())) } rule ld_cmd() -> Command<'input> = "ld" f:comment()? { Command::Ld(f.unwrap_or(".".to_string())) } diff --git a/hel/src/structs.rs b/hel/src/structs.rs index 53ac732..b6cbfc0 100644 --- a/hel/src/structs.rs +++ b/hel/src/structs.rs @@ -384,7 +384,7 @@ mod tests { assert_eq!(*lkread.state.borrow().db.get("t2").unwrap().borrow(), *t2.borrow()); assert_eq!(*lkread.state.borrow().db.get("t3").unwrap().borrow(), *t3.borrow()); - LKEval::new(command_parser::cmd("dump").unwrap(), lkread.state.clone(), password) + LKEval::new(command_parser::cmd("save").unwrap(), lkread.state.clone(), password) .eval() .print(); assert_eq!(