Skip to content

Commit

Permalink
Fix the NO_COLOR environment variable interpretation. (#4011)
Browse files Browse the repository at this point in the history
Before this PR, NO_COLOR had to be true or false.
This PR interprets the environment variable as true for any value that
is non empty as described in https://no-color.org/

This fixes the config in our own configuration:
https://github.com/quickwit-oss/quickwit/blob/kzf2f6aacca1cd645a473b7a9ec0707a6efc6e323/docs/log-management/send-logs/using-otel-collector-with-helm.md?plain=1#L75
  • Loading branch information
fulmicoton authored Oct 23, 2023
1 parent 5f2f6aa commit 7414956
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions quickwit/quickwit-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ use crate::tool::{build_tool_command, ToolCliCommand};
pub fn build_cli() -> Command {
Command::new("Quickwit")
.arg(
// Following https://no-color.org/
Arg::new("no-color")
.long("no-color")
.help(
"Disable ANSI terminal codes (colors, etc...) being injected into the logging \
output",
)
.env("NO_COLOR")
.value_parser(clap::builder::FalseyValueParser::new())
.global(true)
.action(ArgAction::SetTrue),
)
Expand Down
6 changes: 3 additions & 3 deletions quickwit/quickwit-cli/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::QW_ENABLE_TOKIO_CONSOLE_ENV_KEY;

pub fn setup_logging_and_tracing(
level: Level,
ansi: bool,
ansi_colors: bool,
build_info: &BuildInfo,
) -> anyhow::Result<()> {
#[cfg(feature = "tokio-console")]
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn setup_logging_and_tracing(
.with(
tracing_subscriber::fmt::layer()
.event_format(event_format)
.with_ansi(ansi),
.with_ansi(ansi_colors),
)
.try_init()
.context("Failed to set up tracing.")?;
Expand All @@ -97,7 +97,7 @@ pub fn setup_logging_and_tracing(
.with(
tracing_subscriber::fmt::layer()
.event_format(event_format)
.with_ansi(ansi),
.with_ansi(ansi_colors),
)
.try_init()
.context("Failed to set up tracing.")?;
Expand Down
34 changes: 32 additions & 2 deletions quickwit/quickwit-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn main_impl() -> anyhow::Result<()> {

let app = build_cli().about(about_text).version(version_text);
let matches = app.get_matches();
let ansi = !matches.get_flag("no-color");
let ansi_colors = !matches.get_flag("no-color");

let command = match CliCommand::parse_cli_args(matches) {
Ok(command) => command,
Expand All @@ -63,7 +63,7 @@ async fn main_impl() -> anyhow::Result<()> {
#[cfg(feature = "jemalloc")]
start_jemalloc_metrics_loop();

setup_logging_and_tracing(command.default_log_level(), ansi, build_info)?;
setup_logging_and_tracing(command.default_log_level(), ansi_colors, build_info)?;
let return_code: i32 = if let Err(err) = command.execute().await {
eprintln!("{} Command failed: {:?}\n", "✘".color(RED_COLOR), err);
1
Expand Down Expand Up @@ -704,4 +704,34 @@ mod tests {
));
Ok(())
}

#[test]
fn test_parse_no_color() {
let previous_no_color_res = std::env::var("NO_COLOR");
{
std::env::set_var("NO_COLOR", "whatever_interpreted_as_true");
let app = build_cli().no_binary_name(true);
let matches = app.try_get_matches_from(["run"]).unwrap();
let no_color = matches.get_flag("no-color");
assert!(no_color);
}
{
// empty string is false.
std::env::set_var("NO_COLOR", "");
let app = build_cli().no_binary_name(true);
let matches = app.try_get_matches_from(["run"]).unwrap();
let no_color = matches.get_flag("no-color");
assert!(!no_color);
}
{
// empty string is false.
let app = build_cli().no_binary_name(true);
let matches = app.try_get_matches_from(["run", "--no-color"]).unwrap();
let no_color = matches.get_flag("no-color");
assert!(no_color);
}
if let Ok(previous_no_color) = previous_no_color_res {
std::env::set_var("NO_COLOR", previous_no_color);
}
}
}

0 comments on commit 7414956

Please sign in to comment.