summaryrefslogtreecommitdiff
path: root/src/birka-cli.rs
blob: 3fd5420e596b8286cf296dab1d9f90febd59ed96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright © 2020 Jakob L. Kreuze <zerodaysfordays@sdf.org>
//
// 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 <http://www.gnu.org/licenses/>.

#[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<String> = 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 hash = database::hash_file(&path).unwrap();
            let id = tb.import_image(&path, &hash).unwrap();
            for arg in args[3..].iter() {
                tb.add_tag(arg).unwrap();
                tb.tag_image(id, arg).unwrap();
            }
        }
        "id_for" => {
            if args.len() < 3 {
                eprintln!("usage: {} id_for PATH ", args[0]);
                std::process::exit(1);
            }
            let path = Path::new(&args[2]).canonicalize().unwrap();
            println!("{}", tb.image_id(&path).unwrap());
        }
        "add_tags" => {
            if args.len() < 3 {
                eprintln!("usage: {} add_tags ID [TAGS ...]", args[0]);
                std::process::exit(1);
            }
            let id = args[2].parse::<i64>().unwrap();
            for arg in args[3..].iter() {
                tb.add_tag(arg).unwrap();
                tb.tag_image(id, arg).unwrap();
            }
        }
        "remove_tags" => {
            if args.len() < 3 {
                eprintln!("usage: {} remove_tags ID [TAGS ...]", args[0]);
                std::process::exit(1);
            }
            let id = args[2].parse::<i64>().unwrap();
            for arg in args[3..].iter() {
                tb.untag_image(id, arg).unwrap();
            }
        }
        "query" => {
            let tags = args[2..].iter().map(|s| s.as_str()).collect::<Vec<&str>>();
            for tag in tags.iter() {
                if tb.tag_id(tag).is_err() {
                    eprintln!("unknown tag: {}", tag);
                    std::process::exit(1);
                }
            }
            for image in tb.images_by_tags(&tags[..]).unwrap() {
                println!(
                    "{},{},{}{}",
                    image.id, image.blake2, image.orig_dir, image.filename
                );
            }
        }
        _ => {
            eprintln!("Unknown action '{}'", action);
            std::process::exit(1);
        }
    }
}