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

use anyhow::Result;

use crate::core::storage::WintsStorage;
use crate::util::log::{DRY_RUN, WRITE};

pub struct IgnoreUrlOptions {
    pub local_basedir: PathBuf,
    pub global_basedir: PathBuf,
    pub global: bool,
    pub url: String,
    pub dry_run: bool,
}

pub fn ignore_url(options: IgnoreUrlOptions) -> Result<()> {
    if options.dry_run {
        println!(" {} Dry-run mode activated.", DRY_RUN);
    }

    let scope = match options.global {
        true => "global ignore list",
        false => "ignore list",
    };
    println!(" {} Add '{}' to the {}...", WRITE, options.url, scope);

    match options.dry_run {
        true => {
            println!(" {} Add '{}' to the {}", DRY_RUN, options.url, scope);
        }
        false => {
            let mut storage = WintsStorage::load(
                options.local_basedir.clone(),
                options.global_basedir.clone(),
            )?;
            storage.ignore_url(options.url.clone(), options.global);
            storage.store()?;
            println!(
                " {} '{}' have been added to the {}",
                WRITE, options.url, scope
            );
        }
    }

    Ok(())
}