Skip to content

Commit

Permalink
Add colors to edgee components test command (#224)
Browse files Browse the repository at this point in the history
* chore: add libs for colored output

* feat: improve test command output with colors
  • Loading branch information
alexcasalboni authored Feb 17, 2025
1 parent 7ba65e2 commit 698df85
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
30 changes: 29 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ bytes = "1.8.0"
cbc = "0.1.2"
chrono = "0.4.38"
clap = "4.5.21"
colored = "3.0.0"
colored_json = "4"
cookie = "0.18.1"
dirs = "6.0.0"
easy-ext = "1.0.2"
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ edition.workspace = true
[dependencies]
anyhow.workspace = true
clap = { workspace = true, features = ["derive", "env"] }
colored.workspace = true
colored_json.workspace = true
inquire = { workspace = true, features = ["editor"] }
miette = { workspace = true, features = ["fancy"] }
openssl.workspace = true
Expand Down
28 changes: 17 additions & 11 deletions crates/cli/src/commands/components/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use edgee_components_runtime::config::{ComponentsConfiguration, DataCollectionComponents};
use edgee_components_runtime::context::ComponentsContext;
use std::collections::HashMap;
use colored_json::prelude::*;
use colored::Colorize;

use edgee_components_runtime::data_collection::payload::{Event, EventType};

Expand Down Expand Up @@ -130,25 +132,25 @@ async fn test_data_collection_component(opts: Options) -> anyhow::Result<()> {
}

if opts.display_input {
println!("Settings: {:#?}", settings_map);
println!("{}: {}", "Settings".green(), serde_json::to_string_pretty(&settings_map)?.to_colored_json_auto()?);
}
for event in events {
println!("---------------------------------------------------");
let request = match event.event_type {
EventType::Page => {
println!("Calling page");
tracing::info!("Running test with `page` event\n");
component
.call_page(&mut store, &event.clone().into(), &settings)
.await
}
EventType::Track => {
println!("Calling track");
tracing::info!("Running test with `track` event\n");
component
.call_track(&mut store, &event.clone().into(), &settings)
.await
}
EventType::User => {
println!("Calling user");
tracing::info!("Running test with `user` event\n");
component
.call_user(&mut store, &event.clone().into(), &settings)
.await
Expand All @@ -162,18 +164,22 @@ async fn test_data_collection_component(opts: Options) -> anyhow::Result<()> {
};

if opts.display_input {
println!("Input Event: {}", serde_json::to_string_pretty(&event)?);
tracing::info!("Input event:\n");
println!("{}: {}\n", "Event".green(), serde_json::to_string_pretty(&event)?.to_colored_json_auto()?);
}

println!("EdgeeRequest object:");
println!("Method: {:#?}", request.method);
println!("URL: {:#?}", request.url);
println!("Headers: {:#?}", request.headers);
tracing::info!("Output from Wasm:");
println!("\n{} {{", "EdgeeRequest".green());
println!("\t{}: {:#?}", "Method".green(), request.method);
println!("\t{}: {}", "URL".green(), request.url.green());
let pretty_headers: HashMap<String, String> = request.headers.into_iter().collect();
println!("\t{}: {}", "Headers".green(), serde_json::to_string_pretty(&pretty_headers)?.to_colored_json_auto()?.replace("\n", "\n\t"));
if let Ok(pretty_json) = serde_json::from_str::<serde_json::Value>(&request.body) {
println!("Body: {}", serde_json::to_string_pretty(&pretty_json)?);
println!("\t{}: {}", "Body".green(), serde_json::to_string_pretty(&pretty_json)?.to_colored_json_auto()?.replace("\n", "\n\t"));
} else {
println!("Body: {:#?}", request.body);
println!("\t{}: {:#?}", "Body".green(), request.body);
}
println!("}}");
}

Ok(())
Expand Down

0 comments on commit 698df85

Please sign in to comment.