Skip to content

Commit

Permalink
Loading full items and exporting website URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
dteare committed Mar 9, 2022
1 parent 35493f0 commit 3779ae1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod op;
mod op7_metadata;

use op::{
find_items, load_all_accounts, load_all_vaults, AccountDetails, ItemOverview, VaultDetails,
load_all_accounts, load_all_items, load_all_vaults, AccountDetails, ItemDetails, VaultDetails,
};
use op7_metadata::write_items;

Expand Down Expand Up @@ -58,7 +58,7 @@ fn generate_opbookmarks(account_user_uuids: &Vec<String>, export_path: &std::pat

let accounts = accounts.unwrap();
let mut vaults_by_account: HashMap<AccountDetails, Vec<VaultDetails>> = HashMap::new();
let mut items_by_vault: HashMap<VaultDetails, Vec<ItemOverview>> = HashMap::new();
let mut items_by_vault: HashMap<VaultDetails, Vec<ItemDetails>> = HashMap::new();

println!(
"Exporting bookmarks for accounts {:?}",
Expand Down Expand Up @@ -88,7 +88,7 @@ fn generate_opbookmarks(account_user_uuids: &Vec<String>, export_path: &std::pat
// Collect the items for each vault
for (account, vaults) in vaults_by_account.iter() {
for vault in vaults.iter() {
let items = find_items(&account.id, &vault.id);
let items = load_all_items(&account.id, &vault.id);

match items {
Ok(items) => {
Expand Down
46 changes: 38 additions & 8 deletions src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ pub struct ItemOverview {
pub struct ItemDetails {
pub id: String,
pub title: String,
pub tags: Vec<String>,
pub tags: Option<Vec<String>>,
pub version: usize,
pub vault: VaultOverview,
pub category: String,
pub last_edited_by: String,
pub created_at: String,
pub updated_at: String,
pub urls: Vec<OPURL>,
pub urls: Option<Vec<OPURL>>,
}

#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct OPURL {
pub primary: bool,
pub primary: Option<bool>,
pub href: String,
}

Expand Down Expand Up @@ -257,6 +257,36 @@ pub fn get_vault(account_id: &String, vault_id: &String) -> Result<VaultDetails,
serde_json::from_slice(json.as_slice()).map_err(|e| Error::Deserialize(e))
}

pub fn load_all_items(account_id: &String, vault_id: &String) -> Result<Vec<ItemDetails>, Error> {
let items = find_items(&account_id, &vault_id);

match items {
Ok(items) => {
let mut details: Vec<ItemDetails> = vec![];
for item in items.iter() {
let item_details = get_item(&account_id, &vault_id, &item.id);

match item_details {
Ok(d) => details.push(d),
Err(e) => {
eprint!(
"Error loading item {} in vault {} for account {}: {:?}",
item.id, vault_id, account_id, e
);
return Err(Error::OPCLI(format!(
"Failed to load details for account {}",
account_id
)));
}
}
}

Ok(details)
}
Err(e) => Err(e),
}
}

pub fn find_items(account_id: &String, vault_id: &String) -> Result<Vec<ItemOverview>, Error> {
let output = Command::new("op")
.arg("--format")
Expand All @@ -283,15 +313,15 @@ pub fn find_items(account_id: &String, vault_id: &String) -> Result<Vec<ItemOver

// op --account BXRGOJ2Z5JB4RMA7FUYUURELUE --vault jnnjfdrzr5rawkimmsvp3zzzxe --format json item get fu5rgmahfihx4j6lludeyx3oei
pub fn get_item(
account: &AccountOverview,
vault: &VaultOverview,
item_id: String,
account_id: &String,
vault_id: &String,
item_id: &String,
) -> Result<ItemDetails, Error> {
let output = Command::new("op")
.arg("--account")
.arg(account.url.clone())
.arg(account_id)
.arg("--vault")
.arg(vault.id.clone())
.arg(vault_id)
.arg("--format")
.arg("json")
.arg("item")
Expand Down
19 changes: 15 additions & 4 deletions src/op7_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Create metadata files that conform to the format used by 1Password 7
use crate::op::{AccountDetails, ItemOverview, VaultDetails};
use crate::op::{AccountDetails, ItemDetails, VaultDetails};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
Expand Down Expand Up @@ -45,7 +45,7 @@ pub struct OP7ItemMetaData {

pub fn write_items(
export_path: &std::path::PathBuf,
items: &Vec<ItemOverview>,
items: &Vec<ItemDetails>,
vault: &VaultDetails,
account: &AccountDetails,
) {
Expand Down Expand Up @@ -97,10 +97,21 @@ fn write_file(path: std::path::PathBuf, contents: String) {
}

fn create_op7_metadata(
item: &ItemOverview,
item: &ItemDetails,
vault: &VaultDetails,
account_id: &String,
) -> OP7ItemMetaData {
let website_urls = match &item.urls {
Some(urls) => {
let mut result: Vec<String> = vec![];
for url in urls.iter() {
result.push(url.href.clone());
}
result
}
None => vec![],
};

return OP7ItemMetaData {
uuid: item.id.clone(),
item_description: format!("Login from {}", &vault.name.clone()),
Expand All @@ -109,7 +120,7 @@ fn create_op7_metadata(
vault_uuid: vault.id.clone(),
category_plural_name: item.category.clone(), // TODO: Map SECURE_NOTE, etc
profile_uuid: account_id.clone(),
website_urls: vec![],
website_urls: website_urls,
category_singular_name: item.category.clone(),
category_uuid: "001".to_string(),
account_name: "".to_string(), // TODO: Not sure anyone uses this?
Expand Down

0 comments on commit 3779ae1

Please sign in to comment.