// Copyright © 2020 Jakob L. Kreuze // // This file is part of бирка-тян. // // бирка-тян is free software; you can redistribute it and/or modify it // under the terms of the GNU Affero General Public License as published // by the Free Software Foundation; either version 3 of the License, or // (at your option) any later version. // // бирка-тян is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public // License along with бирка-тян. If not, see . #[macro_use] extern crate anyhow; extern crate dirs; #[macro_use] extern crate rusqlite; mod database; use database::TagDatabase; use std::env; use std::path::Path; fn main() { let args: Vec = env::args().collect(); if args.len() < 2 { eprintln!("usage: {} ACTION [ARGS ...]", args[0]); std::process::exit(1); } let tb = if let Ok(val) = env::var("TB_PATH") { TagDatabase::new(&val) } else if let Some(dir) = dirs::config_dir() { TagDatabase::new(&dir.join("birka.db")) } else { TagDatabase::new(&Path::new("/tmp").join("birka.db")) } .unwrap(); let action = &args[1]; match action.to_lowercase().as_str() { "add" => { if args.len() < 3 { eprintln!("usage: {} add PATH [TAGS ...]", args[0]); std::process::exit(1); } let path = Path::new(&args[2]).canonicalize().unwrap(); let id = tb.add_file(&path, &args[3..].to_vec()).unwrap(); } "add_tags" => { if args.len() < 3 { eprintln!("usage: {} add_tags ID [TAGS ...]", args[0]); std::process::exit(1); } let id = args[2].parse::().unwrap(); tb.tag_file(id, &args[3..].to_vec()).unwrap(); } "remove_tags" => { if args.len() < 3 { eprintln!("usage: {} remove_tags ID [TAGS ...]", args[0]); std::process::exit(1); } let id = args[2].parse::().unwrap(); tb.untag_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(); for file in tb.query(&query, None).unwrap() { println!("{}", file.path); } } _ => { eprintln!("Unknown action '{}'", action); std::process::exit(1); } } }