From bc543e4f26d3e29fe88a61457a924ab6ab238535 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 19 Apr 2020 21:23:08 -0400 Subject: allow listing various different fields --- src/bin/rbw/commands.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src/bin/rbw/commands.rs') diff --git a/src/bin/rbw/commands.rs b/src/bin/rbw/commands.rs index 2385163..ff267eb 100644 --- a/src/bin/rbw/commands.rs +++ b/src/bin/rbw/commands.rs @@ -18,6 +18,25 @@ struct DecryptedHistoryEntry { password: String, } +enum ListField { + Name, + Id, + User, +} + +impl std::convert::TryFrom<&str> for ListField { + type Error = anyhow::Error; + + fn try_from(s: &str) -> anyhow::Result { + Ok(match s { + "name" => Self::Name, + "id" => Self::Id, + "user" => Self::User, + _ => return Err(anyhow::anyhow!("unknown field {}", s)), + }) + } +} + const HELP: &str = r#" # The first line of this file will be the password, and the remainder of the # file (after any blank lines after the password) will be stored as a note. @@ -76,7 +95,13 @@ pub fn sync() -> anyhow::Result<()> { Ok(()) } -pub fn list() -> anyhow::Result<()> { +pub fn list(fields: &[&str]) -> anyhow::Result<()> { + let fields: Vec = fields + .iter() + .copied() + .map(std::convert::TryFrom::try_from) + .collect::>()?; + unlock()?; let email = config_email()?; @@ -92,7 +117,19 @@ pub fn list() -> anyhow::Result<()> { ciphers.sort_unstable_by(|a, b| a.name.cmp(&b.name)); for cipher in ciphers { - println!("{}", cipher.name); + let values: Vec = fields + .iter() + .map(|field| match field { + ListField::Name => cipher.name.clone(), + ListField::Id => cipher.id.clone(), + ListField::User => cipher + .username + .as_ref() + .map(std::string::ToString::to_string) + .unwrap_or_else(|| "".to_string()), + }) + .collect(); + println!("{}", values.join("\t")); } Ok(()) -- cgit v1.2.3-54-g00ecf