mirror of
https://github.com/nix-community/nurl.git
synced 2024-10-26 12:04:01 +03:00
fetchHex support
This commit is contained in:
parent
d102bc36e6
commit
4c227cac30
@ -98,6 +98,7 @@ pub enum FetcherFunction {
|
||||
FetchFromGitiles,
|
||||
FetchFromRepoOrCz,
|
||||
FetchFromSourcehut,
|
||||
FetchHex,
|
||||
Fetchgit,
|
||||
Fetchhg,
|
||||
Fetchsvn,
|
||||
|
28
src/fetcher/hex.rs
Normal file
28
src/fetcher/hex.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
impl_fetcher,
|
||||
simple::{SimpleFetcher, SimpleUrlFetcher},
|
||||
};
|
||||
|
||||
pub struct FetchHex;
|
||||
impl_fetcher!(FetchHex);
|
||||
|
||||
impl<'a> SimpleFetcher<'a, 1> for FetchHex {
|
||||
const HASH_KEY: &'static str = "sha256";
|
||||
const KEYS: [&'static str; 1] = ["pkg"];
|
||||
const NAME: &'static str = "fetchHex";
|
||||
const REV_KEY: &'static str = "version";
|
||||
|
||||
fn get_values(&self, url: &'a Url) -> Option<[&'a str; 1]> {
|
||||
Some([url.path_segments()?.nth(1)?])
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> SimpleUrlFetcher<'a, 1> for FetchHex {
|
||||
const UNPACK: bool = false;
|
||||
|
||||
fn get_url(&self, [pkg]: &[&str; 1], version: &str) -> String {
|
||||
format!("https://repo.hex.pm/tarballs/{pkg}-{version}.tar")
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ mod gitea;
|
||||
mod github;
|
||||
mod gitiles;
|
||||
mod gitlab;
|
||||
mod hex;
|
||||
mod hg;
|
||||
mod repo_or_cz;
|
||||
mod sourcehut;
|
||||
@ -17,6 +18,7 @@ pub use gitea::FetchFromGitea;
|
||||
pub use github::FetchFromGitHub;
|
||||
pub use gitiles::FetchFromGitiles;
|
||||
pub use gitlab::FetchFromGitLab;
|
||||
pub use hex::FetchHex;
|
||||
pub use hg::Fetchhg;
|
||||
pub use repo_or_cz::FetchFromRepoOrCz;
|
||||
pub use sourcehut::FetchFromSourcehut;
|
||||
@ -80,6 +82,7 @@ pub enum FetcherDispatch<'a> {
|
||||
FetchFromGitiles(FetchFromGitiles),
|
||||
FetchFromRepoOrCz(FetchFromRepoOrCz),
|
||||
FetchFromSourcehut(FetchFromSourcehut<'a>),
|
||||
FetchHex(FetchHex),
|
||||
Fetchgit(Fetchgit),
|
||||
Fetchhg(Fetchhg),
|
||||
Fetchsvn(Fetchsvn),
|
||||
|
@ -14,8 +14,8 @@ use crate::{
|
||||
cli::{FetcherFunction, Opts},
|
||||
fetcher::{
|
||||
FetchCrate, FetchFromBitbucket, FetchFromGitHub, FetchFromGitLab, FetchFromGitea,
|
||||
FetchFromGitiles, FetchFromRepoOrCz, FetchFromSourcehut, Fetcher, FetcherDispatch,
|
||||
Fetchgit, Fetchhg, Fetchsvn,
|
||||
FetchFromGitiles, FetchFromRepoOrCz, FetchFromSourcehut, FetchHex, Fetcher,
|
||||
FetcherDispatch, Fetchgit, Fetchhg, Fetchsvn,
|
||||
},
|
||||
};
|
||||
|
||||
@ -126,6 +126,11 @@ fn main() -> Result<()> {
|
||||
bail!("{fetcher:?} does not support URLs without a host");
|
||||
}
|
||||
|
||||
(None | Some(FetcherFunction::FetchHex), Some("hex.pm"), _) => FetchHex.into(),
|
||||
(Some(FetcherFunction::FetchHex), ..) => {
|
||||
bail!("fetchHex only supports hex.pm");
|
||||
}
|
||||
|
||||
(None | Some(FetcherFunction::Fetchgit), _, "git") => Fetchgit(GitScheme::Yes).into(),
|
||||
(None | Some(FetcherFunction::Fetchgit), _, scheme) if scheme.starts_with("git+") => {
|
||||
Fetchgit(GitScheme::Plus).into()
|
||||
|
@ -51,14 +51,17 @@ pub fn flake_prefetch(flake_ref: String) -> Result<String> {
|
||||
.hash)
|
||||
}
|
||||
|
||||
pub fn url_prefetch(url: String) -> Result<String> {
|
||||
info!("$ nix-prefetch-url --unpack {url}");
|
||||
let hash = String::from_utf8(
|
||||
Command::new("nix-prefetch-url")
|
||||
.arg("--unpack")
|
||||
.arg(url)
|
||||
.get_stdout()?,
|
||||
)?;
|
||||
pub fn url_prefetch(url: String, unpack: bool) -> Result<String> {
|
||||
let mut cmd = Command::new("nix-prefetch-url");
|
||||
if unpack {
|
||||
cmd.arg("--unpack");
|
||||
info!("$ nix-prefetch-url --unpack {url}");
|
||||
} else {
|
||||
info!("$ nix-prefetch-url {url}");
|
||||
}
|
||||
cmd.arg(url);
|
||||
|
||||
let hash = String::from_utf8(cmd.get_stdout()?)?;
|
||||
let hash = hash.trim_end();
|
||||
|
||||
info!("$ nix hash to-sri --experimental-features nix-command --type sha256 {hash}");
|
||||
|
@ -237,6 +237,8 @@ pub trait SimpleFlakeFetcher<'a, const N: usize>: SimpleFetcher<'a, N> {
|
||||
}
|
||||
|
||||
pub trait SimpleUrlFetcher<'a, const N: usize>: SimpleFetcher<'a, N> {
|
||||
const UNPACK: bool = true;
|
||||
|
||||
fn get_url(&self, values: &[&str; N], rev: &str) -> String;
|
||||
|
||||
fn fetch(
|
||||
@ -247,7 +249,7 @@ pub trait SimpleUrlFetcher<'a, const N: usize>: SimpleFetcher<'a, N> {
|
||||
args_str: &[(String, String)],
|
||||
) -> Result<String> {
|
||||
if args.is_empty() && args_str.is_empty() {
|
||||
url_prefetch(self.get_url(values, rev))
|
||||
url_prefetch(self.get_url(values, rev), Self::UNPACK)
|
||||
} else {
|
||||
self.fetch_fod(values, rev, args, args_str)
|
||||
}
|
||||
|
5
tests/cmd/fetcher/hex.stdout
Normal file
5
tests/cmd/fetcher/hex.stdout
Normal file
@ -0,0 +1,5 @@
|
||||
fetchHex {
|
||||
pkg = "phoenix";
|
||||
version = "1.6.0";
|
||||
sha256 = "sha256-Uv/dMfLa6zmbLh61fUaPmaGtbu5djqGdI1NJLwbJ/JY=";
|
||||
}
|
1
tests/cmd/fetcher/hex.toml
Normal file
1
tests/cmd/fetcher/hex.toml
Normal file
@ -0,0 +1 @@
|
||||
args = ["https://hex.pm/packages/phoenix", "1.6.0"]
|
Loading…
Reference in New Issue
Block a user