mirror of
https://github.com/astral-sh/uv.git
synced 2025-11-20 11:56:03 +00:00
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:
parent
b81060674e
commit
00e2a0e54d
7 changed files with 53 additions and 27 deletions
|
|
@ -1,20 +1,26 @@
|
||||||
use anstream::println;
|
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
use uv_auth::{PyxTokenStore, Service, TextCredentialStore};
|
use uv_auth::{PyxTokenStore, Service, TextCredentialStore};
|
||||||
use uv_fs::Simplified;
|
use uv_fs::Simplified;
|
||||||
|
|
||||||
|
use crate::printer::Printer;
|
||||||
|
|
||||||
/// Show the credentials directory.
|
/// 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 {
|
if let Some(service) = service {
|
||||||
let pyx_store = PyxTokenStore::from_settings()?;
|
let pyx_store = PyxTokenStore::from_settings()?;
|
||||||
if pyx_store.is_known_domain(service.url()) {
|
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(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let root = TextCredentialStore::directory_path()?;
|
let root = TextCredentialStore::directory_path()?;
|
||||||
println!("{}", root.simplified_display().cyan());
|
writeln!(printer.stdout(), "{}", root.simplified_display().cyan())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,18 @@
|
||||||
use anstream::println;
|
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
use uv_cache::Cache;
|
use uv_cache::Cache;
|
||||||
use uv_fs::Simplified;
|
use uv_fs::Simplified;
|
||||||
|
|
||||||
|
use crate::commands::ExitStatus;
|
||||||
|
use crate::printer::Printer;
|
||||||
|
|
||||||
/// Show the cache directory.
|
/// Show the cache directory.
|
||||||
pub(crate) fn cache_dir(cache: &Cache) {
|
pub(crate) fn cache_dir(cache: &Cache, printer: Printer) -> anyhow::Result<ExitStatus> {
|
||||||
println!("{}", cache.root().simplified_display().cyan());
|
writeln!(
|
||||||
|
printer.stdout(),
|
||||||
|
"{}",
|
||||||
|
cache.root().simplified_display().cyan()
|
||||||
|
)?;
|
||||||
|
Ok(ExitStatus::Success)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,26 @@
|
||||||
use anstream::println;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
|
|
||||||
use uv_fs::Simplified;
|
use uv_fs::Simplified;
|
||||||
use uv_python::managed::{ManagedPythonInstallations, python_executable_dir};
|
use uv_python::managed::{ManagedPythonInstallations, python_executable_dir};
|
||||||
|
|
||||||
|
use crate::printer::Printer;
|
||||||
|
|
||||||
/// Show the Python installation directory.
|
/// 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 {
|
if bin {
|
||||||
let bin = python_executable_dir()?;
|
let bin = python_executable_dir()?;
|
||||||
println!("{}", bin.simplified_display().cyan());
|
writeln!(printer.stdout(), "{}", bin.simplified_display().cyan())?;
|
||||||
} else {
|
} else {
|
||||||
let installed_toolchains = ManagedPythonInstallations::from_settings(None)
|
let installed_toolchains = ManagedPythonInstallations::from_settings(None)
|
||||||
.context("Failed to initialize toolchain settings")?;
|
.context("Failed to initialize toolchain settings")?;
|
||||||
println!(
|
writeln!(
|
||||||
|
printer.stdout(),
|
||||||
"{}",
|
"{}",
|
||||||
installed_toolchains.root().simplified_display().cyan()
|
installed_toolchains.root().simplified_display().cyan()
|
||||||
);
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use anstream::println;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
|
|
||||||
|
|
@ -6,15 +7,25 @@ use uv_fs::Simplified;
|
||||||
use uv_preview::Preview;
|
use uv_preview::Preview;
|
||||||
use uv_tool::{InstalledTools, tool_executable_dir};
|
use uv_tool::{InstalledTools, tool_executable_dir};
|
||||||
|
|
||||||
|
use crate::printer::Printer;
|
||||||
|
|
||||||
/// Show the tool directory.
|
/// 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 {
|
if bin {
|
||||||
let executable_directory = tool_executable_dir()?;
|
let executable_directory = tool_executable_dir()?;
|
||||||
println!("{}", executable_directory.simplified_display().cyan());
|
writeln!(
|
||||||
|
printer.stdout(),
|
||||||
|
"{}",
|
||||||
|
executable_directory.simplified_display().cyan()
|
||||||
|
)?;
|
||||||
} else {
|
} else {
|
||||||
let installed_tools =
|
let installed_tools =
|
||||||
InstalledTools::from_settings().context("Failed to initialize tools settings")?;
|
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(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,11 @@ pub(crate) async fn dir(
|
||||||
let workspace =
|
let workspace =
|
||||||
Workspace::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache).await?;
|
Workspace::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache).await?;
|
||||||
|
|
||||||
let dir: &Path = match package_name {
|
let dir = match package_name {
|
||||||
None => workspace.install_path().as_path(),
|
None => workspace.install_path(),
|
||||||
Some(package) => {
|
Some(package) => {
|
||||||
if let Some(p) = workspace.packages().get(&package) {
|
if let Some(p) = workspace.packages().get(&package) {
|
||||||
p.root().as_path()
|
p.root()
|
||||||
} else {
|
} else {
|
||||||
bail!("Package `{package}` not found in workspace.")
|
bail!("Package `{package}` not found in workspace.")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -531,7 +531,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
|
||||||
Commands::Auth(AuthNamespace {
|
Commands::Auth(AuthNamespace {
|
||||||
command: AuthCommand::Dir(args),
|
command: AuthCommand::Dir(args),
|
||||||
}) => {
|
}) => {
|
||||||
commands::auth_dir(args.service.as_ref())?;
|
commands::auth_dir(args.service.as_ref(), printer)?;
|
||||||
Ok(ExitStatus::Success)
|
Ok(ExitStatus::Success)
|
||||||
}
|
}
|
||||||
Commands::Help(args) => commands::help(
|
Commands::Help(args) => commands::help(
|
||||||
|
|
@ -1056,10 +1056,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
|
||||||
}
|
}
|
||||||
Commands::Cache(CacheNamespace {
|
Commands::Cache(CacheNamespace {
|
||||||
command: CacheCommand::Dir,
|
command: CacheCommand::Dir,
|
||||||
}) => {
|
}) => commands::cache_dir(&cache, printer),
|
||||||
commands::cache_dir(&cache);
|
|
||||||
Ok(ExitStatus::Success)
|
|
||||||
}
|
|
||||||
Commands::Cache(CacheNamespace {
|
Commands::Cache(CacheNamespace {
|
||||||
command: CacheCommand::Size(args),
|
command: CacheCommand::Size(args),
|
||||||
}) => commands::cache_size(&cache, args.human, printer, globals.preview),
|
}) => 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);
|
let args = settings::ToolDirSettings::resolve(args, filesystem);
|
||||||
show_settings!(args);
|
show_settings!(args);
|
||||||
|
|
||||||
commands::tool_dir(args.bin, globals.preview)?;
|
commands::tool_dir(args.bin, globals.preview, printer)?;
|
||||||
Ok(ExitStatus::Success)
|
Ok(ExitStatus::Success)
|
||||||
}
|
}
|
||||||
Commands::Python(PythonNamespace {
|
Commands::Python(PythonNamespace {
|
||||||
|
|
@ -1680,7 +1677,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
|
||||||
let args = settings::PythonDirSettings::resolve(args, filesystem);
|
let args = settings::PythonDirSettings::resolve(args, filesystem);
|
||||||
show_settings!(args);
|
show_settings!(args);
|
||||||
|
|
||||||
commands::python_dir(args.bin)?;
|
commands::python_dir(args.bin, printer)?;
|
||||||
Ok(ExitStatus::Success)
|
Ok(ExitStatus::Success)
|
||||||
}
|
}
|
||||||
Commands::Python(PythonNamespace {
|
Commands::Python(PythonNamespace {
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ fn workspace_dir_simple() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Workspace dir output when run with `--package`
|
/// Workspace dir output when run with `--package`.
|
||||||
#[test]
|
#[test]
|
||||||
fn workspace_dir_specific_package() {
|
fn workspace_dir_specific_package() {
|
||||||
let context = TestContext::new("3.12");
|
let context = TestContext::new("3.12");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue