summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakob L. Kreuze <zerodaysfordays@sdf.org>2020-06-14 18:56:22 -0400
committerJakob L. Kreuze <zerodaysfordays@sdf.org>2020-06-14 18:56:22 -0400
commit95742615ef31e7751663b0192a882fb7e5a9d718 (patch)
tree42cbddb2ed79f4f5b4ee7a8f167ae1aacf6ce08a /src
parent70f13d8277e71191b94443dde0758587e9e56115 (diff)
Don't expose image IDs through the CLI interface.
Diffstat (limited to 'src')
-rw-r--r--src/birka-cli.rs24
-rw-r--r--src/database.rs14
2 files changed, 25 insertions, 13 deletions
diff --git a/src/birka-cli.rs b/src/birka-cli.rs
index 703da7d..1e05712 100644
--- a/src/birka-cli.rs
+++ b/src/birka-cli.rs
@@ -51,30 +51,30 @@ fn main() {
std::process::exit(1);
}
let path = Path::new(&args[2]).canonicalize().unwrap();
- let id = tb.add_file(&path, &args[3..].to_vec()).unwrap();
+ tb.add_file(&path, &args[3..].to_vec()).unwrap();
}
"add_tags" => {
if args.len() < 3 {
- eprintln!("usage: {} add_tags ID [TAGS ...]", args[0]);
+ eprintln!("usage: {} add_tags PATH [TAGS ...]", args[0]);
std::process::exit(1);
}
- let id = args[2].parse::<i64>().unwrap();
- tb.tag_file(id, &args[3..].to_vec()).unwrap();
+ let file = tb.file_by_path(&args[2]).unwrap();
+ tb.tag_file(file.id, &args[3..].to_vec()).unwrap();
}
"remove_tags" => {
if args.len() < 3 {
- eprintln!("usage: {} remove_tags ID [TAGS ...]", args[0]);
+ eprintln!("usage: {} remove_tags PATH [TAGS ...]", args[0]);
std::process::exit(1);
}
- let id = args[2].parse::<i64>().unwrap();
- tb.untag_file(id, &args[3..].to_vec()).unwrap();
+ let file = tb.file_by_path(&args[2]).unwrap();
+ tb.untag_file(file.id, &args[3..].to_vec()).unwrap();
}
"query" => {
- if args.len() < 3 {
- eprintln!("usage: {} query QUERY_STRING", args[0]);
- std::process::exit(1);
- }
- let query = database::parse_query(&args[2]).unwrap();
+ let query = if args.len() < 3 {
+ database::parse_query("").unwrap()
+ } else {
+ database::parse_query(&args[2]).unwrap()
+ };
for file in tb.query(&query, None).unwrap() {
println!("{}", file.path);
}
diff --git a/src/database.rs b/src/database.rs
index bd5ae65..4e33fa7 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -264,6 +264,11 @@ impl TagDatabase {
})?)
}
+ /// Return the `File` object for the file at `path`.
+ pub fn file_by_path<T: AsRef<Path>>(&self, path: T) -> Result<File> {
+ self.file_by_id(self.id_for_path(path)?)
+ }
+
/// Return the files in the database satisfying `q` and `k`.
pub fn query(&self, q: &Query, k: Option<Keyset>) -> Result<Vec<File>> {
// Return an empty vector if the query contains tags that aren't in the
@@ -433,7 +438,7 @@ mod tests {
#[test]
fn test_add_file_fail_on_already_indexed() {
let tb = TagDatabase::new_mem().unwrap();
- let img = tb.add_file("test", &vec!["tag1"]).unwrap();
+ tb.add_file("test", &vec!["tag1"]).unwrap();
assert!(tb.add_file("test", &vec!["tag2"]).is_err());
}
@@ -570,6 +575,13 @@ mod tests {
}
#[test]
+ fn test_path_resolution() {
+ let tb = TagDatabase::new_mem().unwrap();
+ let img = tb.add_file("test1", &vec!["tag1", "tag2"]).unwrap();
+ assert_eq!(tb.file_by_path(img.path).unwrap().path, "test1");
+ }
+
+ #[test]
fn top_tags() {
let tb = TagDatabase::new_mem().unwrap();
let ids: Vec<_> = (1..4)