mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-18 09:50:03 +00:00
Find best download source out of alternatives (format extension) (#236)
This commit is contained in:
parent
19266a4fb6
commit
adef01f3dd
7 changed files with 93 additions and 52 deletions
|
@ -2,25 +2,21 @@ use std::path::Path;
|
|||
use std::sync::Arc;
|
||||
|
||||
use log::{debug, info, warn};
|
||||
use once_cell::sync::OnceCell;
|
||||
use reqwest::Client;
|
||||
use reqwest::Method;
|
||||
use serde::Serialize;
|
||||
use url::Url;
|
||||
|
||||
use super::Data;
|
||||
use crate::{download_and_extract, remote_exists, BinstallError, PkgFmt, Template};
|
||||
use crate::{
|
||||
download_and_extract, remote_exists, AutoAbortJoinHandle, BinstallError, PkgFmt, Template,
|
||||
};
|
||||
|
||||
pub struct GhCrateMeta {
|
||||
client: Client,
|
||||
data: Data,
|
||||
}
|
||||
|
||||
impl GhCrateMeta {
|
||||
fn url(&self) -> Result<Url, BinstallError> {
|
||||
let ctx = Context::from_data(&self.data);
|
||||
debug!("Using context: {:?}", ctx);
|
||||
ctx.render_url(&self.data.meta.pkg_url)
|
||||
}
|
||||
url: OnceCell<Url>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
|
@ -29,24 +25,52 @@ impl super::Fetcher for GhCrateMeta {
|
|||
Arc::new(Self {
|
||||
client: client.clone(),
|
||||
data: data.clone(),
|
||||
url: OnceCell::new(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn check(&self) -> Result<bool, BinstallError> {
|
||||
let url = self.url()?;
|
||||
async fn find(&self) -> Result<bool, BinstallError> {
|
||||
// build up list of potential URLs
|
||||
let urls = self.data.meta.pkg_fmt.extensions().iter().map(|ext| {
|
||||
let ctx = Context::from_data(&self.data, ext);
|
||||
ctx.render_url(&self.data.meta.pkg_url)
|
||||
});
|
||||
|
||||
if url.scheme() != "https" {
|
||||
warn!(
|
||||
"URL is not HTTPS! This may become a hard error in the future, tell the upstream!"
|
||||
);
|
||||
// go check all potential URLs at once
|
||||
let checks = urls
|
||||
.map(|url| {
|
||||
let client = self.client.clone();
|
||||
AutoAbortJoinHandle::new(tokio::spawn(async move {
|
||||
let url = url?;
|
||||
info!("Checking for package at: '{url}'");
|
||||
remote_exists(client, url.clone(), Method::HEAD)
|
||||
.await
|
||||
.map(|exists| (url.clone(), exists))
|
||||
}))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// get the first URL that exists
|
||||
for check in checks {
|
||||
let (url, exists) = check.await??;
|
||||
if exists {
|
||||
if url.scheme() != "https" {
|
||||
warn!(
|
||||
"URL is not HTTPS! This may become a hard error in the future, tell the upstream!"
|
||||
);
|
||||
}
|
||||
|
||||
info!("Winning URL is {url}");
|
||||
self.url.set(url).unwrap(); // find() is called first
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
|
||||
info!("Checking for package at: '{url}'");
|
||||
remote_exists(&self.client, url, Method::HEAD).await
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn fetch_and_extract(&self, dst: &Path) -> Result<(), BinstallError> {
|
||||
let url = self.url()?;
|
||||
let url = self.url.get().unwrap(); // find() is called first
|
||||
info!("Downloading package from: '{url}'");
|
||||
download_and_extract(&self.client, url, self.pkg_fmt(), dst).await
|
||||
}
|
||||
|
@ -56,7 +80,8 @@ impl super::Fetcher for GhCrateMeta {
|
|||
}
|
||||
|
||||
fn source_name(&self) -> String {
|
||||
self.url()
|
||||
self.url
|
||||
.get()
|
||||
.map(|url| {
|
||||
if let Some(domain) = url.domain() {
|
||||
domain.to_string()
|
||||
|
@ -66,7 +91,7 @@ impl super::Fetcher for GhCrateMeta {
|
|||
url.to_string()
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|_| "invalid url template".to_string())
|
||||
.unwrap_or_else(|| "invalid url".to_string())
|
||||
}
|
||||
|
||||
fn is_third_party(&self) -> bool {
|
||||
|
@ -87,11 +112,11 @@ struct Context<'c> {
|
|||
pub version: &'c str,
|
||||
|
||||
/// Soft-deprecated alias for archive-format
|
||||
pub format: String,
|
||||
pub format: &'c str,
|
||||
|
||||
/// Archive format e.g. tar.gz, zip
|
||||
#[serde(rename = "archive-format")]
|
||||
pub archive_format: String,
|
||||
pub archive_format: &'c str,
|
||||
|
||||
/// Filename extension on the binary, i.e. .exe on Windows, nothing otherwise
|
||||
#[serde(rename = "binary-ext")]
|
||||
|
@ -101,15 +126,14 @@ struct Context<'c> {
|
|||
impl<'c> Template for Context<'c> {}
|
||||
|
||||
impl<'c> Context<'c> {
|
||||
pub(self) fn from_data(data: &'c Data) -> Self {
|
||||
let pkg_fmt = data.meta.pkg_fmt.to_string();
|
||||
pub(self) fn from_data(data: &'c Data, archive_format: &'c str) -> Self {
|
||||
Self {
|
||||
name: &data.name,
|
||||
repo: data.repo.as_ref().map(|s| &s[..]),
|
||||
target: &data.target,
|
||||
version: &data.version,
|
||||
format: pkg_fmt.clone(),
|
||||
archive_format: pkg_fmt,
|
||||
format: archive_format,
|
||||
archive_format,
|
||||
binary_ext: if data.target.contains("windows") {
|
||||
".exe"
|
||||
} else {
|
||||
|
@ -119,6 +143,7 @@ impl<'c> Context<'c> {
|
|||
}
|
||||
|
||||
pub(self) fn render_url(&self, template: &str) -> Result<Url, BinstallError> {
|
||||
debug!("Render {template:?} using context: {:?}", self);
|
||||
Ok(Url::parse(&self.render(template)?)?)
|
||||
}
|
||||
}
|
||||
|
@ -144,7 +169,7 @@ mod test {
|
|||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data);
|
||||
let ctx = Context::from_data(&data, "tgz");
|
||||
assert_eq!(
|
||||
ctx.render_url(&data.meta.pkg_url).unwrap(),
|
||||
url("https://github.com/ryankurte/cargo-binstall/releases/download/v1.2.3/cargo-binstall-x86_64-unknown-linux-gnu-v1.2.3.tgz")
|
||||
|
@ -163,7 +188,7 @@ mod test {
|
|||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data);
|
||||
let ctx = Context::from_data(&data, "tgz");
|
||||
ctx.render_url(&data.meta.pkg_url).unwrap();
|
||||
}
|
||||
|
||||
|
@ -182,7 +207,7 @@ mod test {
|
|||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data);
|
||||
let ctx = Context::from_data(&data, "tgz");
|
||||
assert_eq!(
|
||||
ctx.render_url(&data.meta.pkg_url).unwrap(),
|
||||
url("https://example.com/releases/download/v1.2.3/cargo-binstall-x86_64-unknown-linux-gnu-v1.2.3.tgz")
|
||||
|
@ -206,7 +231,7 @@ mod test {
|
|||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data);
|
||||
let ctx = Context::from_data(&data, "tgz");
|
||||
assert_eq!(
|
||||
ctx.render_url(&data.meta.pkg_url).unwrap(),
|
||||
url("https://github.com/rust-iot/rust-radio-sx128x/releases/download/v0.14.1-alpha.5/sx128x-util-x86_64-unknown-linux-gnu-v0.14.1-alpha.5.tgz")
|
||||
|
@ -228,7 +253,7 @@ mod test {
|
|||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data);
|
||||
let ctx = Context::from_data(&data, "tgz");
|
||||
assert_eq!(
|
||||
ctx.render_url(&data.meta.pkg_url).unwrap(),
|
||||
url("https://github.com/rust-iot/rust-radio-sx128x/releases/download/v0.14.1-alpha.5/sx128x-util-x86_64-unknown-linux-gnu-v0.14.1-alpha.5.tgz")
|
||||
|
@ -253,7 +278,7 @@ mod test {
|
|||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data);
|
||||
let ctx = Context::from_data(&data, "txz");
|
||||
assert_eq!(
|
||||
ctx.render_url(&data.meta.pkg_url).unwrap(),
|
||||
url("https://github.com/watchexec/cargo-watch/releases/download/v9.0.0/cargo-watch-v9.0.0-aarch64-apple-darwin.tar.xz")
|
||||
|
@ -276,7 +301,7 @@ mod test {
|
|||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data);
|
||||
let ctx = Context::from_data(&data, "bin");
|
||||
assert_eq!(
|
||||
ctx.render_url(&data.meta.pkg_url).unwrap(),
|
||||
url("https://github.com/watchexec/cargo-watch/releases/download/v9.0.0/cargo-watch-v9.0.0-aarch64-pc-windows-msvc.exe")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue