summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-mail/src/ui.rs
blob: 3a928d87bede7e1ba56987f315bab48776239b5b (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
111
112
113
114
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use std::path::PathBuf;
use anyhow::Result;

use libimagstore::storeid::StoreId;
use libimagrt::runtime::IdPathProvider;
use libimagstore::storeid::IntoStoreId;

use clap::{Arg, ArgMatches, App, SubCommand};

pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
    app

        .arg(Arg::with_name("database_path")
             .long("db")
             .takes_value(true)
             .required(false)
             .value_name("PATH")
             .help("The path to the notmuch database to use (defaults to value in config)"))

        .subcommand(SubCommand::with_name("import")
                    .about("Import mails from notmuch")
                    .version("0.1")
                    .arg(Arg::with_name("query")
                         .index(1)
                         .takes_value(true)
                         .required(true)
                         .value_name("QUERY")
                         .help("Imports mails from notmuch to imag, using a query to fetch mails to import"))
                    )

        .subcommand(SubCommand::with_name("list")
                    .about("List mails in imag")
                    .version("0.1")
                    .arg(Arg::with_name("query")
                         .long("query")
                         .short("q")
                         .takes_value(true)
                         .required(false)
                         .value_name("QUERY")
                         .help("List mails in imag by using this notmuch query"))

                    .arg(Arg::with_name("ID")
                         .index(1)
                         .takes_value(true)
                         .required(false)
                         .multiple(true)
                         .value_name("ID")
                         .help("List mails by Store ID (non-mails will be ignored)"))

                    .arg(Arg::with_name("query-from-stdin")
                         .long("stdin")
                         .short("S")
                         .takes_value(false)
                         .required(false)
                         .multiple(false)
                         .help("Expect notmuch queries linewise on STDIN, for example from 'notmuch search --output=messages <query>'"))

                    .arg(Arg::with_name("format")
                         .long("format")
                         .short("f")
                         .takes_value(true)
                         .required(false)
                         .value_name("FORMAT")
                         .help("Format to list entries with (default in config)"))

                    )

        .subcommand(SubCommand::with_name("print-id")
                    .about("Print id of mail(s)")
                    .version("0.1")
                    .arg(Arg::with_name("ID")
                         .index(1)
                         .takes_value(true)
                         .multiple(true)
                         .required(false)
                         .value_name("ID")
                         .help("Print ID of mail(s) identified by Store ID(s)"))
                    )

}

pub struct PathProvider;
impl IdPathProvider for PathProvider {
    fn get_ids(matches: &ArgMatches) -> Result<Option<Vec<StoreId>>> {
        matches.values_of("ID")
            .map(|v| v
                 .map(PathBuf::from)
                 .map(|pb| pb.into_storeid())
                 .collect::<Result<Vec<_>>>()
            )
            .unwrap_or_else(|| Ok(Vec::new()))
            .map(Some)
    }
}