QuickInstall support (#94)

See this issue: https://github.com/alsuren/cargo-quickinstall/issues/27

Quick Install is a hosted repo of built crates, essentially. The approach I've taken here is
a list of strategies:

1. First, we check the crate meta or default and build the URL to the repo. Once we have
   that, we perform a `HEAD` request to the URL to see if it's available.
2. If it's not, we build the URL to the quickinstall repo, and perform a `HEAD` to there.

As soon as we've got a hit, we use that. I've built it so it's extensible with more strategies.
This could be useful for #4.

This also adds a prompt before downloading from third-party sources, and logs a short
name for a source, which is easier to glance than a full URL, and includes a quick refactor
of the install/link machinery.
This commit is contained in:
Félix Saparelli 2022-02-16 14:49:07 +13:00 committed by GitHub
parent e691255650
commit 370ae05620
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 600 additions and 209 deletions

View file

@ -0,0 +1,80 @@
use std::path::Path;
use log::info;
use reqwest::Method;
use super::Data;
use crate::{download, remote_exists, PkgFmt};
const BASE_URL: &str = "https://github.com/alsuren/cargo-quickinstall/releases/download";
const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
pub struct QuickInstall {
package: String,
}
#[async_trait::async_trait]
impl super::Fetcher for QuickInstall {
async fn new(data: &Data) -> Result<Box<Self>, anyhow::Error> {
let crate_name = &data.name;
let version = &data.version;
let target = &data.target;
Ok(Box::new(Self {
package: format!("{crate_name}-{version}-{target}"),
}))
}
async fn check(&self) -> Result<bool, anyhow::Error> {
let url = self.package_url();
self.report().await?;
info!("Checking for package at: '{url}'");
remote_exists(&url, Method::HEAD).await
}
async fn fetch(&self, dst: &Path) -> Result<(), anyhow::Error> {
let url = self.package_url();
info!("Downloading package from: '{url}'");
download(&url, dst).await
}
fn pkg_fmt(&self) -> PkgFmt {
PkgFmt::Tgz
}
fn source_name(&self) -> String {
String::from("QuickInstall")
}
fn is_third_party(&self) -> bool {
true
}
}
impl QuickInstall {
fn package_url(&self) -> String {
format!(
"{base_url}/{package}/{package}.tar.gz",
base_url = BASE_URL,
package = self.package
)
}
fn stats_url(&self) -> String {
format!(
"{stats_url}/{package}.tar.gz",
stats_url = STATS_URL,
package = self.package
)
}
pub async fn report(&self) -> Result<(), anyhow::Error> {
info!("Sending installation report to quickinstall (anonymous)");
reqwest::Client::builder()
.user_agent(USER_AGENT)
.build()?
.request(Method::HEAD, &self.stats_url())
.send()
.await?;
Ok(())
}
}