From f506835b5390719498554eaa8b41e5893f370728 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 6 Mar 2025 15:11:54 +0100 Subject: [PATCH] Add color to status --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 3 ++- src/main.rs | 10 ++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f3bca8..09a8264 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -181,6 +181,16 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "colored" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" +dependencies = [ + "lazy_static", + "windows-sys", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -455,6 +465,12 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.170" @@ -575,6 +591,7 @@ dependencies = [ "anyhow", "base64", "clap", + "colored", "env_logger", "http", "log", diff --git a/Cargo.toml b/Cargo.toml index 5ae84e4..52ba5ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ http = { version = "1.2.0" } anyhow = "1.0.97" url = "2.5.4" base64 = "0.22.1" -sha256 = "1.6.0" \ No newline at end of file +sha256 = "1.6.0" +colored = "2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2bde47d..2a0ebf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use sha256::{digest, Sha256Digest}; use url::Url; use base64::prelude::*; use base64::engine::general_purpose::URL_SAFE as BASE64_URL_URL_SAFE; +use colored::Colorize; /// Simple SCEP parser program #[derive(Parser, Debug)] @@ -141,13 +142,18 @@ fn main() { _ => panic!("Unsupported operation {}!", operation), }; + let status = match response.status().as_u16() { + 100..=199=> response.status().as_u16().to_string().blue(), + 200..=299 => response.status().as_u16().to_string().green(), + 400..=999 => response.status().as_u16().to_string().red(), + _ => response.status().as_u16().to_string().white().on_red(), + }; let date = response.headers().get("Date").unwrap().to_str().unwrap(); let sha_req = sha256::digest(request.body()).chars().take(10).collect::(); let sha_res = sha256::digest(response.body()).chars().take(10).collect::(); - // TODO : add color - println!("{date} - {} - {op:?}\t - Req: {sha_req}\t - Res: {sha_res}", response.status().as_u16()); + println!("{date} - {status} - {op:?}\t - Req: {sha_req}\t - Res: {sha_res}"); // Parse details about PKIOperation request // https://www.rfc-editor.org/rfc/rfc8894.html#section-4.3