implement history get

This commit is contained in:
Navid Sassan 2024-02-18 22:44:14 +01:00
parent a6f24bd0b6
commit 778d4e324f

View File

@ -44,7 +44,10 @@ enum CliCommands {
#[derive(Subcommand)]
enum HistoryCommands {
List {},
Get {},
Get {
index: usize,
field: String,
},
}
#[derive(Serialize, Deserialize)]
@ -68,15 +71,14 @@ fn main() {
debug!("Running history list");
handle_history_list();
}
HistoryCommands::Get {} => {
HistoryCommands::Get {index, field} => {
debug!("Running history get");
// handle_history_get();
handle_history_get(*index, field);
}
}
}
}
fn get_history() -> Vec<String> {
let file_path = "/tmp/bw-menu-history.json"; // TODO: use XDG paths
@ -429,3 +431,91 @@ fn handle_history_list() {
}
dbg!(&logins);
}
fn handle_history_get(index: usize, field: &String) {
let history = get_history();
if index >= history.len() {
eprintln!("Trying to get history element {}, but the history only contains {} elements", index, history.len());
process::exit(1);
}
let id = &history[index];
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);
process::exit(1);
}
};
// dbg!(&item.login);
// dbg!(&field);
match item.login {
Some(ref login) => {
match field.as_ref() {
"uris" => {
if let Some(uris) = &login.uris {
println!("{:?}", uris);
}
}
"username" => {
if let Some(username) = &login.username {
println!("{}", username);
}
}
"password" => {
if let Some(password) = &login.password {
println!("{}", password);
}
}
"totp" => {
if let Some(totp) = &login.totp {
println!("{}", totp);
}
}
"title" => {
let collections: HashMap<String, String> = 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<String, String> = 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 title = get_title(&item, &folders, &collections);
println!("{}", title);
}
_ => {
eprintln!("Could not find a field named '{}'", field);
process::exit(1);
}
}
}
None => {
error!("Login of Bitwarden item is none");
process::exit(2);
}
}
}