mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-15 15:16:37 +00:00
Add source name and prompt for third-party sources
This commit is contained in:
parent
d3f8802514
commit
a92336689a
6 changed files with 51 additions and 4 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -146,6 +146,7 @@ dependencies = [
|
||||||
"tempdir",
|
"tempdir",
|
||||||
"tinytemplate",
|
"tinytemplate",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"url",
|
||||||
"xz2",
|
"xz2",
|
||||||
"zip",
|
"zip",
|
||||||
]
|
]
|
||||||
|
|
|
@ -42,6 +42,7 @@ semver = "1.0.4"
|
||||||
xz2 = "0.1.6"
|
xz2 = "0.1.6"
|
||||||
zip = "0.5.13"
|
zip = "0.5.13"
|
||||||
async-trait = "0.1.52"
|
async-trait = "0.1.52"
|
||||||
|
url = "2.2.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
env_logger = "0.9.0"
|
env_logger = "0.9.0"
|
||||||
|
|
|
@ -23,6 +23,12 @@ pub trait Fetcher {
|
||||||
|
|
||||||
/// Return the package format
|
/// Return the package format
|
||||||
fn pkg_fmt(&self) -> PkgFmt;
|
fn pkg_fmt(&self) -> PkgFmt;
|
||||||
|
|
||||||
|
/// A short human-readable name or descriptor for the package source
|
||||||
|
fn source_name(&self) -> String;
|
||||||
|
|
||||||
|
/// Should return true if the remote is from a third-party source
|
||||||
|
fn is_third_party(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Data required to fetch a package
|
/// Data required to fetch a package
|
||||||
|
|
|
@ -3,12 +3,13 @@ use std::path::Path;
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
use super::Data;
|
use super::Data;
|
||||||
use crate::{download, remote_exists, PkgFmt, Template};
|
use crate::{download, remote_exists, PkgFmt, Template};
|
||||||
|
|
||||||
pub struct GhCrateMeta {
|
pub struct GhCrateMeta {
|
||||||
url: String,
|
url: Url,
|
||||||
pkg_fmt: PkgFmt,
|
pkg_fmt: PkgFmt,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,24 +27,38 @@ impl super::Fetcher for GhCrateMeta {
|
||||||
debug!("Using context: {:?}", ctx);
|
debug!("Using context: {:?}", ctx);
|
||||||
|
|
||||||
Ok(Box::new(Self {
|
Ok(Box::new(Self {
|
||||||
url: ctx.render(&data.meta.pkg_url)?,
|
url: Url::parse(&ctx.render(&data.meta.pkg_url)?)?,
|
||||||
pkg_fmt: data.meta.pkg_fmt,
|
pkg_fmt: data.meta.pkg_fmt,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check(&self) -> Result<bool, anyhow::Error> {
|
async fn check(&self) -> Result<bool, anyhow::Error> {
|
||||||
info!("Checking for package at: '{}'", self.url);
|
info!("Checking for package at: '{}'", self.url);
|
||||||
remote_exists(&self.url, Method::HEAD).await
|
remote_exists(self.url.as_str(), Method::HEAD).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch(&self, dst: &Path) -> Result<(), anyhow::Error> {
|
async fn fetch(&self, dst: &Path) -> Result<(), anyhow::Error> {
|
||||||
info!("Downloading package from: '{}'", self.url);
|
info!("Downloading package from: '{}'", self.url);
|
||||||
download(&self.url, dst).await
|
download(self.url.as_str(), dst).await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pkg_fmt(&self) -> PkgFmt {
|
fn pkg_fmt(&self) -> PkgFmt {
|
||||||
self.pkg_fmt
|
self.pkg_fmt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn source_name(&self) -> String {
|
||||||
|
if let Some(domain) = self.url.domain() {
|
||||||
|
domain.to_string()
|
||||||
|
} else if let Some(host) = self.url.host_str() {
|
||||||
|
host.to_string()
|
||||||
|
} else {
|
||||||
|
self.url.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_third_party(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Template for constructing download paths
|
/// Template for constructing download paths
|
||||||
|
|
|
@ -41,6 +41,13 @@ impl super::Fetcher for QuickInstall {
|
||||||
fn pkg_fmt(&self) -> PkgFmt {
|
fn pkg_fmt(&self) -> PkgFmt {
|
||||||
PkgFmt::Tgz
|
PkgFmt::Tgz
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn source_name(&self) -> String {
|
||||||
|
String::from("QuickInstall")
|
||||||
|
}
|
||||||
|
fn is_third_party(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl QuickInstall {
|
impl QuickInstall {
|
||||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -151,6 +151,23 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||||
anyhow::anyhow!("No viable remote package found")
|
anyhow::anyhow!("No viable remote package found")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
// Prompt user for third-party source
|
||||||
|
if fetcher.is_third_party() {
|
||||||
|
warn!(
|
||||||
|
"The package will be downloaded from third-party source {}",
|
||||||
|
fetcher.source_name()
|
||||||
|
);
|
||||||
|
if !opts.no_confirm && !confirm()? {
|
||||||
|
warn!("Installation cancelled");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
info!(
|
||||||
|
"The package will be downloaded from {}",
|
||||||
|
fetcher.source_name()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Download package
|
// Download package
|
||||||
fetcher.fetch(&pkg_path).await?;
|
fetcher.fetch(&pkg_path).await?;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue