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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 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
//
#![forbid(unsafe_code)]
#![deny(
non_camel_case_types,
non_snake_case,
path_statements,
trivial_numeric_casts,
unstable_features,
unused_allocation,
unused_import_braces,
unused_imports,
unused_must_use,
unused_mut,
unused_qualifications,
while_true,
)]
extern crate clap;
#[macro_use] extern crate log;
#[macro_use] extern crate vobject;
extern crate toml;
extern crate toml_query;
extern crate handlebars;
extern crate walkdir;
extern crate uuid;
extern crate serde_json;
extern crate failure;
extern crate libimagcontact;
extern crate libimagstore;
#[macro_use] extern crate libimagrt;
extern crate libimagerror;
extern crate libimagutil;
extern crate libimaginteraction;
extern crate libimagentryedit;
use std::process::exit;
use std::path::PathBuf;
use std::io::Write;
use handlebars::Handlebars;
use clap::ArgMatches;
use toml_query::read::TomlValueReadTypeExt;
use walkdir::WalkDir;
use failure::Error;
use failure::err_msg;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagerror::trace::MapErrTrace;
use libimagerror::io::ToExitCode;
use libimagerror::exit::ExitUnwrap;
use libimagerror::iter::TraceIterator;
use libimagcontact::store::ContactStore;
use libimagcontact::contact::Contact;
use libimagcontact::deser::DeserVcard;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
mod ui;
mod util;
mod create;
use ui::build_ui;
use util::build_data_object_for_handlebars;
use create::create;
fn main() {
let version = make_imag_version!();
let rt = generate_runtime_setup("imag-contact",
&version,
"Contact management tool",
build_ui);
rt.cli()
.subcommand_name()
.map(|name| {
debug!("Call {}", name);
match name {
"list" => list(&rt),
"import" => import(&rt),
"show" => show(&rt),
"find" => find(&rt),
"create" => create(&rt),
other => {
debug!("Unknown command");
let _ = rt.handle_unknown_subcommand("imag-contact", other, rt.cli())
.map_err_trace_exit_unwrap()
.code()
.map(::std::process::exit);
},
}
});
}
fn list(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("list").unwrap();
let list_format = get_contact_print_format("contact.list_format", rt, &scmd);
let iterator = rt
.store()
.all_contacts()
.map_err_trace_exit_unwrap()
.into_get_iter(rt.store())
.trace_unwrap_exit()
.map(|fle| fle.ok_or_else(|| Error::from(err_msg("StoreId not found".to_owned()))))
.trace_unwrap_exit()
.map(|fle| {
let _ = rt.report_touched(fle.get_location()).unwrap_or_exit();
fle
})
.map(|e| e.deser())
.trace_unwrap_exit()
.enumerate();
if scmd.is_present("json") {
let v : Vec<DeserVcard> = iterator.map(|tpl| tpl.1).collect();
match ::serde_json::to_string(&v) {
Ok(s) => writeln!(rt.stdout(), "{}", s).to_exit_code().unwrap_or_exit(),
Err(e) => {
error!("Error generating JSON: {:?}", e);
::std::process::exit(1)
}
}
} else {
let output = rt.stdout();
let mut output = output.lock();
iterator
.map(|(i, dvcard)| build_data_object_for_handlebars(i, &dvcard))
.map(|data| list_format.render("format", &data).map_err(Error::from))
.trace_unwrap_exit()
.for_each(|s| {
writeln!(output, "{}", s).to_exit_code().unwrap_or_exit()
});
}
}
fn import(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("import").unwrap(); // secured by main
let path = scmd.value_of("path").map(PathBuf::from).unwrap(); // secured by clap
if !path.exists() {
error!("Path does not exist");
exit(1)
}
if path.is_file() {
let entry = rt
.store()
.retrieve_from_path(&path)
.map_err_trace_exit_unwrap();
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
} else if path.is_dir() {
for entry in WalkDir::new(path).min_depth(1).into_iter() {
let entry = entry
.map_err(Error::from)
.map_err_trace_exit_unwrap();
if entry.file_type().is_file() {
let pb = PathBuf::from(entry.path());
let fle = rt
.store()
.retrieve_from_path(&pb)
.map_err_trace_exit_unwrap();
let _ = rt.report_touched(fle.get_location()).unwrap_or_exit();
info!("Imported: {}", entry.path().to_str().unwrap_or("<non UTF-8 path>"));
} else {
warn!("Ignoring non-file: {}", entry.path().to_str().unwrap_or("<non UTF-8 path>"));
}
}
} else {
error!("Path is neither directory nor file");
exit(1)
}
}
fn show(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("show").unwrap();
let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap
let show_format = get_contact_print_format("contact.show_format", rt, &scmd);
let out = rt.stdout();
let mut outlock = out.lock();
rt.store()
.all_contacts()
.map_err_trace_exit_unwrap()
.into_get_iter(rt.store())
.trace_unwrap_exit()
.map(|o| o.unwrap_or_else(|| {
error!("Failed to get entry");
exit(1)
}))
.filter_map(|entry| {
let deser = entry.deser().map_err_trace_exit_unwrap();
if deser.uid()
.ok_or_else(|| {
error!("Could not get StoreId from Store::all_contacts(). This is a BUG!");
::std::process::exit(1)
})
.unwrap() // exited above
.starts_with(&hash)
{
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
Some(deser)
} else {
None
}
})
.enumerate()
.for_each(|(i, elem)| {
let data = build_data_object_for_handlebars(i, &elem);
let s = show_format
.render("format", &data)
.map_err(Error::from)
.map_err_trace_exit_unwrap();
let _ = writeln!(outlock, "{}", s).to_exit_code().unwrap_or_exit();
});
}
fn find(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("find").unwrap();
let grepstring = scmd
.values_of("string")
.unwrap() // safed by clap
.map(String::from)
.collect::<Vec<String>>();
// We don't know yet which we need, but we pay that price for simplicity of the codebase
let show_format = get_contact_print_format("contact.show_format",
|