Add source name and prompt for third-party sources

This commit is contained in:
Félix Saparelli 2022-02-16 11:41:00 +13:00
parent d3f8802514
commit a92336689a
No known key found for this signature in database
GPG key ID: B948C4BAE44FC474
6 changed files with 51 additions and 4 deletions

1
Cargo.lock generated
View file

@ -146,6 +146,7 @@ dependencies = [
"tempdir", "tempdir",
"tinytemplate", "tinytemplate",
"tokio", "tokio",
"url",
"xz2", "xz2",
"zip", "zip",
] ]

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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 {

View file

@ -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?;