Replace println with Printer for all dir commands & workspace dir followups (#16690)

## Summary

1. Discussed in review of #16678 that println should be replaced by
using `printer`. The `println` pattern was pretty consistent across all
the `dir` commands, so I've updated all of them in this PR (there are
some usages of `println` outside of `uv/src/commands` that I didn't
touch -- the use cases there seemed more complex and nuanced).
2. I missed two comments in the previous PR before merging, so updates
from those are in here as well.

## Test Plan

No behavior changes, existing tests for all commands pass.

---------

Signed-off-by: Mikayla Thompson <mrt@mikayla.codes>
This commit is contained in:
Mikayla Thompson 2025-11-11 13:36:31 -07:00 committed by GitHub
parent b81060674e
commit 00e2a0e54d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 53 additions and 27 deletions

View file

@ -1,20 +1,26 @@
use anstream::println;
use owo_colors::OwoColorize;
use std::fmt::Write;
use uv_auth::{PyxTokenStore, Service, TextCredentialStore};
use uv_fs::Simplified;
use crate::printer::Printer;
/// Show the credentials directory.
pub(crate) fn dir(service: Option<&Service>) -> anyhow::Result<()> {
pub(crate) fn dir(service: Option<&Service>, printer: Printer) -> anyhow::Result<()> {
if let Some(service) = service {
let pyx_store = PyxTokenStore::from_settings()?;
if pyx_store.is_known_domain(service.url()) {
println!("{}", pyx_store.root().simplified_display().cyan());
writeln!(
printer.stdout(),
"{}",
pyx_store.root().simplified_display().cyan()
)?;
return Ok(());
}
}
let root = TextCredentialStore::directory_path()?;
println!("{}", root.simplified_display().cyan());
writeln!(printer.stdout(), "{}", root.simplified_display().cyan())?;
Ok(())
}

View file

@ -1,10 +1,18 @@
use anstream::println;
use owo_colors::OwoColorize;
use std::fmt::Write;
use uv_cache::Cache;
use uv_fs::Simplified;
use crate::commands::ExitStatus;
use crate::printer::Printer;
/// Show the cache directory.
pub(crate) fn cache_dir(cache: &Cache) {
println!("{}", cache.root().simplified_display().cyan());
pub(crate) fn cache_dir(cache: &Cache, printer: Printer) -> anyhow::Result<ExitStatus> {
writeln!(
printer.stdout(),
"{}",
cache.root().simplified_display().cyan()
)?;
Ok(ExitStatus::Success)
}

View file

@ -1,22 +1,26 @@
use anstream::println;
use std::fmt::Write;
use anyhow::Context;
use owo_colors::OwoColorize;
use uv_fs::Simplified;
use uv_python::managed::{ManagedPythonInstallations, python_executable_dir};
use crate::printer::Printer;
/// Show the Python installation directory.
pub(crate) fn dir(bin: bool) -> anyhow::Result<()> {
pub(crate) fn dir(bin: bool, printer: Printer) -> anyhow::Result<()> {
if bin {
let bin = python_executable_dir()?;
println!("{}", bin.simplified_display().cyan());
writeln!(printer.stdout(), "{}", bin.simplified_display().cyan())?;
} else {
let installed_toolchains = ManagedPythonInstallations::from_settings(None)
.context("Failed to initialize toolchain settings")?;
println!(
writeln!(
printer.stdout(),
"{}",
installed_toolchains.root().simplified_display().cyan()
);
)?;
}
Ok(())

View file

@ -1,4 +1,5 @@
use anstream::println;
use std::fmt::Write;
use anyhow::Context;
use owo_colors::OwoColorize;
@ -6,15 +7,25 @@ use uv_fs::Simplified;
use uv_preview::Preview;
use uv_tool::{InstalledTools, tool_executable_dir};
use crate::printer::Printer;
/// Show the tool directory.
pub(crate) fn dir(bin: bool, _preview: Preview) -> anyhow::Result<()> {
pub(crate) fn dir(bin: bool, _preview: Preview, printer: Printer) -> anyhow::Result<()> {
if bin {
let executable_directory = tool_executable_dir()?;
println!("{}", executable_directory.simplified_display().cyan());
writeln!(
printer.stdout(),
"{}",
executable_directory.simplified_display().cyan()
)?;
} else {
let installed_tools =
InstalledTools::from_settings().context("Failed to initialize tools settings")?;
println!("{}", installed_tools.root().simplified_display().cyan());
writeln!(
printer.stdout(),
"{}",
installed_tools.root().simplified_display().cyan()
)?;
}
Ok(())

View file

@ -31,11 +31,11 @@ pub(crate) async fn dir(
let workspace =
Workspace::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache).await?;
let dir: &Path = match package_name {
None => workspace.install_path().as_path(),
let dir = match package_name {
None => workspace.install_path(),
Some(package) => {
if let Some(p) = workspace.packages().get(&package) {
p.root().as_path()
p.root()
} else {
bail!("Package `{package}` not found in workspace.")
}

View file

@ -531,7 +531,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
Commands::Auth(AuthNamespace {
command: AuthCommand::Dir(args),
}) => {
commands::auth_dir(args.service.as_ref())?;
commands::auth_dir(args.service.as_ref(), printer)?;
Ok(ExitStatus::Success)
}
Commands::Help(args) => commands::help(
@ -1056,10 +1056,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
}
Commands::Cache(CacheNamespace {
command: CacheCommand::Dir,
}) => {
commands::cache_dir(&cache);
Ok(ExitStatus::Success)
}
}) => commands::cache_dir(&cache, printer),
Commands::Cache(CacheNamespace {
command: CacheCommand::Size(args),
}) => commands::cache_size(&cache, args.human, printer, globals.preview),
@ -1504,7 +1501,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
let args = settings::ToolDirSettings::resolve(args, filesystem);
show_settings!(args);
commands::tool_dir(args.bin, globals.preview)?;
commands::tool_dir(args.bin, globals.preview, printer)?;
Ok(ExitStatus::Success)
}
Commands::Python(PythonNamespace {
@ -1680,7 +1677,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
let args = settings::PythonDirSettings::resolve(args, filesystem);
show_settings!(args);
commands::python_dir(args.bin)?;
commands::python_dir(args.bin, printer)?;
Ok(ExitStatus::Success)
}
Commands::Python(PythonNamespace {

View file

@ -27,7 +27,7 @@ fn workspace_dir_simple() {
);
}
// Workspace dir output when run with `--package`
/// Workspace dir output when run with `--package`.
#[test]
fn workspace_dir_specific_package() {
let context = TestContext::new("3.12");