Import into database

This commit is contained in:
~erin 2021-12-29 16:21:21 -05:00
parent 03ef223108
commit 05f9b135da
No known key found for this signature in database
GPG Key ID: DA70E064A8C70F44
3 changed files with 37 additions and 2 deletions

View File

@ -138,7 +138,7 @@ pub fn export(file_path: PathBuf, json: bool, path: Option<PathBuf>) {
}
}
pub fn import(file_path: PathBuf, json: bool) {
pub fn import(file_path: PathBuf, json: bool, store_path: Option<PathBuf>) {
match json {
true => {
println!("{}", json!({
@ -164,6 +164,7 @@ pub fn import(file_path: PathBuf, json: bool) {
}
};
database::insert_multiple(&bookmarks, json, store_path);
info!("succesfully imported bookmarks from {}!", file_path.to_str().unwrap());
},

View File

@ -45,6 +45,40 @@ fn open_database(json: bool, path: Option<PathBuf>) -> Option<sled::Db> {
return Some(db);
}
pub fn insert_multiple(entries: &Vec<Bookmark>, json: bool, path: Option<PathBuf>) {
let db = match open_database(json, path) {
Some(database) => database,
None => std::process::exit(exitcode::NOINPUT),
};
let mut batch = sled::Batch::default();
for i in entries {
let bytes;
match bincode::serialize(&i) {
Ok(result) => bytes = result,
Err(error) => {
if json {
println!("{}", json!({
"status": "fail",
"reason": error.to_string(),
}));
} else {
error!("failed serializing entry: {}", error);
}
std::process::exit(exitcode::DATAERR);
},
}
batch.insert(i.link.as_str(), bytes);
}
match db.apply_batch(batch) {
Ok(_) => info!("succesfully applied batch insert"),
Err(e) => warn!("error in applying batch insert: {}", e),
}
}
pub fn insert_entry(entry: &Bookmark, json: bool, path: Option<PathBuf>) {
let db = match open_database(json, path) {
Some(database) => database,

View File

@ -56,6 +56,6 @@ fn main() {
Commands::Delete { url } => database::remove_entry(url, json, storage_path),
Commands::List { } => commands::list(json, storage_path),
Commands::Export { file } => commands::export(file.to_path_buf(), json, storage_path),
Commands::Import { file } => commands::import(file.to_path_buf(), json),
Commands::Import { file } => commands::import(file.to_path_buf(), json, storage_path),
}
}