Skip to content

Commit

Permalink
Add --raw option to search command
Browse files Browse the repository at this point in the history
  • Loading branch information
sweharris committed Jan 18, 2025
1 parent 90e895f commit 66366ed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
40 changes: 26 additions & 14 deletions src/bin/rbw/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,11 +1093,13 @@ pub fn get(
Ok(())
}

pub fn search(term: &str, folder: Option<&str>) -> anyhow::Result<()> {
pub fn search(term: &str, folder: Option<&str>, raw: bool) -> anyhow::Result<()> {
unlock()?;

let db = load_db()?;

let mut json_entries = vec![];

let found_entries: Vec<_> = db
.entries
.iter()
Expand All @@ -1106,18 +1108,23 @@ pub fn search(term: &str, folder: Option<&str>) -> anyhow::Result<()> {
entry
.map(|decrypted| {
if decrypted.search_match(term, folder) {
let mut display = decrypted.name;
if let DecryptedData::Login {
username: Some(username),
..
} = decrypted.data
{
display = format!("{username}@{display}");
}
if let Some(folder) = decrypted.folder {
display = format!("{folder}/{display}");
if raw {
json_entries.push(decrypted);
None
} else {
let mut display = decrypted.name;
if let DecryptedData::Login {
username: Some(username),
..
} = decrypted.data
{
display = format!("{username}@{display}");
}
if let Some(folder) = decrypted.folder {
display = format!("{folder}/{display}");
}
Some(display)
}
Some(display)
} else {
None
}
Expand All @@ -1126,8 +1133,13 @@ pub fn search(term: &str, folder: Option<&str>) -> anyhow::Result<()> {
})
.collect::<Result<_, anyhow::Error>>()?;

for name in found_entries {
println!("{name}");
if raw {
let j = serde_json::to_string(&json_entries)?;
println!("{}",j);
} else {
for name in found_entries {
println!("{name}");
}
}

Ok(())
Expand Down
6 changes: 4 additions & 2 deletions src/bin/rbw/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ enum Opt {
term: String,
#[arg(long, help = "Folder name to search in")]
folder: Option<String>,
#[structopt(long, help = "Display output as JSON")]
raw: bool,
},

#[command(
Expand Down Expand Up @@ -352,8 +354,8 @@ fn main() {
false,
*ignorecase,
),
Opt::Search { term, folder } => {
commands::search(term, folder.as_deref())
Opt::Search { term, folder, raw } => {
commands::search(term, folder.as_deref(), *raw)
}
Opt::Code {
needle,
Expand Down

0 comments on commit 66366ed

Please sign in to comment.