mirror of
https://github.com/nix-community/nurl.git
synced 2024-09-11 18:39:19 +03:00
avoid cloning if possible
This commit is contained in:
parent
75f968b42b
commit
29286f7375
@ -3,19 +3,19 @@ use crate::{
|
||||
simple::{SimpleFetcher, SimpleUrlFetcher},
|
||||
};
|
||||
|
||||
pub struct FetchFromGitea(pub String);
|
||||
impl_fetcher!(FetchFromGitea);
|
||||
pub struct FetchFromGitea<'a>(pub &'a str);
|
||||
impl_fetcher!(FetchFromGitea<'a>);
|
||||
|
||||
impl<'a> SimpleFetcher<'a> for FetchFromGitea {
|
||||
impl<'a> SimpleFetcher<'a> for FetchFromGitea<'a> {
|
||||
const KEYS: [&'static str; 2] = ["owner", "repo"];
|
||||
const NAME: &'static str = "fetchFromGitea";
|
||||
|
||||
fn host(&'a self) -> Option<&'a str> {
|
||||
Some(&self.0)
|
||||
Some(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> SimpleUrlFetcher<'a> for FetchFromGitea {
|
||||
impl<'a> SimpleUrlFetcher<'a> for FetchFromGitea<'a> {
|
||||
fn get_url(&self, [owner, repo]: [&str; 2], rev: &str) -> String {
|
||||
format!("https://{}/{owner}/{repo}/archive/{rev}.tar.gz", self.0)
|
||||
}
|
||||
|
@ -3,19 +3,19 @@ use crate::{
|
||||
simple::{SimpleFetcher, SimpleFlakeFetcher},
|
||||
};
|
||||
|
||||
pub struct FetchFromGitHub(pub Option<String>);
|
||||
impl_fetcher!(FetchFromGitHub);
|
||||
pub struct FetchFromGitHub<'a>(pub Option<&'a str>);
|
||||
impl_fetcher!(FetchFromGitHub<'a>);
|
||||
|
||||
impl<'a> SimpleFetcher<'a> for FetchFromGitHub {
|
||||
impl<'a> SimpleFetcher<'a> for FetchFromGitHub<'a> {
|
||||
const HOST_KEY: &'static str = "githubBase";
|
||||
const KEYS: [&'static str; 2] = ["owner", "repo"];
|
||||
const NAME: &'static str = "fetchFromGitHub";
|
||||
|
||||
fn host(&'a self) -> Option<&'a str> {
|
||||
self.0.as_deref()
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> SimpleFlakeFetcher<'a> for FetchFromGitHub {
|
||||
impl<'a> SimpleFlakeFetcher<'a> for FetchFromGitHub<'a> {
|
||||
const FLAKE_TYPE: &'static str = "github";
|
||||
}
|
||||
|
@ -3,18 +3,18 @@ use crate::{
|
||||
simple::{SimpleFetcher, SimpleFlakeFetcher},
|
||||
};
|
||||
|
||||
pub struct FetchFromGitLab(pub Option<String>);
|
||||
impl_fetcher!(FetchFromGitLab);
|
||||
pub struct FetchFromGitLab<'a>(pub Option<&'a str>);
|
||||
impl_fetcher!(FetchFromGitLab<'a>);
|
||||
|
||||
impl<'a> SimpleFetcher<'a> for FetchFromGitLab {
|
||||
impl<'a> SimpleFetcher<'a> for FetchFromGitLab<'a> {
|
||||
const KEYS: [&'static str; 2] = ["owner", "repo"];
|
||||
const NAME: &'static str = "fetchFromGitLab";
|
||||
|
||||
fn host(&'a self) -> Option<&'a str> {
|
||||
self.0.as_deref()
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> SimpleFlakeFetcher<'a> for FetchFromGitLab {
|
||||
impl<'a> SimpleFlakeFetcher<'a> for FetchFromGitLab<'a> {
|
||||
const FLAKE_TYPE: &'static str = "gitlab";
|
||||
}
|
||||
|
@ -42,21 +42,21 @@ pub trait Fetcher {
|
||||
}
|
||||
|
||||
#[enum_dispatch(Fetcher)]
|
||||
pub enum FetcherDispatch {
|
||||
pub enum FetcherDispatch<'a> {
|
||||
FetchFromBitBucket(FetchFromBitBucket),
|
||||
FetchFromGitHub(FetchFromGitHub),
|
||||
FetchFromGitLab(FetchFromGitLab),
|
||||
FetchFromGitea(FetchFromGitea),
|
||||
FetchFromGitHub(FetchFromGitHub<'a>),
|
||||
FetchFromGitLab(FetchFromGitLab<'a>),
|
||||
FetchFromGitea(FetchFromGitea<'a>),
|
||||
FetchFromRepoOrCz(FetchFromRepoOrCz),
|
||||
FetchFromSourcehut(FetchFromSourcehut),
|
||||
FetchFromSourcehut(FetchFromSourcehut<'a>),
|
||||
Fetchgit(Fetchgit),
|
||||
Fetchhg(Fetchhg),
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_fetcher {
|
||||
($t:ty) => {
|
||||
impl $crate::fetcher::Fetcher for $t {
|
||||
($t:ident $($tt:tt)*) => {
|
||||
impl $($tt)* $crate::fetcher::Fetcher for $t $($tt)* {
|
||||
fn fetch_nix(
|
||||
&self,
|
||||
out: &mut impl ::std::io::Write,
|
||||
|
@ -3,18 +3,18 @@ use crate::{
|
||||
simple::{SimpleFetcher, SimpleFlakeFetcher},
|
||||
};
|
||||
|
||||
pub struct FetchFromSourcehut(pub Option<String>);
|
||||
impl_fetcher!(FetchFromSourcehut);
|
||||
pub struct FetchFromSourcehut<'a>(pub Option<&'a str>);
|
||||
impl_fetcher!(FetchFromSourcehut<'a>);
|
||||
|
||||
impl<'a> SimpleFetcher<'a> for FetchFromSourcehut {
|
||||
impl<'a> SimpleFetcher<'a> for FetchFromSourcehut<'a> {
|
||||
const KEYS: [&'static str; 2] = ["owner", "repo"];
|
||||
const NAME: &'static str = "fetchFromSourcehut";
|
||||
|
||||
fn host(&'a self) -> Option<&'a str> {
|
||||
self.0.as_deref()
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> SimpleFlakeFetcher<'a> for FetchFromSourcehut {
|
||||
impl<'a> SimpleFlakeFetcher<'a> for FetchFromSourcehut<'a> {
|
||||
const FLAKE_TYPE: &'static str = "sourcehut";
|
||||
}
|
||||
|
36
src/main.rs
36
src/main.rs
@ -8,7 +8,6 @@ mod simple;
|
||||
use anyhow::{bail, Result};
|
||||
use clap::{Parser, ValueEnum};
|
||||
use itertools::Itertools;
|
||||
use url::Host;
|
||||
|
||||
use crate::{
|
||||
cli::{FetcherFunction, Opts},
|
||||
@ -36,51 +35,42 @@ fn main() -> Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let fetcher: FetcherDispatch = match (opts.fetcher, opts.url.host()) {
|
||||
(None | Some(FetcherFunction::FetchFromBitBucket), Some(Host::Domain("bitbucket.org"))) => {
|
||||
let fetcher: FetcherDispatch = match (opts.fetcher, opts.url.host_str()) {
|
||||
(None | Some(FetcherFunction::FetchFromBitBucket), Some("bitbucket.org")) => {
|
||||
FetchFromBitBucket.into()
|
||||
}
|
||||
(Some(FetcherFunction::FetchFromBitBucket), _) => {
|
||||
bail!("fetchFromBitBucket only supports bitbucket.org");
|
||||
}
|
||||
|
||||
(None | Some(FetcherFunction::FetchFromGitHub), Some(Host::Domain("github.com"))) => {
|
||||
(None | Some(FetcherFunction::FetchFromGitHub), Some("github.com")) => {
|
||||
FetchFromGitHub(None).into()
|
||||
}
|
||||
(Some(FetcherFunction::FetchFromGitHub), Some(host)) => {
|
||||
FetchFromGitHub(Some(host.to_string())).into()
|
||||
}
|
||||
(Some(FetcherFunction::FetchFromGitHub), Some(host)) => FetchFromGitHub(Some(host)).into(),
|
||||
|
||||
(None | Some(FetcherFunction::FetchFromGitLab), Some(Host::Domain("gitlab.com"))) => {
|
||||
(None | Some(FetcherFunction::FetchFromGitLab), Some("gitlab.com")) => {
|
||||
FetchFromGitLab(None).into()
|
||||
}
|
||||
(None, Some(Host::Domain(host))) if host.starts_with("gitlab.") => {
|
||||
FetchFromGitLab(Some(host.into())).into()
|
||||
}
|
||||
(Some(FetcherFunction::FetchFromGitLab), Some(host)) => {
|
||||
FetchFromGitLab(Some(host.to_string())).into()
|
||||
}
|
||||
(None, Some(host)) if host.starts_with("gitlab.") => FetchFromGitLab(Some(host)).into(),
|
||||
(Some(FetcherFunction::FetchFromGitLab), Some(host)) => FetchFromGitLab(Some(host)).into(),
|
||||
|
||||
(
|
||||
None | Some(FetcherFunction::FetchFromGitea),
|
||||
Some(Host::Domain(host @ "codeberg.org")),
|
||||
) => FetchFromGitea(host.into()).into(),
|
||||
(Some(FetcherFunction::FetchFromGitea), Some(host)) => {
|
||||
FetchFromGitea(host.to_string()).into()
|
||||
(None | Some(FetcherFunction::FetchFromGitea), Some(host @ "codeberg.org")) => {
|
||||
FetchFromGitea(host).into()
|
||||
}
|
||||
(Some(FetcherFunction::FetchFromGitea), Some(host)) => FetchFromGitea(host).into(),
|
||||
|
||||
(None | Some(FetcherFunction::FetchFromRepoOrCz), Some(Host::Domain("repo.or.cz"))) => {
|
||||
(None | Some(FetcherFunction::FetchFromRepoOrCz), Some("repo.or.cz")) => {
|
||||
FetchFromRepoOrCz.into()
|
||||
}
|
||||
(Some(FetcherFunction::FetchFromRepoOrCz), _) => {
|
||||
bail!("fetchFromRepoOrCz only supports repo.or.cz");
|
||||
}
|
||||
|
||||
(None | Some(FetcherFunction::FetchFromSourcehut), Some(Host::Domain("git.sr.ht"))) => {
|
||||
(None | Some(FetcherFunction::FetchFromSourcehut), Some("git.sr.ht")) => {
|
||||
FetchFromSourcehut(None).into()
|
||||
}
|
||||
(Some(FetcherFunction::FetchFromSourcehut), Some(host)) => {
|
||||
FetchFromSourcehut(Some(host.to_string())).into()
|
||||
FetchFromSourcehut(Some(host)).into()
|
||||
}
|
||||
|
||||
(
|
||||
|
Loading…
Reference in New Issue
Block a user