mirror of
https://github.com/nix-community/nurl.git
synced 2024-10-26 12:04:01 +03:00
add --parse
This commit is contained in:
parent
30eb520ee2
commit
708ca82d5f
@ -33,6 +33,12 @@ pub struct Opts {
|
|||||||
#[arg(short, long, group = "format")]
|
#[arg(short, long, group = "format")]
|
||||||
pub json: bool,
|
pub json: bool,
|
||||||
|
|
||||||
|
/// parse the url without fetching the hash, similar to --json
|
||||||
|
///
|
||||||
|
/// note that --arg(-str) and --overwrite(-str) will be ignored silently
|
||||||
|
#[arg(short, long, group = "format")]
|
||||||
|
pub parse: bool,
|
||||||
|
|
||||||
/// additional arguments to pass to the fetcher
|
/// additional arguments to pass to the fetcher
|
||||||
#[arg(short, long = "arg", num_args = 2, value_names = ["NAME", "EXPR"])]
|
#[arg(short, long = "arg", num_args = 2, value_names = ["NAME", "EXPR"])]
|
||||||
pub args: Vec<String>,
|
pub args: Vec<String>,
|
||||||
|
@ -59,6 +59,13 @@ pub trait Fetcher<'a> {
|
|||||||
overwrites: Vec<(String, String)>,
|
overwrites: Vec<(String, String)>,
|
||||||
overwrites_str: Vec<(String, String)>,
|
overwrites_str: Vec<(String, String)>,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
|
|
||||||
|
fn to_json(
|
||||||
|
&'a self,
|
||||||
|
out: &mut impl ::std::io::Write,
|
||||||
|
url: &'a ::url::Url,
|
||||||
|
rev: Option<String>,
|
||||||
|
) -> ::anyhow::Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[enum_dispatch(Fetcher)]
|
#[enum_dispatch(Fetcher)]
|
||||||
@ -164,6 +171,42 @@ macro_rules! impl_fetcher {
|
|||||||
overwrites_str,
|
overwrites_str,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn to_json(
|
||||||
|
&'a self,
|
||||||
|
out: &mut impl ::std::io::Write,
|
||||||
|
url: &'a ::url::Url,
|
||||||
|
rev: Option<String>,
|
||||||
|
) -> ::anyhow::Result<()> {
|
||||||
|
use anyhow::Context;
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
let values = self
|
||||||
|
.get_values(url)
|
||||||
|
.with_context(|| format!("failed to parse {url}"))?;
|
||||||
|
|
||||||
|
let mut fetcher_args = Value::from_iter(Self::KEYS.into_iter().zip(values));
|
||||||
|
|
||||||
|
if let Some(host) = self.host() {
|
||||||
|
fetcher_args[Self::HOST_KEY] = json!(host);
|
||||||
|
}
|
||||||
|
if let Some(group) = self.group() {
|
||||||
|
fetcher_args["group"] = json!(group);
|
||||||
|
}
|
||||||
|
if let Some(rev) = rev {
|
||||||
|
fetcher_args["rev"] = json!(rev);
|
||||||
|
}
|
||||||
|
|
||||||
|
serde_json::to_writer(
|
||||||
|
out,
|
||||||
|
&json!({
|
||||||
|
"fetcher": Self::NAME,
|
||||||
|
"args": fetcher_args,
|
||||||
|
}),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -152,6 +152,8 @@ fn main() -> Result<()> {
|
|||||||
opts.overwrites.into_iter().tuples().collect(),
|
opts.overwrites.into_iter().tuples().collect(),
|
||||||
opts.overwrites_str.into_iter().tuples().collect(),
|
opts.overwrites_str.into_iter().tuples().collect(),
|
||||||
)
|
)
|
||||||
|
} else if opts.parse {
|
||||||
|
fetcher.to_json(out, &opts.url, opts.rev)
|
||||||
} else {
|
} else {
|
||||||
let mut overwrites: FxHashMap<_, _> = opts.overwrites.into_iter().tuples().collect();
|
let mut overwrites: FxHashMap<_, _> = opts.overwrites.into_iter().tuples().collect();
|
||||||
|
|
||||||
|
1
tests/cmd/parse.stdout
Normal file
1
tests/cmd/parse.stdout
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"args":{"owner":"nix-community","repo":"nurl","rev":"v0.3.0"},"fetcher":"fetchFromGitHub"}
|
1
tests/cmd/parse.toml
Normal file
1
tests/cmd/parse.toml
Normal file
@ -0,0 +1 @@
|
|||||||
|
args = ["https://github.com/nix-community/nurl", "v0.3.0", "--parse"]
|
@ -23,7 +23,7 @@ fn verify_outputs() {
|
|||||||
.strip_suffix(".stdout")
|
.strip_suffix(".stdout")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
if matches!(name, "hash" | "json") {
|
if matches!(name, "hash" | "json" | "parse") {
|
||||||
eprintln!("skipping {}", path.display());
|
eprintln!("skipping {}", path.display());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user