Renamed the dump command to save, added command dump to dump the db to stdout.

This commit is contained in:
Oleksandr Kozachuk
2023-01-03 11:58:48 +01:00
parent 1b96ca3239
commit fd3914dacb
3 changed files with 14 additions and 7 deletions
+9 -3
View File
@@ -303,14 +303,20 @@ impl<'a> LKEval<'a> {
} }
}; };
if output.len() > 0 { 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); out.o(output);
} else { } 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::<Vec<PasswordRef>>();
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 { } else {
match save_dump(&self.state.borrow().db, &script) { 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())), Err(e) => out.e(format!("error: failed to dump passswords to {}: {}", script, e.to_string())),
}; };
} }
+4 -3
View File
@@ -8,7 +8,7 @@ use std::{cell::RefCell, rc::Rc};
peg::parser! { peg::parser! {
pub grammar command_parser() for str { 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 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 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 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 } 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 help_cmd() -> Command<'input> = "help" { Command::Help }
rule quit_cmd() -> Command<'input> = "quit" { Command::Quit } rule quit_cmd() -> Command<'input> = "quit" { Command::Quit }
rule pb_cmd() -> Command<'input> = "pb" _ e:$(([' '..='~'])+) { Command::PasteBuffer(e.to_string()) } 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 save_cmd() -> Command<'input> = "save" _ s:$(([' '..='~'])+) { Command::Dump(Some(s.to_string())) }
rule dump_def_cmd() -> Command<'input> = "dump" { Command::Dump(None) } 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 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 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())) } rule ld_cmd() -> Command<'input> = "ld" f:comment()? { Command::Ld(f.unwrap_or(".".to_string())) }
+1 -1
View File
@@ -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("t2").unwrap().borrow(), *t2.borrow());
assert_eq!(*lkread.state.borrow().db.get("t3").unwrap().borrow(), *t3.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() .eval()
.print(); .print();
assert_eq!( assert_eq!(