Skip to content

Commit

Permalink
Use errors everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 12, 2024
1 parent 3739fed commit 158b54f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
66 changes: 29 additions & 37 deletions crates/uv/src/commands/tool/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub(crate) async fn upgrade(
concurrency: Concurrency,
native_tls: bool,
cache: &Cache,

printer: Printer,
) -> Result<ExitStatus> {
let installed_tools = InstalledTools::from_settings()?.init()?;
Expand All @@ -55,13 +54,14 @@ pub(crate) async fn upgrade(

// Determine whether we applied any upgrades.
let mut did_upgrade = false;
// Determine whether a tool upgrade failed

// Determine whether any tool upgrade failed.
let mut failed_upgrade = false;

for name in names {
for name in &names {
debug!("Upgrading tool: `{name}`");
let changelog = upgrade_tool(
&name,
name,
printer,
&installed_tools,
&args,
Expand All @@ -75,32 +75,32 @@ pub(crate) async fn upgrade(

match changelog {
Ok(changelog) => {
if let Some(changelog) = changelog {
did_upgrade |= !changelog.is_empty();
did_upgrade |= !changelog.is_empty();
}
Err(err) => {
// If we have a single tool, return the error directly.
if names.len() > 1 {
writeln!(
printer.stderr(),
"Failed to upgrade `{}`: {err}",
name.cyan(),
)?;
} else {
failed_upgrade = true;
writeln!(printer.stderr(), "{err}")?;
}
}
Err(e) => {
writeln!(
printer.stderr(),
"Failed to upgrade `{}` due to `{e}`",
name.cyan(),
)?;

failed_upgrade = true;
}
}
}

if !did_upgrade && !failed_upgrade {
writeln!(printer.stderr(), "Nothing to upgrade")?;
}

if failed_upgrade {
return Ok(ExitStatus::Failure);
}

if !did_upgrade {
writeln!(printer.stderr(), "Nothing to upgrade")?;
}

Ok(ExitStatus::Success)
}

Expand All @@ -114,53 +114,45 @@ async fn upgrade_tool(
connectivity: Connectivity,
concurrency: Concurrency,
native_tls: bool,
) -> anyhow::Result<Option<Changelog>> {
) -> Result<Changelog> {
// Ensure the tool is installed.
let existing_tool_receipt = match installed_tools.get_tool_receipt(name) {
Ok(Some(receipt)) => receipt,
Ok(None) => {
let install_command = format!("uv tool install {name}");
writeln!(
printer.stderr(),
return Err(anyhow::anyhow!(
"`{}` is not installed; run `{}` to install",
name.cyan(),
install_command.green()
)?;
return Ok(None);
));
}
Err(_) => {
let install_command = format!("uv tool install --force {name}");
writeln!(
printer.stderr(),
return Err(anyhow::anyhow!(
"`{}` is missing a valid receipt; run `{}` to reinstall",
name.cyan(),
install_command.green()
)?;
return Ok(None);
));
}
};

let existing_environment = match installed_tools.get_environment(name, cache) {
Ok(Some(environment)) => environment,
Ok(None) => {
let install_command = format!("uv tool install {name}");
writeln!(
printer.stderr(),
return Err(anyhow::anyhow!(
"`{}` is not installed; run `{}` to install",
name.cyan(),
install_command.green()
)?;
return Ok(None);
));
}
Err(_) => {
let install_command = format!("uv tool install --force {name}");
writeln!(
printer.stderr(),
return Err(anyhow::anyhow!(
"`{}` is missing a valid environment; run `{}` to reinstall",
name.cyan(),
install_command.green()
)?;
return Ok(None);
));
}
};

Expand Down Expand Up @@ -216,5 +208,5 @@ async fn upgrade_tool(
)?;
}

Ok(Some(changelog))
Ok(changelog)
}
2 changes: 1 addition & 1 deletion crates/uv/tests/tool_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn test_tool_upgrade_not_stop_if_upgrade_fails() -> anyhow::Result<()> {
+ babel==2.14.0
- pytz==2018.5
Installed 1 executable: pybabel
`python-dotenv` is missing a valid receipt; run `uv tool install --force python-dotenv` to reinstall
Failed to upgrade `python-dotenv`: `python-dotenv` is missing a valid receipt; run `uv tool install --force python-dotenv` to reinstall
"###);

Ok(())
Expand Down

0 comments on commit 158b54f

Please sign in to comment.