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
use crate::*;
use colored::Colorize;


/**
   Public library API for Krecik remote-checks functionality
**/

/// Execute single check by exact file
pub fn execute_checks_from_file(check_path: &str) -> History {
    debug!(
        "Loading single check from file under path: {}",
        &check_path.cyan()
    );
    GenCheck::load(&check_path)
        .and_then(|check| {
            let file_name = file_name_from_path(check_path);
            debug!("Executing check: {}", file_name.magenta());
            Ok(check.execute(&file_name))
        })
        .unwrap_or_else(|err| {
            let error = format!(
                "Failed to load check from file: {}. Error details: {}",
                &check_path, err
            );
            error!("{}", error.red());
            History::new(Story::error(Unexpected::CheckParseProblem(error)))
        })
}


/// Execute all file checks from path
pub fn execute_checks_from_path(check_path: &str) -> History {
    debug!(
        "Loading all checks from local path: {}/*.json",
        &check_path.cyan()
    );
    History::new_from(
        list_check_files_from(&check_path)
            .into_iter()
            .flat_map(|check_resource| {
                let check_file = format!("{}/{}", check_path, check_resource);
                GenCheck::load(&check_file)
                    .and_then(|check| {
                        let file_name = file_name_from_path(&check_file);
                        debug!("Executing check from file: {}", file_name.magenta());
                        Ok(check.execute(&file_name))
                    })
                    .unwrap_or_else(|err| {
                        let error = format!(
                            "Failed to load check from file: {}. Error details: {}",
                            &check_file, err
                        );
                        error!("{}", error.red());
                        History::new(Story::error(Unexpected::CheckParseProblem(error)))
                    })
                    .stories()
            })
            .collect(),
    )
}


/// Remote PongoHost check request
pub fn execute_checks_from_remote_resource_defined_in_path(check_path: &str) -> History {
    debug!(
        "Loading checks from remote resources defined under path: {}",
        &check_path.cyan()
    );
    History::new_from(
        list_check_files_from(&check_path)
            .into_iter()
            .flat_map(|check_file| {
                let mapper = format!("{}/{}", check_path, check_file);
                debug!("Mapper file: {}", mapper);
                PongoHost::load(&mapper)
                    .and_then(|check| {
                        let file_name = file_name_from_path(&check_file);
                        debug!("Executing remote check from file: {}", file_name);
                        Ok(check.execute(&file_name))
                    })
                    .unwrap_or_else(|err| {
                        let error = format!(
                            "Failed to load remote check from file: {}. Error details: {}",
                            &mapper, err
                        );
                        error!("{}", error.red());
                        History::new(Story::error(Unexpected::CheckParseProblem(error)))
                    })
                    .stories()
            })
            .collect(),
    )
}