From 88681e0b8afcccc61eb5208b37515c046537eac7 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Fri, 4 Aug 2023 12:29:40 +0200 Subject: [PATCH] feat: add macros for `warn_println` and `info_println` --- src/main.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index e4e9e2d..27a8356 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -use std::{env, process::{Command, Stdio}}; +use std::{env, process::{Command, Stdio}, ffi::OsString}; +use std::str::FromStr; use gumdrop::Options; use anyhow::{Result, Context}; use colored::*; @@ -23,6 +24,18 @@ macro_rules! debug_println { } } +macro_rules! warn_println { + ($($arg:tt)*) => { + println!("{}",format!($($arg)*).yellow()); + } +} + +macro_rules! info_println { + ($($arg:tt)*) => { + println!("{}",format!($($arg)*).green()); + } +} + fn main() -> Result<()> { // This environment variable only set when calling ssh-agent, so return the passphrase to authenticate and then quit. let unlock_passphrase = env::var(BW_FIELD_KEY_PASSPHRASE); @@ -49,6 +62,8 @@ fn main() -> Result<()> { for folder in folders { let folder_items = bwutil::exec_list_folder_items(&session_token, &folder.id)?; + debug_println!("Found {} item(s) in folder `{}` id({}) with at least one custom field and attachment", folder_items.len().to_string().cyan(), folder.name.cyan(), folder.id.cyan()); + 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: // - private - (required) this is the filename of the attachment containing the SSH key. @@ -90,7 +105,7 @@ fn main() -> Result<()> { /// 4. Login or unlock based on login status and use that. /// An invalid `BW_SESSION` or `--session` will prompt a login. fn check_session_token(args: &util::Cli) -> Result { - println!("Getting Bitwarden session..."); + info_println!("Getting Bitwarden session..."); let mut session_token: String = String::new(); // Get session flag from the user if args.session.trim().is_empty() { @@ -98,18 +113,18 @@ fn check_session_token(args: &util::Cli) -> Result { let env_key = env::var(SESSION_ENV_KEY); match env_key { Ok(key) => { - println!("{} is set. Reusing existing session.", SESSION_ENV_KEY); + info_println!("{} is set. Reusing existing session.", SESSION_ENV_KEY); // We found it, set our session key session_token.push_str(&key); }, Err(_) => { // We don't have a token to reuse, so get it from Bitwarden - println!("{} is not set. Attempting to login.", SESSION_ENV_KEY); + warn_println!("{} is not set. Attempting to login.", SESSION_ENV_KEY); let token: &String = &bwutil::get_session_token()?; if !token.is_empty() { - println!("Successfully unlocked. To re-use this session, run:"); - println!("export {}=\"{}\"", SESSION_ENV_KEY, token); + info_println!("Successfully unlocked. To re-use this session, run:"); + info_println!("export {}=\"{}\"", SESSION_ENV_KEY, token); } session_token.push_str(token) } -- libgit2 1.7.2