Import into database
This commit is contained in:
parent
03ef223108
commit
05f9b135da
|
@ -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 {
|
match json {
|
||||||
true => {
|
true => {
|
||||||
println!("{}", json!({
|
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());
|
info!("succesfully imported bookmarks from {}!", file_path.to_str().unwrap());
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
|
@ -45,6 +45,40 @@ fn open_database(json: bool, path: Option<PathBuf>) -> Option<sled::Db> {
|
||||||
return Some(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>) {
|
pub fn insert_entry(entry: &Bookmark, json: bool, path: Option<PathBuf>) {
|
||||||
let db = match open_database(json, path) {
|
let db = match open_database(json, path) {
|
||||||
Some(database) => database,
|
Some(database) => database,
|
||||||
|
|
|
@ -56,6 +56,6 @@ fn main() {
|
||||||
Commands::Delete { url } => database::remove_entry(url, json, storage_path),
|
Commands::Delete { url } => database::remove_entry(url, json, storage_path),
|
||||||
Commands::List { } => commands::list(json, storage_path),
|
Commands::List { } => commands::list(json, storage_path),
|
||||||
Commands::Export { file } => commands::export(file.to_path_buf(), 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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue