quickwit_common/
checklist.rs

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
// Copyright (C) 2021 Quickwit, Inc.
//
// Quickwit is offered under the AGPL v3.0 and as commercial software.
// For commercial licensing, contact us at hello@quickwit.io.
//
// AGPL:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use colored::{Color, Colorize};

/// Quickwit main colors slightly adapted to be readable on a terminal.
pub const BLUE_COLOR: Color = Color::TrueColor {
    r: 22,
    g: 74,
    b: 209,
};

pub const GREEN_COLOR: Color = Color::TrueColor {
    r: 22,
    g: 209,
    b: 142,
};
pub const WHITE_COLOR: Color = Color::TrueColor {
    r: 255,
    g: 255,
    b: 255,
};
pub const RED_COLOR: Color = Color::TrueColor {
    r: 230,
    g: 0,
    b: 34,
};

pub fn print_checklist(check_list_results: &[(&str, anyhow::Result<()>)]) {
    eprintln!(
        "\n{}\n{}",
        "---------------------------------------------------".color(GREEN_COLOR),
        " Connectivity checklist "
            .color(WHITE_COLOR)
            .on_color(GREEN_COLOR)
    );
    let mut errors = Vec::new();
    for (check_item_name, check_item_result) in check_list_results {
        let outcome_symbol = if check_item_result.is_ok() {
            "✔".color(GREEN_COLOR) // '✓'
        } else {
            "✖".color(RED_COLOR) //𐄂
        };
        eprintln!(" {} {}", outcome_symbol, check_item_name);
        if let Err(check_item_err) = check_item_result {
            errors.push((check_item_name, check_item_err));
        }
    }
    if errors.is_empty() {
        println!();
        return;
    }
    eprintln!(
        "{}\n{}",
        "---------------------------------------------------".color(RED_COLOR),
        " Error Details ".color(WHITE_COLOR).on_color(RED_COLOR)
    );
    for (check_item_name, check_item_err) in errors {
        eprintln!(
            "\n{}\n{:?}",
            format!(" ✖ {}", check_item_name).color(RED_COLOR),
            check_item_err
        );
    }
    eprintln!("\n\n");
}

/// Run a checklist and print out its successes and failures on stdout.
///
/// If an error is encountered, the proccess will exit with exit code 1.
pub fn run_checklist(checks: Vec<(&str, anyhow::Result<()>)>) {
    print_checklist(&checks);
    if !checks
        .iter()
        .all(|(_, check_items_res)| check_items_res.is_ok())
    {
        std::process::exit(1);
    }
}