Use full path in config file

This commit is contained in:
~erin 2021-12-30 08:46:00 -05:00
parent 05f9b135da
commit 5ac60c85d6
No known key found for this signature in database
GPG Key ID: DA70E064A8C70F44
5 changed files with 92 additions and 70 deletions

View File

@ -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.

View File

@ -115,58 +115,77 @@ pub fn env_err(json: bool, e: VarError) {
}
pub fn export(file_path: PathBuf, json: bool, path: Option<PathBuf>) {
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<PathBuf>) {
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<Bookmark> = match serde_json::from_reader(reader) {
Ok(contents) => contents,
Err(e) => {
warn!("error serializing file! {}", e);
std::process::exit(exitcode::DATAERR);
}
};
let bookmarks: Vec<Bookmark> = 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());
}
}

View File

@ -18,6 +18,7 @@ fn open_database(json: bool, path: Option<PathBuf>) -> Option<sled::Db> {
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<Bookmark>, json: bool, path: Option<PathBuf
}
match db.apply_batch(batch) {
Ok(_) => 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);
}
},
}
}

View File

@ -31,31 +31,12 @@ fn main() {
json = args.json;
}
let storage_path: Option<PathBuf> = 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),
}
}

View File

@ -48,7 +48,7 @@ impl fmt::Display for Bookmark {
None => do_nothing(),
};
print!("[");
print!("Tags: [");
for i in &self.tags {
print!("{},", i);
}