Skip to content

Commit

Permalink
Add --dry-run support to uv self update
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Dec 12, 2024
1 parent c0f8e20 commit a422b33
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
3 changes: 3 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ pub struct SelfUpdateArgs {
/// A token is not required but can be used to reduce the chance of encountering rate limits.
#[arg(long, env = EnvVars::UV_GITHUB_TOKEN)]
pub token: Option<String>,

#[arg(long)]
pub dry_run: bool,
}

#[derive(Args)]
Expand Down
52 changes: 41 additions & 11 deletions crates/uv/src/commands/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::printer::Printer;
pub(crate) async fn self_update(
version: Option<String>,
token: Option<String>,
dry_run: bool,
printer: Printer,
) -> Result<ExitStatus> {
let mut updater = AxoUpdater::new_for("uv");
Expand Down Expand Up @@ -87,7 +88,31 @@ pub(crate) async fn self_update(
UpdateRequest::Latest
};

updater.configure_version_specifier(update_request);
updater.configure_version_specifier(update_request.clone());

if dry_run {
// TODO: `updater.fetch_release` is not public, we can't say what the latest version is
if updater.is_update_needed().await? {
// TODO: `updater.version_specifier` is not public
let version = match update_request {
UpdateRequest::Latest | UpdateRequest::LatestMaybePrerelease => {
"the latest version".to_string()
}
UpdateRequest::SpecificTag(version) | UpdateRequest::SpecificVersion(version) => {
format!("v{version}")
}
};
writeln!(
printer.stderr(),
"Would upgrade uv from {} to {}",
format!("v{}", env!("CARGO_PKG_VERSION")).bold().white(),
version.bold().white(),
)?;
} else {
write_is_latest(&printer)?;
}
return Ok(ExitStatus::Success);
}

// Run the updater. This involves a network request, since we need to determine the latest
// available version of uv.
Expand Down Expand Up @@ -120,16 +145,7 @@ pub(crate) async fn self_update(
)?;
}
Ok(None) => {
writeln!(
printer.stderr(),
"{}",
format_args!(
"{}{} You're on the latest version of uv ({})",
"success".green().bold(),
":".bold(),
format!("v{}", env!("CARGO_PKG_VERSION")).bold().white()
)
)?;
write_is_latest(&printer)?;
}
Err(err) => {
return if let AxoupdateError::Reqwest(err) = err {
Expand All @@ -156,3 +172,17 @@ pub(crate) async fn self_update(

Ok(ExitStatus::Success)
}

fn write_is_latest(printer: &Printer) -> Result<(), anyhow::Error> {
writeln!(
printer.stderr(),
"{}",
format_args!(
"{}{} You're on the latest version of uv ({})",
"success".green().bold(),
":".bold(),
format!("v{}", env!("CARGO_PKG_VERSION")).bold().white()
)
)?;
Ok(())
}
3 changes: 2 additions & 1 deletion crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,9 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
SelfCommand::Update(SelfUpdateArgs {
target_version,
token,
dry_run,
}),
}) => commands::self_update(target_version, token, printer).await,
}) => commands::self_update(target_version, token, dry_run, printer).await,
#[cfg(not(feature = "self-update"))]
Commands::Self_(_) => {
anyhow::bail!(
Expand Down

0 comments on commit a422b33

Please sign in to comment.