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
use std::path::PathBuf;

use anyhow::Result;

use crate::core::storage::WintsStorage;
use crate::util::log::{DONE, DRY_RUN, INFO, SAD, SEARCH, WRITE};
use crate::util::url_scanner;

pub struct ScanOptions {
    pub local_basedir: PathBuf,
    pub global_basedir: PathBuf,
    pub module_name: String,
    pub global_module: bool,
    pub scan_path: PathBuf,
    pub dry_run: bool,
}

pub fn scan(options: ScanOptions) -> Result<()> {
    println!(" {} Using module '{}'", INFO, options.module_name);
    if options.dry_run {
        println!(" {} Dry-run mode activated.", DRY_RUN);
    }
    let mut storage = WintsStorage::load(
        options.local_basedir.clone(),
        options.global_basedir.clone(),
    )?;

    println!(" {} Scanning for new URLs...", SEARCH);
    let url_scanner_options = url_scanner::UrlScannerOptions {
        ignore_files: storage.list_of_ignore_files(),
        scan_path: options.scan_path.clone(),
    };
    let scanned_urls = url_scanner::scan_urls(url_scanner_options);
    let new_urls = find_new_urls(&options, &storage, scanned_urls);
    match new_urls.len() {
        0 => println!(" {} Scan found no new urls.", SAD),
        _ => {
            append_new_urls(options, &mut storage, new_urls)?;
            println!(" {} Scan completed.", DONE);
        }
    }
    Ok(())
}

fn append_new_urls(
    options: ScanOptions,
    storage: &mut WintsStorage,
    new_urls: Vec<String>,
) -> Result<()> {
    let url_word = match new_urls.len() {
        1 => "URL",
        _ => "URLs",
    };
    let context = "need to be contextualised";
    match options.dry_run {
        true => println!(
            " {} Append {} {} on the context '{}'.",
            DRY_RUN,
            new_urls.len(),
            url_word,
            context
        ),
        false => {
            storage.contextualise_urls(
                options.module_name,
                options.global_module,
                context,
                new_urls.clone(),
            );
            storage.store()?;
            println!(
                " {} {} {} have been added in context '{}', ready to be sorted.",
                WRITE,
                new_urls.len(),
                url_word,
                context
            );
        }
    }
    Ok(())
}

fn find_new_urls(
    options: &ScanOptions,
    storage: &WintsStorage,
    scanned_urls: Vec<String>,
) -> Vec<String> {
    let known_urls = storage.list_of_all_urls(options.module_name.clone());
    let ignored_urls = storage.ignored_urls();

    scanned_urls
        .into_iter()
        .filter(|url| !known_urls.contains(url) && !ignored_urls.contains(url))
        .collect()
}