From d6e0c6d34449224ac3687daf551a0acfd15cee32 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 23 Feb 2026 15:11:36 +0900 Subject: refactor(cli): route command output through Console abstraction Replace direct println\!/eprintln\! calls with console.write(), console.info(), and console.write_stdout() across all command handlers to respect verbosity settings. Co-Authored-By: Claude Opus 4.6 --- crates/mozart/src/commands/require.rs | 163 ++++++++++++++++------------------ 1 file changed, 75 insertions(+), 88 deletions(-) (limited to 'crates/mozart/src/commands/require.rs') diff --git a/crates/mozart/src/commands/require.rs b/crates/mozart/src/commands/require.rs index 46914cf..2dda5fb 100644 --- a/crates/mozart/src/commands/require.rs +++ b/crates/mozart/src/commands/require.rs @@ -1,4 +1,5 @@ use clap::Args; +use mozart_core::console::Verbosity; use mozart_core::console_format; use mozart_core::package::{self, Stability}; use mozart_core::validation; @@ -135,6 +136,7 @@ async fn interactive_search_packages( already_required: &std::collections::HashSet, preferred_stability: Stability, fixed: bool, + console: &mozart_core::console::Console, ) -> anyhow::Result> { let stdin = std::io::stdin(); if !stdin.is_terminal() { @@ -168,10 +170,9 @@ async fn interactive_search_packages( let (results, total) = match packagist::search_packages(&query, None).await { Ok(r) => r, Err(e) => { - eprintln!( - "{}", - console_format!("Search failed: {e}. Try again.") - ); + console.info(&console_format!( + "Search failed: {e}. Try again." + )); continue; } }; @@ -184,21 +185,18 @@ async fn interactive_search_packages( .collect(); if filtered.is_empty() { - eprintln!( - "{}", - console_format!( - "No new packages found for \"{query}\" (total: {total})." - ) - ); + console.info(&console_format!( + "No new packages found for \"{query}\" (total: {total})." + )); continue; } - eprintln!( + console.info(&format!( "\nFound {} package{} for \"{}\":", filtered.len(), if filtered.len() == 1 { "" } else { "s" }, query - ); + )); let name_width = filtered.iter().map(|r| r.name.len()).max().unwrap_or(0); for (idx, result) in filtered.iter().enumerate() { @@ -207,15 +205,15 @@ async fn interactive_search_packages( } else { format!(" — {}", result.description) }; - eprintln!( + console.info(&format!( " [{idx}] {:Invalid selection: {num}") - ); + console.info(&console_format!( + "Invalid selection: {num}" + )); continue; } } else { @@ -259,25 +256,21 @@ async fn interactive_search_packages( match validation::parse_require_string(&package_name) { Ok((n, v)) => (n.to_lowercase(), v), Err(e) => { - eprintln!("{}", console_format!("Invalid: {e}")); + console.info(&console_format!("Invalid: {e}")); continue; } } } else { if !validation::validate_package_name(&package_name) { - eprintln!( - "{}", - console_format!("Invalid package name: \"{package_name}\"") - ); + console.info(&console_format!( + "Invalid package name: \"{package_name}\"" + )); continue; } - eprintln!( - "{}", - console_format!( - "Using version constraint for {package_name} from Packagist..." - ) - ); + console.info(&console_format!( + "Using version constraint for {package_name} from Packagist..." + )); match packagist::fetch_package_versions(&package_name, None).await { Ok(versions) => { @@ -293,32 +286,23 @@ async fn interactive_search_packages( stability, ) }; - eprintln!( - "{}", - console_format!( - "Using version {c} for {package_name}" - ) - ); + console.info(&console_format!( + "Using version {c} for {package_name}" + )); (package_name, c) } None => { - eprintln!( - "{}", - console_format!( - "Could not find a version of \"{package_name}\" matching your minimum-stability. Try specifying it explicitly." - ) - ); + console.info(&console_format!( + "Could not find a version of \"{package_name}\" matching your minimum-stability. Try specifying it explicitly." + )); continue; } } } Err(e) => { - eprintln!( - "{}", - console_format!( - "Could not fetch versions for \"{package_name}\": {e}" - ) - ); + console.info(&console_format!( + "Could not fetch versions for \"{package_name}\": {e}" + )); continue; } } @@ -392,8 +376,13 @@ pub async fn execute( }) .unwrap_or(Stability::Stable); - let found = - interactive_search_packages(&already_required, preferred_stability, args.fixed).await?; + let found = interactive_search_packages( + &already_required, + preferred_stability, + args.fixed, + console, + ) + .await?; if found.is_empty() { // Nothing selected — exit cleanly @@ -470,11 +459,11 @@ pub async fn execute( anyhow::bail!("Invalid package name: \"{name}\""); } - println!( - "{}", - console_format!( + console.write_stdout( + &console_format!( "Using version constraint for {name} from Packagist..." - ) + ), + Verbosity::Normal, ); let versions = packagist::fetch_package_versions(&name, None).await?; @@ -497,9 +486,9 @@ pub async fn execute( ) }; - println!( - "{}", - console_format!("Using version {constraint} for {name}") + console.write_stdout( + &console_format!("Using version {constraint} for {name}"), + Verbosity::Normal, ); (name, constraint) @@ -525,23 +514,17 @@ pub async fn execute( if *is_dev { // Adding to require-dev: check require (prod) if raw.require.contains_key(name.as_str()) { - eprintln!( - "{}", - console_format!( - "{name} is currently present in the require key and will be moved to the require-dev key." - ) - ); + console.info(&console_format!( + "{name} is currently present in the require key and will be moved to the require-dev key." + )); raw.require.remove(name.as_str()); } } else { // Adding to require (prod): check require-dev if raw.require_dev.contains_key(name.as_str()) { - eprintln!( - "{}", - console_format!( - "{name} is currently present in the require-dev key and will be moved to the require key." - ) - ); + console.info(&console_format!( + "{name} is currently present in the require-dev key and will be moved to the require key." + )); raw.require_dev.remove(name.as_str()); } } @@ -557,16 +540,16 @@ pub async fn execute( }; if let Some(existing) = target.get(name) { - println!( - "{}", - console_format!( + console.write_stdout( + &console_format!( "Updating {name} from {existing} to {constraint} in {section_name}" - ) + ), + Verbosity::Normal, ); } else { - println!( - "{}", - console_format!("Adding {name} ({constraint}) to {section_name}") + console.write_stdout( + &console_format!("Adding {name} ({constraint}) to {section_name}"), + Verbosity::Normal, ); } @@ -592,9 +575,9 @@ pub async fn execute( // Write back composer.json (unless --dry-run) if args.dry_run { - println!( - "{}", - console_format!("Dry run: composer.json not modified.") + console.write_stdout( + &console_format!("Dry run: composer.json not modified."), + Verbosity::Normal, ); } else { package::write_to_file(&raw, &composer_path)?; @@ -602,11 +585,11 @@ pub async fn execute( // Handle --no-update: skip resolution entirely if args.no_update { - println!( - "{}", - console_format!( + console.write_stdout( + &console_format!( "Not updating dependencies, only modifying composer.json." - ) + ), + Verbosity::Normal, ); return Ok(()); } @@ -674,16 +657,20 @@ pub async fn execute( Err(e) => { // Fix 1: Revert composer.json (and composer.lock) on failure if !args.dry_run { - eprintln!( - "Installation failed, reverting ./composer.json to its original content." + console.write_error( + "Installation failed, reverting ./composer.json to its original content.", ); if let Err(revert_err) = std::fs::write(&composer_path, &original_composer_json) { - eprintln!("Warning: Failed to revert composer.json: {revert_err}"); + console.write_error(&format!( + "Warning: Failed to revert composer.json: {revert_err}" + )); } if let Some(ref lock_content) = original_composer_lock && let Err(revert_err) = std::fs::write(&lock_path_for_backup, lock_content) { - eprintln!("Warning: Failed to revert composer.lock: {revert_err}"); + console.write_error(&format!( + "Warning: Failed to revert composer.lock: {revert_err}" + )); } } return Err(mozart_core::exit_code::bail( -- cgit v1.3.1