From a5359f162c30c5c8cb3dfa68308c20dc2a287cdc Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Fri, 4 Aug 2023 12:24:32 +0200 Subject: [PATCH] refactor!: `debug_println` no longer requires a boolean parameter BREAKING CHANGE: The signature for `debug_println` now matches `println` - A new environment variable is introduced and is set by `--debug` --- src/main.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 24362d5..e4e9e2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,24 @@ use std::{env, process::{Command, Stdio}}; use gumdrop::Options; use anyhow::{Result, Context}; +use colored::*; mod util; mod bwutil; /// Environment variable housing an existing Bitwarden session. const SESSION_ENV_KEY: &str = "BW_SESSION"; +const BW_SSH_DEBUG_ENV_KEY: &str = "BW_SSH_DEBUG"; /// Environment variable referencing the name of the field containing the SSH key passphrase (if any). const BW_FIELD_KEY_PASSPHRASE: &str = "BW_KEY_PASSPHRASE"; /// A macro to print when the debug CLI arg is enabled. macro_rules! debug_println { - ($debug:expr, $($arg:tt)*) => { - if $debug { - println!($($arg)*); + ($($arg:tt)*) => { + if let Ok(enabled) = env::var(BW_SSH_DEBUG_ENV_KEY) { + if bool::from_str(&enabled).unwrap_or(false) { + println!("{}",format!($($arg)*).yellow()); + } } } } @@ -27,7 +31,10 @@ fn main() -> Result<()> { return Ok(()) } + // Parse args and set the debug environment variable let args: util::Cli = util::Cli::parse_args_default_or_exit(); + + env::set_var(BW_SSH_DEBUG_ENV_KEY, OsString::from(args.debug.to_string())); if args.version { println!("{}", &util::get_version_string()?); return Ok(()); @@ -36,12 +43,11 @@ fn main() -> Result<()> { let session_token: String = check_session_token(&args)?; if !session_token.is_empty() { let folders = bwutil::exec_folder_search(&session_token, &args.folder)?; - debug_println!(args.debug, "Found {} folder(s) named `{}`", folders.len(), args.folder); + debug_println!("Found {} folder(s) named `{}`", folders.len().to_string().cyan(), args.folder.cyan()); // 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)?; - debug_println!(args.debug, "Found {} item(s) in folder `{}` id({})", folder_items.len(), folder.name, folder.id); for item in folder_items { // In order for this to be considered a valid SSH key item, this item needs to have the following fields: @@ -65,7 +71,6 @@ fn main() -> Result<()> { if let Some(attachments) = &item.attachments { for attachment in attachments { if attachment.file_name.eq(&key_filename) { - debug_println!(args.debug, "Item `{}` id({}) meets all requirements. Adding to `ssh-agent`", item.name, item.id); let _key = register_key(&item.id, &attachment.id, &key_passphrase, &session_token)?; } } -- libgit2 1.7.2