From 81e6ab127d32d6a5cb04fce773217836fed14cff Mon Sep 17 00:00:00 2001 From: Jens Reimann Date: Fri, 4 Oct 2024 10:38:02 +0200 Subject: [PATCH] refactor: pull the offline flag on a global level This will also skip the update check if the flag is enabled. See https://github.com/trunk-rs/trunk/issues/732 --- src/cmd/build.rs | 4 +--- src/main.rs | 7 ++++++- src/version/enabled/mod.rs | 6 +++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 1192f0c2..60ac402c 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -32,9 +32,7 @@ pub struct Build { #[arg(short, long, env = "TRUNK_BUILD_DIST")] pub dist: Option, - /// Run without accessing the network - #[arg(long, env = "TRUNK_BUILD_OFFLINE")] - #[arg(default_missing_value="true", num_args=0..=1)] + #[arg(from_global)] pub offline: Option, /// Require Cargo.lock and cache are up to date diff --git a/src/main.rs b/src/main.rs index c32bb964..9a179396 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,6 +132,11 @@ struct Trunk { #[arg(long, global(true), env = "TRUNK_SKIP_VERSION_CHECK")] pub skip_version_check: bool, + /// Run without accessing the network + #[arg(long, global(true), env = "TRUNK_OFFLINE")] + #[arg(default_missing_value = "true", num_args=0..=1)] + pub offline: Option, + /// Color mode #[arg(long, env = "TRUNK_COLOR", global(true), value_enum, conflicts_with = "no_color", default_value_t = ColorMode::Auto)] pub color: ColorMode, @@ -167,7 +172,7 @@ enum ColorMode { impl Trunk { #[tracing::instrument(level = "trace", skip(self))] pub async fn run(self) -> Result<()> { - version::update_check(self.skip_version_check); + version::update_check(self.skip_version_check | self.offline.unwrap_or_default()); match self.action { TrunkSubcommands::Build(inner) => inner.run(self.config).await, diff --git a/src/version/enabled/mod.rs b/src/version/enabled/mod.rs index 26d322e4..27a90245 100644 --- a/src/version/enabled/mod.rs +++ b/src/version/enabled/mod.rs @@ -4,10 +4,14 @@ use crate::version::{ }; use semver::Version; use std::time::Duration; +use tracing::instrument; mod state; +#[instrument] pub fn update_check(skip: bool) { + tracing::trace!("Update check"); + if skip { return; } @@ -15,7 +19,7 @@ pub fn update_check(skip: bool) { tracing::debug!("Spawning update check"); // We need to spawn this in a dedicated tokio runtime, as otherwise this would block - // the current tokio runtime from existing. There seems to be an issue with where even + // the current tokio runtime from exiting. There seems to be an issue with where even // with an aborted spawned task, tokio will wait for it to end indefinitely. std::thread::spawn(|| { perform_update_check();