Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
kakkoyun committed Sep 24, 2024
1 parent a0cc8ad commit 68b116d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
27 changes: 16 additions & 11 deletions crates/uv/src/commands/tool/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ pub(crate) async fn list(
return Ok(ExitStatus::Success);
}

writeln!(
printer.stdout(),
"Provide a command to invoke with `uvx <command>` or `uvx --from <package> <command>`.\n\n\
The following tools are already installed:\n"
)?;

// Create a string buffer and aggregate the tools with their versions, and entry points
let mut buffer = String::new();
for (name, tool) in tools {
// Skip invalid tools
let Ok(tool) = tool else {
Expand Down Expand Up @@ -79,14 +75,14 @@ pub(crate) async fn list(

if show_paths {
writeln!(
printer.stdout(),
buffer,
"{} ({})",
format!("{name} v{version}{version_specifier}").bold(),
installed_tools.tool_dir(&name).simplified_display().cyan(),
)?;
} else {
writeln!(
printer.stdout(),
buffer,
"{}",
format!("{name} v{version}{version_specifier}").bold()
)?;
Expand All @@ -96,17 +92,26 @@ pub(crate) async fn list(
for entrypoint in tool.entrypoints() {
if show_paths {
writeln!(
printer.stdout(),
buffer,
"- {} ({})",
entrypoint.name,
entrypoint.install_path.simplified_display().cyan()
)?;
} else {
writeln!(printer.stdout(), "- {}", entrypoint.name)?;
writeln!(buffer, "- {}", entrypoint.name)?;
}
}
}

writeln!(printer.stdout(), "\nSee `uvx --help` for more information.")?;
if buffer.is_empty() {
writeln!(printer.stderr(), "{no_tools_installed_msg}")?;
return Ok(ExitStatus::Success);
}

writeln!(
printer.stdout(),
"The following tools are already installed:\n"
)?;
writeln!(printer.stdout(), "{buffer}")?;
Ok(ExitStatus::Success)
}
12 changes: 11 additions & 1 deletion crates/uv/src/commands/tool/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,17 @@ pub(crate) async fn run(
) -> anyhow::Result<ExitStatus> {
// treat empty command as `uv tool list`
let Some(command) = command else {
return tool_list(false, false, &cache, printer).await;
writeln!(
printer.stdout(),
"Provide a command to invoke with `{invocation_source} <command>` \
or `{invocation_source} --from <package> <command>`.\n"
)?;
let result = tool_list(false, false, &cache, printer).await;
writeln!(
printer.stdout(),
"See `{invocation_source} --help` for more information."
)?;
return result;
};

let (target, args) = command.split();
Expand Down
33 changes: 6 additions & 27 deletions crates/uv/tests/tool_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ fn tool_list() {
success: true
exit_code: 0
----- stdout -----
Provide a command to invoke with `uvx <command>` or `uvx --from <package> <command>`.
The following tools are already installed:
black v24.2.0
- black
- blackd
See `uvx --help` for more information.
----- stderr -----
"###);
Expand All @@ -65,15 +62,12 @@ fn tool_list_paths() {
success: true
exit_code: 0
----- stdout -----
Provide a command to invoke with `uvx <command>` or `uvx --from <package> <command>`.
The following tools are already installed:
black v24.2.0 ([TEMP_DIR]/tools/black)
- black ([TEMP_DIR]/bin/black)
- blackd ([TEMP_DIR]/bin/blackd)
See `uvx --help` for more information.
----- stderr -----
"###);
Expand Down Expand Up @@ -122,15 +116,12 @@ fn tool_list_missing_receipt() {
success: true
exit_code: 0
----- stdout -----
Provide a command to invoke with `uvx <command>` or `uvx --from <package> <command>`.
The following tools are already installed:
See `uvx --help` for more information.
----- stderr -----
warning: Ignoring malformed tool `black` (run `uv tool uninstall black` to remove)
No tools installed.
See `uv tool install --help` for more information.
"###);
}

Expand Down Expand Up @@ -175,14 +166,11 @@ fn tool_list_bad_environment() -> Result<()> {
success: true
exit_code: 0
----- stdout -----
Provide a command to invoke with `uvx <command>` or `uvx --from <package> <command>`.
The following tools are already installed:
ruff v0.3.4
- ruff
See `uvx --help` for more information.
----- stderr -----
Invalid environment at `tools/black`: missing Python executable at `tools/black/[BIN]/python`
Expand Down Expand Up @@ -244,15 +232,12 @@ fn tool_list_deprecated() -> Result<()> {
success: true
exit_code: 0
----- stdout -----
Provide a command to invoke with `uvx <command>` or `uvx --from <package> <command>`.
The following tools are already installed:
black v24.2.0
- black
- blackd
See `uvx --help` for more information.
----- stderr -----
"###);
Expand All @@ -277,15 +262,12 @@ fn tool_list_deprecated() -> Result<()> {
success: true
exit_code: 0
----- stdout -----
Provide a command to invoke with `uvx <command>` or `uvx --from <package> <command>`.
The following tools are already installed:
See `uvx --help` for more information.
----- stderr -----
warning: Ignoring malformed tool `black` (run `uv tool uninstall black` to remove)
No tools installed.
See `uv tool install --help` for more information.
"###);

Ok(())
Expand All @@ -312,15 +294,12 @@ fn tool_list_show_version_specifiers() {
success: true
exit_code: 0
----- stdout -----
Provide a command to invoke with `uvx <command>` or `uvx --from <package> <command>`.
The following tools are already installed:
black v24.2.0 [required: <24.3.0]
- black
- blackd
See `uvx --help` for more information.
----- stderr -----
"###);
Expand Down
4 changes: 4 additions & 0 deletions crates/uv/tests/tool_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,10 @@ fn tool_run_list_installed() {
success: true
exit_code: 0
----- stdout -----
Provide a command to invoke with `uv tool run <command>` or `uv tool run --from <package> <command>`.
See `uv tool run --help` for more information.
----- stderr -----
No tools installed.
Expand Down

0 comments on commit 68b116d

Please sign in to comment.