diff --git a/src/main.rs b/src/main.rs index 994eb11..06c4996 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,10 +32,19 @@ struct Cli { #[derive(Subcommand)] enum CliCommands { - // Show the details of the item Select { title: Option, }, + History { + #[command(subcommand)] + command: HistoryCommands + }, +} + +#[derive(Subcommand)] +enum HistoryCommands { + List {}, + Get {}, } #[derive(Serialize, Deserialize)] @@ -52,7 +61,17 @@ fn main() { CliCommands::Select{title: _} => { // the title is always ignored, we want the id which we get via the environment variables debug!("Running in select mode"); - rofi(); + handle_select(); + } + CliCommands::History { command } => match &command { + HistoryCommands::List {} => { + debug!("Running history list"); + handle_history_list(); + } + HistoryCommands::Get {} => { + debug!("Running history get"); + // handle_history_get(); + } } } } @@ -125,7 +144,7 @@ fn add_history(entry: &String) { // // currently using the script mode so that we can pass additional info (eg the id). // no other mode supports this, meaning we would need to match based on the title -fn rofi() { +fn handle_select() { let rofi_retv = env::var("ROFI_RETV"); let rofi_info = env::var("ROFI_INFO"); dbg!(&rofi_retv); @@ -358,3 +377,55 @@ fn save_to_clipboard(input: String) { Err(e) => error!("Child thread encountered an error: {:?}", e), } } + + +fn handle_history_list() { + // TODO: not sure if we need this, would make more sense to just show these entries at the top + // of the normal selector + let history = get_history(); + + let collections: HashMap = match crate::bitwarden_api::list_object_collections() { + Ok(response) => { + info!("Got list of Bitwarden collections."); + response.data.data.iter() + .map(|item| (item.id.clone(), item.name.clone())) + .collect() + } + Err(msg) => { + error!("Failed to get list of Bitwarden collections:\n{msg}"); + process::exit(1); + } + }; + + let folders: HashMap = match crate::bitwarden_api::list_object_folders() { + Ok(response) => { + info!("Got list of Bitwarden folders."); + response.data.data.iter() + .filter(|item| item.id.is_some()) + .map(|item| (item.id.as_ref().unwrap().clone(), item.name.clone())) + .collect() + } + Err(msg) => { + error!("Failed to get list of Bitwarden folders:\n{msg}"); + process::exit(1); + } + }; + + let mut logins: HashMap = HashMap::new(); + for id in history { + dbg!(&id); + let item = match crate::bitwarden_api::object_item(&id) { + Ok(response) => response.data, + Err(msg) => { + error!("Failed to get Bitwarden item with id {}. It might not exist anymore.\n{}", id, msg); + continue; + } + }; + if item.login.is_some() { + let title = get_title(&item, &folders, &collections); + debug!("Login: {}", title); + logins.insert(title, item); + } + } + dbg!(&logins); +}