From c8d3b54a6f6dcf00910971c0bbadaa95076d1668 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Tue, 1 Aug 2023 21:49:26 +0200 Subject: [PATCH] feat!: add listing items from ssh key folders BREAKING CHANGE: `exec_interactive_command` is now `exec_piped_command` --- src/bwutil.rs | 23 +++++++++++++++++------ src/main.rs | 10 ++++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/bwutil.rs b/src/bwutil.rs index 36597fb..0e007e5 100644 --- a/src/bwutil.rs +++ b/src/bwutil.rs @@ -62,7 +62,7 @@ pub fn get_session_token() -> Result { operation.push_str("login") } - let success: Output = exec_interactive_command("bw", + let success: Output = exec_piped_command("bw", ["--raw", &operation].to_vec()); if success.status.success() { token.push_str(&String::from_utf8(success.stdout)?); @@ -73,7 +73,7 @@ pub fn get_session_token() -> Result { /// Search Bitwarden for folders matching `folder_name` and return the results. pub fn exec_folder_search(session: &str, folder_name: &str) -> Result> { - let folders: Output = exec_interactive_command("bw", + let folders: Output = exec_piped_command("bw", ["list", "folders", "--search", &folder_name, "--session", &session].to_vec()); let result: String = String::from_utf8_lossy(&folders.stdout).to_string(); if result.is_empty() { @@ -84,19 +84,30 @@ pub fn exec_folder_search(session: &str, folder_name: &str) -> Result Result> { + let items = exec_piped_command("bw", + ["list", "items", "--folderid", &folder_id, "--session", &session].to_vec()); + let result = String::from_utf8_lossy(&items.stdout).to_string(); + if result.is_empty() { + Err(anyhow!("Could not authenticate.")) + } + else { + Ok(serde_json::from_str(&result).with_context(|| "Could not deserialize item search results.")?) + } +} + /// Execute an interactive command. /// /// The resulting output will be returned as `Output`. This is a modified version of: /// /// https://users.rust-lang.org/t/command-if-a-child-process-is-asking-for-input-how-to-forward-the-question-to-the-user/37490/3 -fn exec_interactive_command(cmd: &str, args: Vec<&str>) -> Output { +fn exec_piped_command(cmd: &str, args: Vec<&str>) -> Output { let cli_command = match Command::new(cmd) .args(&args) - .stdin(Stdio::inherit()) .stdout(Stdio::piped()) - .stderr(Stdio::inherit()) .spawn() - { + { Err(err) => panic!("Error spawning: {}", err), Ok(process) => process, }; diff --git a/src/main.rs b/src/main.rs index 1d6ba4e..b815b4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,8 +37,14 @@ fn main() -> Result<()> { let session_token: String = check_session_token(&args)?; if !session_token.is_empty() { - let result = bwutil::exec_folder_search(&session_token, &args.folder)?; - println!("{:#?}", result); + let folders = bwutil::exec_folder_search(&session_token, &args.folder)?; + // Retrieve items from each folder since there may be multiple folders with the same name. + for folder in folders { + let folder_items = bwutil::exec_list_folder_items(&session_token, &folder.id)?; + for item in folder_items { + println!("{:#?}", item); + } + } } Ok(()) -- libgit2 1.7.2