From 90ac7820bdb1c020caf25202263e44481dedd50e Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Wed, 11 Dec 2024 19:22:03 -0600 Subject: [PATCH] Add `--dry-run` support to `uv self update` --- crates/uv-cli/src/lib.rs | 6 +++++ crates/uv/src/commands/self_update.rs | 34 ++++++++++++++++++++++++++- crates/uv/src/lib.rs | 3 ++- docs/reference/cli.md | 4 ++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index 49cbdc72098a..8d63995c571d 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -511,6 +511,12 @@ 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, + + /// Run without performing the update. + /// + /// uv will report if it would upgrade or not. + #[arg(long)] + pub dry_run: bool, } #[derive(Args)] diff --git a/crates/uv/src/commands/self_update.rs b/crates/uv/src/commands/self_update.rs index 1ba48666cec9..64ffc560dbad 100644 --- a/crates/uv/src/commands/self_update.rs +++ b/crates/uv/src/commands/self_update.rs @@ -15,6 +15,7 @@ use crate::printer::Printer; pub(crate) async fn self_update( version: Option, token: Option, + dry_run: bool, printer: Printer, ) -> Result { let mut updater = AxoUpdater::new_for("uv"); @@ -87,7 +88,38 @@ 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 update uv from {} to {}", + format!("v{}", env!("CARGO_PKG_VERSION")).bold().white(), + version.bold().white(), + )?; + } else { + writeln!( + printer.stderr(), + "{}", + format_args!( + "Nothing to do. You're on the latest version of uv ({})", + format!("v{}", env!("CARGO_PKG_VERSION")).bold().white() + ) + )?; + } + return Ok(ExitStatus::Success); + } // Run the updater. This involves a network request, since we need to determine the latest // available version of uv. diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index 8b326f5cb9bb..b34d5fdf9ac0 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -838,8 +838,9 @@ async fn run(mut cli: Cli) -> Result { 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!( diff --git a/docs/reference/cli.md b/docs/reference/cli.md index 987a18c5c979..56834f5b22b2 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -8762,6 +8762,10 @@ uv self update [OPTIONS] [TARGET_VERSION]

See --project to only change the project root directory.

+
--dry-run

Run without performing the update.

+ +

uv will report if it would upgrade or not.

+
--help, -h

Display the concise help for this command

--native-tls

Whether to load TLS certificates from the platform’s native certificate store.