diff --git a/README.md b/README.md index 2d6909a..ebe4a6e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ without needing a browser. It doesn't support icons or much of anything else right now. A configuration file will be stored in `~/.config/tinymark/tinymark.toml` -By default the database will be stored in `~/.local/share/tinymark` +By default the databases will be stored in `~/.local/share/tinymark` ## Using List stored bookmarks: @@ -34,3 +34,11 @@ Any command that returns a Bookmark will return a JSON serialized object of it. Otherwise it will return a 'status' key of value 'success' or 'fail', along with a 'reason' value with a full-length output. + +## Configuration +There is only one configuration file, stored as a TOML file. +The valid fields are: + +`json`: Manually specify to output as JSON. Set to `true` or `false` + +`storage_location`: The full path for where to store the databases. diff --git a/src/commands.rs b/src/commands.rs index 1cf3ef7..ccc8413 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -115,58 +115,77 @@ pub fn env_err(json: bool, e: VarError) { } pub fn export(file_path: PathBuf, json: bool, path: Option) { + let file = match File::create(&file_path) { + Ok(f) => f, + Err(e) => { + if json { + println!("{}", json!({ + "status": "fail", + "reason": e.to_string(), + })); + } else { + warn!("error opening file! {}", e); + } + std::process::exit(exitcode::DATAERR); + }, + }; + let writer = BufWriter::new(file); + + match database::get_all(json, path) { + Some(bookmarks) => serde_json::to_writer(writer, &bookmarks).unwrap(), + None => std::process::exit(exitcode::IOERR), + } + if json { println!("{}", json!({ - "status": "fail", - "reason": "unsupported command", + "status": "success", + "reason": format!("exported bookmarks to {}", file_path.to_str().unwrap()), })); } else { - let file = match File::create(&file_path) { - Ok(f) => f, - Err(e) => { - warn!("error opening file! {}", e); - std::process::exit(exitcode::DATAERR); - }, - }; - let writer = BufWriter::new(file); - - match database::get_all(json, path) { - Some(bookmarks) => serde_json::to_writer(writer, &bookmarks).unwrap(), - None => std::process::exit(exitcode::IOERR), - } info!("Succesfully exported bookmarks to {}!", file_path.to_str().unwrap()); } } pub fn import(file_path: PathBuf, json: bool, store_path: Option) { - match json { - true => { - println!("{}", json!({ - "status": "fail", - "reason": "unsupported command", - })); - }, - false => { - let file = match File::open(&file_path) { - Ok(f) => f, - Err(e) => { - warn!("error opening file! {}", e); - std::process::exit(exitcode::DATAERR); - } - }; - let reader = BufReader::new(file); + let file = match File::open(&file_path) { + Ok(f) => f, + Err(e) => { + if json { + println!("{}", json!({ + "status": "fail", + "reason": e.to_string(), + })); + } else { + warn!("error opening file! {}", e); + } + std::process::exit(exitcode::DATAERR); + } + }; + let reader = BufReader::new(file); - let bookmarks: Vec = match serde_json::from_reader(reader) { - Ok(contents) => contents, - Err(e) => { - warn!("error serializing file! {}", e); - std::process::exit(exitcode::DATAERR); - } - }; + let bookmarks: Vec = match serde_json::from_reader(reader) { + Ok(contents) => contents, + Err(e) => { + if json { + println!("{}", json!({ + "status": "fail", + "reason": e.to_string(), + })); + } else { + warn!("error serializing file! {}", e); + } + std::process::exit(exitcode::DATAERR); + } + }; - database::insert_multiple(&bookmarks, json, store_path); - info!("succesfully imported bookmarks from {}!", file_path.to_str().unwrap()); + database::insert_multiple(&bookmarks, json, store_path); - }, + if json { + println!("{}", json!({ + "status": "success", + "reason": format!("imported bookmarks from {}", file_path.to_str().unwrap()), + })); + } else { + info!("succesfully imported bookmarks from {}!", file_path.to_str().unwrap()); } } diff --git a/src/database.rs b/src/database.rs index ac9eebe..ebcd4fe 100644 --- a/src/database.rs +++ b/src/database.rs @@ -18,6 +18,7 @@ fn open_database(json: bool, path: Option) -> Option { let mut tmp_path = PathBuf::new(); tmp_path.push(val); tmp_path.push(".local/share/tinymark"); + tmp_path.push("bookmarks_db"); tmp_path }, Err(e) => { @@ -74,8 +75,21 @@ pub fn insert_multiple(entries: &Vec, json: bool, path: Option info!("succesfully applied batch insert"), - Err(e) => warn!("error in applying batch insert: {}", e), + Ok(_) => { + if !json { + info!("succesfully applied batch insert"); + } + }, + Err(e) => { + if json { + println!("{}", json!({ + "status": "fail", + "reason": e.to_string(), + })); + } else { + warn!("error in applying batch insert: {}", e); + } + }, } } diff --git a/src/main.rs b/src/main.rs index dfe0c6e..b397b40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,31 +31,12 @@ fn main() { json = args.json; } - let storage_path: Option = match &cfg.storage_location { - Some(path) => { - let HOME = env::var("HOME"); - match HOME { - Ok(val) => { - let mut new_path = PathBuf::new(); - new_path.push(val); - new_path.push(path); - Some(new_path) - }, - Err(e) => { - commands::env_err(json, e); - None - }, - } - }, - None => None, - }; - match &args.command { - Commands::Add { url, name, description, tags } => commands::add(url, name, description, tags, json, storage_path), - Commands::Edit { url } => commands::edit(json, url, storage_path), - 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, storage_path), + Commands::Add { url, name, description, tags } => commands::add(url, name, description, tags, json, cfg.storage_location), + Commands::Edit { url } => commands::edit(json, url, cfg.storage_location), + Commands::Delete { url } => database::remove_entry(url, json, cfg.storage_location), + Commands::List { } => commands::list(json, cfg.storage_location), + Commands::Export { file } => commands::export(file.to_path_buf(), json, cfg.storage_location), + Commands::Import { file } => commands::import(file.to_path_buf(), json, cfg.storage_location), } } diff --git a/src/structures.rs b/src/structures.rs index a788ed6..08a6d43 100644 --- a/src/structures.rs +++ b/src/structures.rs @@ -48,7 +48,7 @@ impl fmt::Display for Bookmark { None => do_nothing(), }; - print!("["); + print!("Tags: ["); for i in &self.tags { print!("{},", i); }