summaryrefslogtreecommitdiffstats
path: root/bin/core/imag-link/src/main.rs
blob: 79f70a43ced5a813a998f5506116d0fc8569e99e (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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//
// 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,
)]

#[macro_use] extern crate log;
extern crate clap;
extern crate url;
extern crate failure;
#[macro_use] extern crate prettytable;
#[cfg(test)] extern crate toml;
#[cfg(test)] extern crate toml_query;
#[cfg(test)] extern crate env_logger;

extern crate libimagentrylink;
#[macro_use] extern crate libimagrt;
extern crate libimagstore;
extern crate libimagerror;

#[cfg(test)]
#[macro_use]
extern crate libimagutil;

#[cfg(not(test))]
extern crate libimagutil;

use std::io::Write;
use std::path::PathBuf;

use failure::Error;
use failure::err_msg;

use libimagentrylink::external::ExternalLinker;
use libimagentrylink::internal::InternalLinker;
use libimagentrylink::internal::store_check::StoreLinkConsistentExt;
use libimagerror::trace::{MapErrTrace, trace_error};
use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
use libimagutil::warn_exit::warn_exit;
use libimagutil::warn_result::*;

use url::Url;
use failure::Fallible as Result;

mod ui;

use ui::build_ui;

fn main() {
    let version = make_imag_version!();
    let rt = generate_runtime_setup("imag-link",
                                    &version,
                                    "Link entries",
                                    build_ui);
    if rt.cli().is_present("check-consistency") {
        let exit_code = match rt.store().check_link_consistency() {
            Ok(_) => {
                info!("Store is consistent");
                0
            }
            Err(e) => {
                trace_error(&e);
                1
            }
        };
        ::std::process::exit(exit_code);
    }

    let _ = rt.cli()
        .subcommand_name()
        .map(|name| {
            match name {
                "remove" => remove_linking(&rt),
                "unlink" => unlink(&rt),
                "list"   => list_linkings(&rt),
                other    => {
                    debug!("Unknown command");
                    let _ = rt.handle_unknown_subcommand("imag-link", other, rt.cli())
                        .map_err_trace_exit_unwrap(1)
                        .code()
                        .map(::std::process::exit);
                },
            }
        })
        .or_else(|| {
            if let (Some(from), Some(to)) = (rt.cli().value_of("from"), rt.cli().values_of("to")) {
                Some(link_from_to(&rt, from, to))
            } else {
                warn_exit("No commandline call", 1)
            }
        })
        .ok_or_else(|| Error::from(err_msg("No commandline call".to_owned())))
        .map_err_trace_exit_unwrap(1);
}

fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<Option<FileLockEntry<'a>>> {
    use libimagstore::storeid::StoreId;

    debug!("Getting: {:?}", name);
    let result = StoreId::new(Some(rt.store().path().clone()), PathBuf::from(name))
        .and_then(|id| rt.store().get(id));

    debug!(" => : {:?}", result);
    result
}

fn link_from_to<'a, I>(rt: &'a Runtime, from: &'a str, to: I)
    where I: Iterator<Item = &'a str>
{
    let mut from_entry = match get_entry_by_name(rt, from).map_err_trace_exit_unwrap(1) {
        Some(e) => e,
        None    => {
            debug!("No 'from' entry");
            warn_exit("No 'from' entry", 1)
        },
    };

    for entry in to {
        debug!("Handling 'to' entry: {:?}", entry);
        if !rt.store().get(PathBuf::from(entry)).map_err_trace_exit_unwrap(1).is_some() {
            debug!("Linking externally: {:?} -> {:?}", from, entry);
            let url = Url::parse(entry).unwrap_or_else(|e| {
                error!("Error parsing URL: {:?}", e);
                ::std::process::exit(1);
            });

            let iter = from_entry
                .add_external_link(rt.store(), url)
                .map_err_trace_exit_unwrap(1)
                .into_iter();

            let _ = rt.report_all_touched(iter).unwrap_or_exit();
        } else {
            debug!("Linking internally: {:?} -> {:?}", from, entry);

            let from_id = StoreId::new_baseless(PathBuf::from(from)).map_err_trace_exit_unwrap(1);
            let entr_id = StoreId::new_baseless(PathBuf::from(entry)).map_err_trace_exit_unwrap(1);

            if from_id == entr_id {
                error!("Cannot link entry with itself. Exiting");
                ::std::process::exit(1)
            }

            let mut to_entry = match rt.store().get(entr_id).map_err_trace_exit_unwrap(1) {
                Some(e) => e,
                None    => {
                    warn!("No 'to' entry: {}", entry);
                    ::std::process::exit(1)
                },
            };
            let _ = from_entry
                .add_internal_link(&mut to_entry)
                .map_err_trace_exit_unwrap(1);

            let _ = rt.report_touched(to_entry.get_location()).unwrap_or_exit();
        }


        info!("Ok: {} -> {}", from, entry);
    }

    let _ = rt.report_touched(from_entry.get_location()).unwrap_or_exit();
}

fn remove_linking(rt: &Runtime) {
    let mut from = rt.cli()
        .subcommand_matches("remove")
        .unwrap() // safe, we know there is an "remove" subcommand
        .value_of("from")
        .map(PathBuf::from)
        .map(|id| {
            rt.store()
                .get(id)
                .map_err_trace_exit_unwrap(1)
                .ok_or_else(|| warn_exit("No 'from' entry", 1))
                .unwrap() // safe by line above
        })
        .unwrap();

    rt.ids::<::ui::PathProvider>()
        .map_err_trace_exit_unwrap(1)
        .into_iter()
        .for_each(|id| match rt.store().get(id.clone()) {
            Err(e) => trace_error(&e),
            Ok(Some(mut to_entry)) => {
                let _ = to_entry
                    .remove_internal_link(&mut from)
                    .map_err_trace_exit_unwrap(1);

                let _ = rt.report_touched(to_entry.get_location()).unwrap_or_exit();
            },
            Ok(None) => {
                // looks like this is not an entry, but a filesystem URI and therefor an
                // external link...?
                if id.local().is_file() {
                    let pb = id.local().to_str().unwrap_or_else(|| {
                        warn!("Not StoreId and not a Path: {}", id);
                        ::std::process::exit(1);
                    });
                    let url = Url::parse(pb).unwrap_or_else(|e| {
                        error!("Error parsing URL: {:?}", e);
                        ::std::process::exit(1);
                    });
                    from.remove_external_link(rt.store(), url).map_err_trace_exit_unwrap(1);
                    info!("Ok: {}", id);
                } else {
                    warn!("Entry not found: {:?}", id);
                }
            }
        });

    let _ = rt.report_touched(from.get_location()).unwrap_or_exit();
}

fn unlink(rt: &Runtime) {
    rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1).into_iter().for_each(|id| {
        rt.store()
            .get(id.clone())
            .map_err_trace_exit_unwrap(1)
            .unwrap_or_else(|| {
                warn!("No entry for {}", id);
                ::std::process::exit(1)
            })
            .unlink(rt.store())
            .map_err_trace_exit_unwrap(1);

        let _ = rt.report_touched(&id).unwrap_or_exit();
    });
}

fn list_linkings(rt: &Runtime) {
    let cmd = rt.cli()
        .subcommand_matches("list")
        .unwrap(); // safed by clap

    let list_externals  = cmd.is_present("list-externals-too");
    let list_plain      = cmd.is_present("list-plain");

    let mut tab = ::prettytable::Table::new();
    tab.set_titles(row!["#", "Link"]);

    rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1).into_iter().for_each(|id| {
        match rt.store().get(id.clone()) {
            Ok(Some(entry)) => {
                for (i, link) in entry.get_internal_links().map_err_trace_exit_unwrap(1).enumerate() {
                    let link = link
                        .to_str()
                        .map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e))
                        .ok();

                    if let Some(link) = link {
                        if list_plain {
                            let _ = writeln!(rt.stdout(), "{: <3}: {}", i, link)
                                .to_exit_code()
                                .unwrap_or_exit();
                        } else {
                            tab.add_row(row![i, link]);
                        }
                    }
                }

                if list_externals {
                    entry.get_external_links(rt.store())
                        .map_err_trace_exit_unwrap(1)
                        .enumerate()
                        .for_each(|(i, link)| {
                            let link = link
                                .map_err_trace_exit_unwrap(1)
                                .into_string();

                            if list_plain {
                                let _ = writeln!(rt.stdout(), "{: <3}: {}", i, link)
                                    .to_exit_code()
                                    .unwrap_or_exit();
                            } else {
                                tab.add_row(row![i, link]);
                            }
                        })
                }

                let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();

            },
            Ok(None)        => warn!("Not found: {}", id),
            Err(e)          => trace_error(&e),
        }

        let _ = rt.report_touched(&id).unwrap_or_exit();
    });

    if !list_plain {
        let out      = rt.stdout();
        let mut lock = out.lock();
        tab.print(&mut lock)
            .to_exit_code()
            .unwrap_or_exit();
    }
}

#[cfg(test)]
mod tests {
    use super::link_from_to;
    use super::remove_linking;

    use std::path::PathBuf;
    use std::ffi::OsStr;

    use toml::value::Value;
    use toml_query::read::TomlValueReadExt;
    use failure::Fallible as Result;
    use failure::Error;

    use libimagrt::runtime::Runtime;
    use libimagstore::storeid::StoreId;
    use libimagstore::store::{FileLockEntry, Entry};

    fn setup_logging() {
        let _ = ::env_logger::try_init();
    }

    make_mock_app! {
        app "imag-link";
        modulename mock;
        version env!("CARGO_PKG_VERSION");
        with help "imag-link mocking app";
    }
    use self::mock::generate_test_runtime;
    use self::mock::reset_test_runtime;

    fn create_test_default_entry<'a, S: AsRef<OsStr>>(rt: &'a Runtime, name: S) -> Result<StoreId> {
        let mut path = PathBuf::new();
        path.set_file_name(name);

        let default_entry = Entry::new(StoreId::new_baseless(PathBuf::from("")).unwrap())
            .to_str()
            .unwrap();

        debug!("Default entry constructed");

        let id = StoreId::new_baseless(path)?;
        debug!("StoreId constructed: {:?}", id);

        let mut entry = rt.store().create(id.clone())?;

        debug!("Entry constructed: {:?}", id);
        entry.get_content_mut().push_str(&default_entry);

        Ok(id)
    }

    fn get_entry_links<'a>(entry: &'a FileLockEntry<'a>) -> Result<&'a Value> {
        match entry.get_header().read(&"links.internal".to_owned()).map_err(Error::from)? {
            Some(v) => Ok(v),
            None    => panic!("Didn't find 'links' in {:?}", entry),
        }
    }

    fn links_toml_value<'a, I: IntoIterator<Item = &'static str>>(links: I) -> Value {
        Value::Array(links
                         .into_iter()
                         .map(|s| Value::String(s.to_owned()))
                         .collect())
    }

    #[test]
    fn test_link_modificates() {
        setup_logging();
        let rt = generate_test_runtime(vec!["test1", "test2"])
            .unwrap();

        debug!("Runtime created");

        let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
        let test_id2 = create_test_default_entry(&rt, "test2").unwrap();

        debug!("Entries created");

        link_from_to(&rt, "test1", vec!["test2"].into_iter());

        debug!("Linking done");

        let test_entry1 = rt.store().get(test_id1).unwrap().unwrap();
        let test_links1 = get_entry_links(&test_entry1).unwrap();

        let test_entry2 = rt.store().get(test_id2).unwrap().unwrap();
        let test_links2 = get_entry_links(&test_entry2).unwrap();

        debug!("Asserting");

        assert_ne!(*test_links1, links_toml_value(vec![]));
        assert_ne!(*test_links2, links_toml_value(vec![]));

        debug!("Test finished")
    }

    #[test]
    fn test_linking_links() {
        setup_logging();
        let rt = generate_test_runtime(vec!["test1", "test2"])
            .unwrap();

        debug!("Runtime created");

        let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
        let test_id2 = create_test_default_entry(&rt, "test2").unwrap();

        debug!("Test entries created");

        link_from_to(&rt, "test1", vec!["test2"].into_iter());

        debug!("Linking done");

        let test_entry1 = rt.store().get(test_id1).unwrap().unwrap();
        let test_links1 = get_entry_links(&test_entry1).unwrap();

        let test_entry2 = rt.store().get(test_id2).unwrap().unwrap();
        let test_links2 = get_entry_links(&test_entry2).unwrap();

        debug!("Asserting");

        assert_eq!(*test_links1, links_toml_value(vec!["test2"]));
        assert_eq!(*test_links2, links_toml_value(vec!["test1"]));
    }

    #[test]
    fn test_multilinking() {
        setup_logging();
        let rt = generate_test_runtime(vec!["test1", "test2"])
            .unwrap();

        debug!("Runtime created");

        let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
        let test_id2 = create_test_default_entry(&rt, "test2").unwrap();

        debug!("Test entries created");

        link_from_to(&rt, "test1", vec!["test2"].into_iter());
        link_from_to(&rt, "test1", vec!["test2"].into_iter());

        debug!("Linking done");

        let test_entry1 = rt.store().get(test_id1).unwrap().unwrap();
        let test_links1 = get_entry_links(&test_entry1).unwrap();

        let test_entry2 = rt.store().get(test_id2).unwrap().unwrap();
        let test_links2 = get_entry_links(&test_entry2).unwrap();

        debug!("Asserting");

        assert_eq!(*test_links1, links_toml_value(vec!["test2"]));
        assert_eq!(*test_links2, links_toml_value(vec!["test1"]));
    }

    #[test]
    fn test_linking_more_than_two() {
        setup_logging();
        let rt = generate_test_runtime(vec!["test1", "test2", "test3"])
            .unwrap();

        debug!("Runtime created");

        let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
        let test_id2 = create_test_default_entry(&rt, "test2").unwrap();
        let test_id3 = create_test_default_entry(&rt, "test3").unwrap();

        debug!("Test entries created");

        link_from_to(&rt, "test1", vec!["test2", "test3"].into_iter());
        link_from_to(&rt, "test1", vec!["test2", "test3"].into_iter());

        debug!("Linking done");

        let test_entry1 = rt.store().get(test_id1).unwrap().unwrap();
        let test_links1 = get_entry_links(&test_entry1).unwrap();

        let test_entry2 = rt.store().get(test_id2).unwrap().unwrap();
        let test_links2 = get_entry_links(&test_entry2).unwrap();

        let test_entry3 = rt.store().get(test_id3).unwrap().unwrap();
        let test_links3 = get_entry_links(&test_entry3).unwrap();

        debug!("Asserting");

        assert_eq!(*test_links1, links_toml_value(vec!["test2", "test3"]));
        assert_eq!(*test_links2, links_toml_value(vec!["test1"]));
        assert_eq!(*test_links3, links_toml_value(vec!["test1"]));
    }

    // Remove tests

    #[test]
    fn test_linking_links_unlinking_removes_links() {
        setup_logging();
        let rt = generate_test_runtime(vec!["test1", "test2"])
            .unwrap();

        debug!("Runtime created");

        let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
        let test_id2 = create_test_default_entry(&rt, "test2").unwrap();

        debug!("Test entries created");

        link_from_to(&rt, "test1", vec!["test2"].into_iter());

        debug!("Linking done");

        let rt = reset_test_runtime(vec!["remove", "test1", "test2"], rt)
            .unwrap();

        remove_linking(&rt);

        debug!("Linking removed");

        let test_entry1 = rt.store().get(test_id1).unwrap().unwrap();
        let test_links1 = get_entry_links(&test_entry1).unwrap();

        let test_entry2 = rt.store().get(test_id2).unwrap().unwrap();
        let test_links2 = get_entry_links(&test_entry2).unwrap();

        debug!("Asserting");

        assert_eq!(*test_links1, links_toml_value(vec![]));
        assert_eq!(*test_links2, links_toml_value(vec![]));
    }

    #[test]
    fn test_linking_and_unlinking_more_than_two() {
        setup_logging();
        let rt = generate_test_runtime(vec!["test1", "test2", "test3"])
            .unwrap();

        debug!("Runtime created");

        let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
        let test_id2 = create_test_default_entry(&rt, "test2").unwrap();
        let test_id3 = create_test_default_entry(&rt, "test3").unwrap();

        debug!("Test entries created");

        link_from_to(&rt, "test1", vec!["test2", "test3"].into_iter());

        debug!("linking done");

        let rt = reset_test_runtime(vec!["remove", "test1", "test2", "test3"], rt)
            .unwrap();

        remove_linking(&rt);

        debug!("linking removed");

        let test_entry1 = rt.store().get(test_id1).unwrap().unwrap();
        let test_links1 = get_entry_links(&test_entry1).unwrap();

        let test_entry2 = rt.store().get(test_id2).unwrap().unwrap();
        let test_links2 = get_entry_links(&test_entry2).unwrap();

        let test_entry3 = rt.store().get(test_id3).unwrap().unwrap();
        let test_links3 = get_entry_links(&test_entry3).unwrap();

        debug!("Asserting");

        assert_eq!(*test_links1, links_toml_value(vec![]));
        assert_eq!(*test_links2, links_toml_value(vec![]));
        assert_eq!(*test_links3, links_toml_value(vec![]));
    }
}