Allow to enter regular expression as argument to ls command, to get only matching passwords.

This commit is contained in:
Oleksandr Kozachuk
2022-12-14 15:12:36 +01:00
parent 4207279364
commit 2a05254415
3 changed files with 16 additions and 9 deletions
+1 -1
View File
@@ -72,7 +72,7 @@ peg::parser! {
rule mode() -> Mode = m:(umode() / rmode()) { m } rule mode() -> Mode = m:(umode() / rmode()) { m }
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 ls_cmd() -> Command<'input> = "ls" { Command::Ls } rule ls_cmd() -> Command<'input> = "ls" f:comment()? { Command::Ls(f.unwrap_or(".".to_string())) }
rule add_cmd() -> Command<'input> = "add" _ name:name() { Command::Add(Rc::new(RefCell::new(name))) } rule add_cmd() -> Command<'input> = "add" _ name:name() { Command::Add(Rc::new(RefCell::new(name))) }
rule error_cmd() -> Command<'input> = "error" _ e:$(([' '..='~'])+) { Command::Error(LKErr::Error(e)) } rule error_cmd() -> Command<'input> = "error" _ e:$(([' '..='~'])+) { Command::Error(LKErr::Error(e)) }
rule mv_cmd() -> Command<'input> = "mv" _ name:word() _ folder:word() { Command::Mv(name, folder) } rule mv_cmd() -> Command<'input> = "mv" _ name:word() _ folder:word() { Command::Mv(name, folder) }
+13 -6
View File
@@ -1,6 +1,7 @@
use home::home_dir; use home::home_dir;
use rpassword::prompt_password; use rpassword::prompt_password;
use rustyline::Editor; use rustyline::Editor;
use regex::Regex;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use crate::lk::LK; use crate::lk::LK;
@@ -173,11 +174,17 @@ impl<'a> LKEval<'a> {
}; };
} }
fn cmd_ls(&self, out: &mut Vec<String>) { fn cmd_ls(&self, out: &mut Vec<String>, filter: String) {
let re = match Regex::new(&filter) {
Ok(re) => re,
Err(e) => { out.push(format!("error: failed to parse re: {:?}", e)); return; }
};
let mut tmp: Vec<PasswordRef> = vec![]; let mut tmp: Vec<PasswordRef> = vec![];
for (_, name) in &self.state.borrow().db { for (_, name) in &self.state.borrow().db {
if re.find(&name.borrow().to_string()).is_some() {
tmp.push(name.clone()); tmp.push(name.clone());
} }
}
tmp.sort_by(|a, b| a.borrow().name.cmp(&b.borrow().name)); tmp.sort_by(|a, b| a.borrow().name.cmp(&b.borrow().name));
self.state.borrow_mut().ls.clear(); self.state.borrow_mut().ls.clear();
let mut counter = 1; let mut counter = 1;
@@ -198,7 +205,7 @@ impl<'a> LKEval<'a> {
out.push("Bye!".to_string()); out.push("Bye!".to_string());
quit = true; quit = true;
} }
Command::Ls => self.cmd_ls(&mut out), Command::Ls(filter) => self.cmd_ls(&mut out, filter.to_string()),
Command::Add(name) => { Command::Add(name) => {
if self.state.borrow().db.get(&name.borrow().name).is_some() { if self.state.borrow().db.get(&name.borrow().name).is_some() {
out.push("error: password already exist".to_string()); out.push("error: password already exist".to_string());
@@ -290,7 +297,7 @@ mod tests {
#[test] #[test]
fn exec_cmds_basic() { fn exec_cmds_basic() {
let lk = Rc::new(RefCell::new(LK::new())); let lk = Rc::new(RefCell::new(LK::new()));
assert_eq!(LKEval::news(Command::Ls, lk.clone()).eval(), LKPrint::new(vec![], false, lk.clone())); assert_eq!(LKEval::news(Command::Ls(".".to_string()), lk.clone()).eval(), LKPrint::new(vec![], false, lk.clone()));
let pwd1 = Rc::new(RefCell::new(Password { let pwd1 = Rc::new(RefCell::new(Password {
name: Rc::new("t1".to_string()), name: Rc::new("t1".to_string()),
prefix: None, prefix: None,
@@ -306,7 +313,7 @@ mod tests {
db.insert(pwd1.borrow().name.clone(), pwd1.clone()); db.insert(pwd1.borrow().name.clone(), pwd1.clone());
db db
}); });
assert_eq!(LKEval::news(Command::Ls, lk.clone()).eval(), LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string()], false, lk.clone())); assert_eq!(LKEval::news(Command::Ls(".".to_string()), lk.clone()).eval(), LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string()], false, lk.clone()));
assert_eq!(LKEval::news(Command::Quit, lk.clone()).eval(), LKPrint::new(vec!["Bye!".to_string()], true, lk.clone())); assert_eq!(LKEval::news(Command::Quit, lk.clone()).eval(), LKPrint::new(vec!["Bye!".to_string()], true, lk.clone()));
let pwd2 = Rc::new(RefCell::new(Password { let pwd2 = Rc::new(RefCell::new(Password {
name: Rc::new("t2".to_string()), name: Rc::new("t2".to_string()),
@@ -325,11 +332,11 @@ mod tests {
db db
}); });
assert_eq!( assert_eq!(
LKEval::news(Command::Ls, lk.clone()).eval(), LKEval::news(Command::Ls(".".to_string()), lk.clone()).eval(),
LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string(), " 2 t2 R 99 2022-12-31 bli blup".to_string()], false, lk.clone()) LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string(), " 2 t2 R 99 2022-12-31 bli blup".to_string()], false, lk.clone())
); );
assert_eq!(LKEval::news(Command::Rm("2".to_string()), lk.clone()).eval(), LKPrint::new(vec!["removed t2".to_string()], false, lk.clone())); assert_eq!(LKEval::news(Command::Rm("2".to_string()), lk.clone()).eval(), LKPrint::new(vec!["removed t2".to_string()], false, lk.clone()));
assert_eq!(LKEval::news(Command::Ls, lk.clone()).eval(), LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string()], false, lk.clone())); assert_eq!(LKEval::news(Command::Ls(".".to_string()), lk.clone()).eval(), LKPrint::new(vec![" 1 t1 R 99 2022-12-30 comment".to_string()], false, lk.clone()));
} }
#[test] #[test]
+1 -1
View File
@@ -14,7 +14,7 @@ pub enum LKErr<'a> {
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub enum Command<'a> { pub enum Command<'a> {
Add(PasswordRef), Add(PasswordRef),
Ls, Ls(String),
Mv(Name, Name), Mv(Name, Name),
Rm(Name), Rm(Name),
Enc(Name), Enc(Name),