quickwit_common/
checklist.rs

1// Copyright (C) 2021 Quickwit, Inc.
2//
3// Quickwit is offered under the AGPL v3.0 and as commercial software.
4// For commercial licensing, contact us at hello@quickwit.io.
5//
6// AGPL:
7// This program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Affero General Public License as
9// published by the Free Software Foundation, either version 3 of the
10// License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU Affero General Public License for more details.
16//
17// You should have received a copy of the GNU Affero General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20use colored::{Color, Colorize};
21
22/// Quickwit main colors slightly adapted to be readable on a terminal.
23pub const BLUE_COLOR: Color = Color::TrueColor {
24    r: 22,
25    g: 74,
26    b: 209,
27};
28
29pub const GREEN_COLOR: Color = Color::TrueColor {
30    r: 22,
31    g: 209,
32    b: 142,
33};
34pub const WHITE_COLOR: Color = Color::TrueColor {
35    r: 255,
36    g: 255,
37    b: 255,
38};
39pub const RED_COLOR: Color = Color::TrueColor {
40    r: 230,
41    g: 0,
42    b: 34,
43};
44
45pub fn print_checklist(check_list_results: &[(&str, anyhow::Result<()>)]) {
46    eprintln!(
47        "\n{}\n{}",
48        "---------------------------------------------------".color(GREEN_COLOR),
49        " Connectivity checklist "
50            .color(WHITE_COLOR)
51            .on_color(GREEN_COLOR)
52    );
53    let mut errors = Vec::new();
54    for (check_item_name, check_item_result) in check_list_results {
55        let outcome_symbol = if check_item_result.is_ok() {
56            "✔".color(GREEN_COLOR) // '✓'
57        } else {
58            "✖".color(RED_COLOR) //𐄂
59        };
60        eprintln!(" {} {}", outcome_symbol, check_item_name);
61        if let Err(check_item_err) = check_item_result {
62            errors.push((check_item_name, check_item_err));
63        }
64    }
65    if errors.is_empty() {
66        println!();
67        return;
68    }
69    eprintln!(
70        "{}\n{}",
71        "---------------------------------------------------".color(RED_COLOR),
72        " Error Details ".color(WHITE_COLOR).on_color(RED_COLOR)
73    );
74    for (check_item_name, check_item_err) in errors {
75        eprintln!(
76            "\n{}\n{:?}",
77            format!(" ✖ {}", check_item_name).color(RED_COLOR),
78            check_item_err
79        );
80    }
81    eprintln!("\n\n");
82}
83
84/// Run a checklist and print out its successes and failures on stdout.
85///
86/// If an error is encountered, the proccess will exit with exit code 1.
87pub fn run_checklist(checks: Vec<(&str, anyhow::Result<()>)>) {
88    print_checklist(&checks);
89    if !checks
90        .iter()
91        .all(|(_, check_items_res)| check_items_res.is_ok())
92    {
93        std::process::exit(1);
94    }
95}