From ef2a83ff16e35907369e97fd8a2cadfb35f3ec91 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Sat, 15 Jun 2024 20:06:34 -0700 Subject: [PATCH] add colors to procedure logs --- Cargo.lock | 1 + bin/core/src/api/execute/procedure.rs | 19 ++++++++++--- bin/core/src/helpers/procedure.rs | 39 +++++++++++++++++++-------- lib/git/Cargo.toml | 1 + lib/git/src/lib.rs | 9 ++++++- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5997a6ca..57f2c5adf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1431,6 +1431,7 @@ version = "1.7.3" dependencies = [ "anyhow", "command", + "formatting", "monitor_client", "run_command", "tracing", diff --git a/bin/core/src/api/execute/procedure.rs b/bin/core/src/api/execute/procedure.rs index 6287a8bb0..2e2d7fcb8 100644 --- a/bin/core/src/api/execute/procedure.rs +++ b/bin/core/src/api/execute/procedure.rs @@ -1,5 +1,6 @@ use std::pin::Pin; +use formatting::{bold, colored, muted, Color}; use monitor_client::{ api::execute::RunProcedure, entities::{ @@ -51,7 +52,11 @@ fn resolve_inner( // and will panic otherwise. update.push_simple_log( "execute_procedure", - format!("executing procedure {}", procedure.name), + format!( + "{}: executing procedure '{}'", + muted("INFO"), + bold(&procedure.name) + ), ); // get the action state for the procedure (or insert default). @@ -75,12 +80,20 @@ fn resolve_inner( Ok(_) => { update.push_simple_log( "execution ok", - "the procedure has completed with no errors", + format!( + "{}: the procedure has {} with no errors", + muted("INFO"), + colored("completed", Color::Green) + ), ); } Err(e) => update.push_error_log( "execution error", - serialize_error_pretty(&e), + format!( + "{}: {}", + colored("ERROR", Color::Red), + serialize_error_pretty(&e) + ), ), } diff --git a/bin/core/src/helpers/procedure.rs b/bin/core/src/helpers/procedure.rs index c6aea319c..06e46d369 100644 --- a/bin/core/src/helpers/procedure.rs +++ b/bin/core/src/helpers/procedure.rs @@ -1,6 +1,7 @@ use std::time::{Duration, Instant}; use anyhow::{anyhow, Context, Ok}; +use formatting::{bold, colored, muted, Color}; use futures::future::join_all; use monitor_client::{ api::execute::Execution, @@ -26,7 +27,11 @@ pub async fn execute_procedure( } add_line_to_update( update, - &format!("executing stage: {}", stage.name), + &format!( + "{}: executing stage: '{}'", + muted("INFO"), + bold(&stage.name) + ), ) .await; let timer = Instant::now(); @@ -44,16 +49,19 @@ pub async fn execute_procedure( .await .with_context(|| { format!( - "failed stage '{}' execution after {:?}", - stage.name, + "{}: failed stage '{}' execution after {:?}", + colored("ERROR", Color::Red), + bold(&stage.name), timer.elapsed(), ) })?; add_line_to_update( update, &format!( - "finished stage '{}' execution in {:?} ✅", - stage.name, + "{}: {} stage '{}' execution in {:?}", + muted("INFO"), + colored("finished", Color::Green), + bold(&stage.name), timer.elapsed() ), ) @@ -72,9 +80,15 @@ async fn execute_stage( ) -> anyhow::Result<()> { let futures = executions.into_iter().map(|execution| async move { let now = Instant::now(); - add_line_to_update(update, &format!("executing: {execution:?}")) - .await; - let fail_log = format!("failed on {execution:?}"); + add_line_to_update( + update, + &format!("{}: executing: {execution:?}", muted("INFO")), + ) + .await; + let fail_log = format!( + "{}: failed on {execution:?}", + colored("ERROR", Color::Red) + ); let res = execute_execution(execution.clone(), parent_id, parent_name) .await @@ -82,7 +96,9 @@ async fn execute_stage( add_line_to_update( update, &format!( - "finished execution in {:?}: {execution:?}", + "{}: {} execution in {:?}: {execution:?}", + muted("INFO"), + colored("finished", Color::Green), now.elapsed() ), ) @@ -266,8 +282,9 @@ async fn execute_execution( Ok(()) } else { Err(anyhow!( - "execution not successful. see update {}", - update.id + "{}: execution not successful. see update '{}'", + colored("ERROR", Color::Red), + bold(&update.id), )) } } diff --git a/lib/git/Cargo.toml b/lib/git/Cargo.toml index c1a62828f..d8a2026f8 100644 --- a/lib/git/Cargo.toml +++ b/lib/git/Cargo.toml @@ -9,6 +9,7 @@ homepage.workspace = true [dependencies] monitor_client.workspace = true +formatting.workspace = true command.workspace = true # run_command.workspace = true diff --git a/lib/git/src/lib.rs b/lib/git/src/lib.rs index 0f8338d15..4a7f5b4b9 100644 --- a/lib/git/src/lib.rs +++ b/lib/git/src/lib.rs @@ -5,6 +5,7 @@ use std::{ use anyhow::Context; use command::run_monitor_command; +use formatting::{bold, muted}; use monitor_client::entities::{ all_logs_success, monitor_timestamp, to_monitor_name, update::Log, CloneArgs, LatestCommit, SystemCommand, @@ -252,7 +253,13 @@ pub async fn get_commit_hash_log( let log = Log { stage: "latest commit".into(), command, - stdout: format!("hash: {short}\nmessage: {msg}"), + stdout: format!( + "{} {}\n{} {}", + muted("hash:"), + bold(short), + muted("message:"), + bold(msg), + ), stderr: String::new(), success: true, start_ts,