From 47f7bed677a6b72e873712de8f3988ea891710e4 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 8 May 2024 22:45:06 -0400 Subject: chore: enable clippy::print_stdout and clippy::print_stderr (#23732) 1. Generally we should prefer to use the `log` crate. 2. I very often accidentally commit `eprintln`s. When we should use `println` or `eprintln`, it's not too bad to be a bit more verbose and ignore the lint rule. --- cli/tools/task.rs | 100 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 40 deletions(-) (limited to 'cli/tools/task.rs') diff --git a/cli/tools/task.rs b/cli/tools/task.rs index 4d14117d8..63c7f9a94 100644 --- a/cli/tools/task.rs +++ b/cli/tools/task.rs @@ -44,7 +44,11 @@ pub async fn execute_script( let task_name = match &task_flags.task { Some(task) => task, None => { - print_available_tasks(&tasks_config, &package_json_scripts); + print_available_tasks( + &mut std::io::stdout(), + &tasks_config, + &package_json_scripts, + )?; return Ok(1); } }; @@ -145,8 +149,14 @@ pub async fn execute_script( Ok(0) } else { - eprintln!("Task not found: {task_name}"); - print_available_tasks(&tasks_config, &package_json_scripts); + log::error!("Task not found: {task_name}"); + if log::log_enabled!(log::Level::Error) { + print_available_tasks( + &mut std::io::stderr(), + &tasks_config, + &package_json_scripts, + )?; + } Ok(1) } } @@ -239,48 +249,58 @@ fn collect_env_vars() -> HashMap { } fn print_available_tasks( - // order can be important, so these use an index map + writer: &mut dyn std::io::Write, tasks_config: &IndexMap, package_json_scripts: &IndexMap, -) { - eprintln!("{}", colors::green("Available tasks:")); - - let mut had_task = false; - for (is_deno, (key, task)) in tasks_config - .iter() - .map(|(k, t)| (true, (k, t.clone()))) - .chain( - package_json_scripts - .iter() - .filter(|(key, _)| !tasks_config.contains_key(*key)) - .map(|(k, v)| (false, (k, deno_config::Task::Definition(v.clone())))), - ) - { - eprintln!( - "- {}{}", - colors::cyan(key), - if is_deno { - "".to_string() - } else { - format!(" {}", colors::italic_gray("(package.json)")) - } - ); - let definition = match &task { - deno_config::Task::Definition(definition) => definition, - deno_config::Task::Commented { definition, .. } => definition, - }; - if let deno_config::Task::Commented { comments, .. } = &task { - let slash_slash = colors::italic_gray("//"); - for comment in comments { - eprintln!(" {slash_slash} {}", colors::italic_gray(comment)); +) -> Result<(), std::io::Error> { + writeln!(writer, "{}", colors::green("Available tasks:"))?; + + if tasks_config.is_empty() && package_json_scripts.is_empty() { + writeln!( + writer, + " {}", + colors::red("No tasks found in configuration file") + )?; + } else { + for (is_deno, (key, task)) in tasks_config + .iter() + .map(|(k, t)| (true, (k, t.clone()))) + .chain( + package_json_scripts + .iter() + .filter(|(key, _)| !tasks_config.contains_key(*key)) + .map(|(k, v)| (false, (k, deno_config::Task::Definition(v.clone())))), + ) + { + writeln!( + writer, + "- {}{}", + colors::cyan(key), + if is_deno { + "".to_string() + } else { + format!(" {}", colors::italic_gray("(package.json)")) + } + )?; + let definition = match &task { + deno_config::Task::Definition(definition) => definition, + deno_config::Task::Commented { definition, .. } => definition, + }; + if let deno_config::Task::Commented { comments, .. } = &task { + let slash_slash = colors::italic_gray("//"); + for comment in comments { + writeln!( + writer, + " {slash_slash} {}", + colors::italic_gray(comment) + )?; + } } + writeln!(writer, " {definition}")?; } - eprintln!(" {definition}"); - had_task = true; - } - if !had_task { - eprintln!(" {}", colors::red("No tasks found in configuration file")); } + + Ok(()) } struct NpxCommand; -- cgit v1.2.3