Skip to content

Commit 54ddcb0

Browse files
committed
chore: minor cleanups on the tool download
1 parent 13e245c commit 54ddcb0

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/tools.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
//! Download management for external tools and applications. Locate and automatically download
22
//! applications (if needed) to use them in the build pipeline.
33
4-
use std::collections::HashMap;
5-
use std::path::PathBuf;
6-
4+
use self::archive::Archive;
5+
use crate::common::{is_executable, path_exists, path_exists_and};
76
use anyhow::{anyhow, bail, ensure, Context, Result};
87
use directories::ProjectDirs;
98
use futures_util::stream::StreamExt;
109
use once_cell::sync::Lazy;
10+
use std::collections::HashMap;
11+
use std::path::PathBuf;
1112
use tokio::fs::File;
1213
use tokio::io::AsyncWriteExt;
1314
use tokio::process::Command;
1415
use tokio::sync::{Mutex, OnceCell};
1516

16-
use self::archive::Archive;
17-
use crate::common::{is_executable, path_exists, path_exists_and};
18-
1917
/// The application to locate and eventually download when calling [`get`].
2018
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, strum::EnumIter)]
2119
pub enum Application {
@@ -34,7 +32,7 @@ pub enum Application {
3432
pub struct HttpClientOptions {
3533
/// Use this specific root certificate to validate the certificate chain. Optional.
3634
///
37-
/// Usefull when behind a corporate proxy that uses a self-signed root certificate.
35+
/// Useful when behind a corporate proxy that uses a self-signed root certificate.
3836
pub root_certificate: Option<PathBuf>,
3937
/// Allows Trunk to accept certificates that can't be verified when fetching dependencies. Defaults to false.
4038
///
@@ -247,21 +245,23 @@ impl AppCache {
247245
}
248246

249247
/// Locate the given application and download it if missing.
250-
#[tracing::instrument(level = "trace")]
248+
#[tracing::instrument(level = "debug")]
251249
pub async fn get(
252250
app: Application,
253251
version: Option<&str>,
254252
offline: bool,
255253
client_options: &HttpClientOptions,
256254
) -> Result<PathBuf> {
255+
tracing::debug!("Getting tool");
256+
257257
if let Some((path, detected_version)) = find_system(app).await {
258258
// consider system installed version
259259

260260
if let Some(required_version) = version {
261261
// we have a version requirement
262262
if required_version == detected_version {
263263
// and a match, so return early
264-
tracing::info!(app = %app.name(), %detected_version, "using system installed binary: {}", path.display());
264+
tracing::debug!(%detected_version, "using system installed binary: {}", path.display());
265265
return Ok(path);
266266
} else if offline {
267267
// a mismatch, in offline mode, we can't help here
@@ -271,7 +271,7 @@ pub async fn get(
271271
)
272272
} else {
273273
// a mismatch, so we need to download
274-
tracing::debug!(app = %app.name(), "tool version mismatch (required: {required_version}, system: {detected_version})");
274+
tracing::info!("tool version mismatch (required: {required_version}, system: {detected_version})");
275275
}
276276
} else {
277277
// we don't require any specific version
@@ -281,8 +281,9 @@ pub async fn get(
281281

282282
if offline {
283283
return Err(anyhow!(
284-
"couldn't find application {}, unable to download in offline mode",
285-
&app.name()
284+
"couldn't find application {name} (version: {version}), unable to download in offline mode",
285+
name = &app.name(),
286+
version = version.unwrap_or("<any>")
286287
));
287288
}
288289

@@ -309,8 +310,9 @@ pub async fn get(
309310
}
310311

311312
/// Try to find a global system installed version of the application.
312-
#[tracing::instrument(level = "trace")]
313+
#[tracing::instrument(level = "debug")]
313314
pub async fn find_system(app: Application) -> Option<(PathBuf, String)> {
315+
// we wrap this into an fn to easier deal with result -> option conversion
314316
let result = || async {
315317
let path = which::which(app.name())?;
316318
let output = Command::new(&path).arg(app.version_test()).output().await?;
@@ -329,7 +331,13 @@ pub async fn find_system(app: Application) -> Option<(PathBuf, String)> {
329331
Ok((path, system_version))
330332
};
331333

332-
result().await.ok()
334+
match result().await {
335+
Ok(result) => Some(result),
336+
Err(err) => {
337+
tracing::debug!("failed to detect system tool: {err}");
338+
None
339+
}
340+
}
333341
}
334342

335343
/// Download a file from its remote location in the given version, extract it and make it ready for

0 commit comments

Comments
 (0)