summaryrefslogtreecommitdiff
path: root/bin/core/imag-link/src/main.rs
blob: 40155f960925cb342b7a6633ef20111ee6368455 (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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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
//

#![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;
#[cfg(test)] extern crate toml;
#[cfg(test)] extern crate toml_query;

extern crate libimagentrylink;
extern crate libimagrt;
extern crate libimagstore;
extern crate libimagerror;

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

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

use std::path::PathBuf;

use libimagentrylink::external::ExternalLinker;
use libimagentrylink::internal::InternalLinker;
use libimagentrylink::internal::store_check::StoreLinkConsistentExt;
use libimagentrylink::error::LinkError as LE;
use libimagerror::trace::{MapErrTrace, trace_error, trace_error_exit};
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagstore::error::StoreError;
use libimagstore::store::FileLockEntry;
use libimagutil::warn_exit::warn_exit;
use libimagutil::warn_result::*;

use url::Url;

mod ui;

use ui::build_ui;

fn main() {
    let rt = generate_runtime_setup("imag-link",
                                    env!("CARGO_PKG_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),
                "list"   => list_linkings(&rt),
                _ => panic!("BUG"),
            }
        })
        .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(LE::from("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>>, StoreError> {
    use libimagstore::storeid::StoreId;

    StoreId::new(Some(rt.store().path().clone()), PathBuf::from(name))
        .and_then(|id| rt.store().get(id))
}

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) {
        Ok(Some(e)) => e,
        Ok(None)    => warn_exit("No 'from' entry", 1),
        Err(e)      => trace_error_exit(&e, 1),
    };

    for entry in to {
        if PathBuf::from(entry).exists() {
            debug!("Linking externally: {:?} -> {:?}", from, entry);
            let url = Url::parse(entry).map_err_trace_exit_unwrap(1);
            let _ = from_entry
                .add_external_link(rt.store(), url)
                .map_err_trace_exit_unwrap(1);
        } else {
            debug!("Linking internally: {:?} -> {:?}", from, entry);
            let mut to_entry = match get_entry_by_name(rt, entry) {
                Ok(Some(e)) => e,
                Ok(None)    => {
                    warn!("No 'to' entry: {}", entry);
                    ::std::process::exit(1)
                },
                Err(e)      => trace_error_exit(&e, 1),
            };
            let _ = from_entry
                .add_internal_link(&mut to_entry)
                .map_err_trace_exit_unwrap(1);
        }

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

fn remove_linking(rt: &Runtime) {

    fn get_from_entry<'a>(rt: &'a Runtime) -> Option<FileLockEntry<'a>> {
        rt.cli()
            .subcommand_matches("remove")
            .unwrap() // safe, we know there is an "remove" subcommand
            .value_of("from")
            .and_then(|from_name| {
                match get_entry_by_name(rt, from_name) {
                    Err(e) => {
                        debug!("We couldn't get the entry from name: '{:?}'", from_name);
                        trace_error(&e); None
                    },
                    Ok(Some(e)) => Some(e),
                    Ok(None)    => None,
                }

            })
    }

    let mut from = match get_from_entry(&rt) {
        None => warn_exit("No 'from' entry", 1),
        Some(s) => s,
    };

    rt.cli()
        .subcommand_matches("remove")
        .unwrap()
        .values_of("to")
        .map(|values| {
            for (entry, value) in values.map(|v| (get_entry_by_name(rt, v), v)) {
                match entry {
                    Err(e) => trace_error(&e),
                    Ok(Some(mut to_entry)) => {
                        if let Err(e) = to_entry.remove_internal_link(&mut from) {
                            trace_error_exit(&e, 1);
                        }
                    },
                    Ok(None) => {
                        // looks like this is not an entry, but a filesystem URI and therefor an
                        // external link...?
                        if PathBuf::from(value).is_file() {
                            let url = Url::parse(value).map_err_trace_exit_unwrap(1);
                            from.remove_external_link(rt.store(), url).map_err_trace_exit_unwrap(1);
                            info!("Ok: {}", value);
                        } else {
                            warn!("Entry not found: {:?}", value);
                        }
                    }
                }
            }
        });
}

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");

    for entry in cmd.values_of("entries").unwrap() { // safed by clap
        match rt.store().get(PathBuf::from(entry)) {
            Ok(Some(entry)) => {
                let mut i = 0;

                for link in entry.get_internal_links().map_err_trace_exit_unwrap(1) {
                    let link = link
                        .to_str()
                        .map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e))
                        .ok();

                    if let Some(link) = link {
                        println!("{: <3}: {}", i, link);
                        i += 1;
                    }
                }

                if list_externals {
                    for link in entry.get_external_links(rt.store()).map_err_trace_exit_unwrap(1) {
                        let link = link
                            .map_err_trace_exit_unwrap(1)
                            .into_string();

                        println!("{: <3}: {}", i, link);
                        i += 1;
                    }
                }
            },
            Ok(None)        => warn!("Not found: {}", entry),
            Err(e)          => trace_error(&e),
        }
    }
}

#[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 toml_query::error::Result as TomlQueryResult;

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

    make_mock_app! {
        app "imag-link";
        modulename mock;
        version "0.6.0";
        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) -> StoreResult<StoreId> {
        let mut path = PathBuf::new();
        path.set_file_name(name);

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

        let id = StoreId::new_baseless(path)?;
        let mut entry = rt.store().create(id.clone())?;
        entry.get_content_mut().push_str(&default_entry);

        Ok(id)
    }

    fn get_entry_links<'a>(entry: &'a FileLockEntry<'a>) -> TomlQueryResult<&'a Value> {
        match entry.get_header().read(&"links.internal".to_owned()) {
            Err(e) => Err(e),
            Ok(Some(v)) => Ok(v),
            Ok(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() {
        let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"])
            .unwrap();

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

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

        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();

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

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

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

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

        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();

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

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

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

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

        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();

        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() {
        let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2", "test3"])
            .unwrap();

        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();

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

        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();

        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() {
        let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"])
            .unwrap();

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

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

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

        remove_linking(&rt);

        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();

        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() {
        let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2", "test3"])
            .unwrap();

        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();

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

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

        remove_linking(&rt);

        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();

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