Init repo
This commit is contained in:
189
src/bin/scan-flake.rs
Normal file
189
src/bin/scan-flake.rs
Normal file
@@ -0,0 +1,189 @@
|
||||
use anyhow::{
|
||||
anyhow,
|
||||
Context,
|
||||
Result,
|
||||
};
|
||||
use clap::{
|
||||
Parser,
|
||||
};
|
||||
use serde::{
|
||||
Deserialize,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct FlakeMetadata {
|
||||
lastModified: i64,
|
||||
locked: FlakeLockedInfo,
|
||||
locks: FlakeLocks,
|
||||
original: FlakeSource,
|
||||
path: String,
|
||||
resolved: FlakeSource,
|
||||
revision: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct FlakeSource {
|
||||
owner: Option<String>,
|
||||
repo: Option<String>,
|
||||
r#ref: Option<String>,
|
||||
rev: Option<String>,
|
||||
r#type: String,
|
||||
url: Option<String>,
|
||||
}
|
||||
|
||||
fn assemble_flake_git_uri(flake_source: &FlakeSource) -> Result<String> {
|
||||
let mut out = String::new();
|
||||
out.push_str("git+");
|
||||
out.push_str(&flake_source.url.clone()
|
||||
.context("Flake git uri does not contain an url")?);
|
||||
|
||||
let mut query: Vec<String> = Vec::new();
|
||||
|
||||
if let Some(r#ref) = &flake_source.r#ref {
|
||||
query.push(format!("ref={}", r#ref));
|
||||
}
|
||||
|
||||
if let Some(rev) = &flake_source.rev {
|
||||
query.push(format!("rev={}", rev));
|
||||
}
|
||||
|
||||
if query.len() > 0 {
|
||||
out.push_str("?");
|
||||
out.push_str(&query.join("&"));
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn assemble_flake_github_uri(flake_source: &FlakeSource) -> Result<String> {
|
||||
let mut out = String::new();
|
||||
out.push_str("github:");
|
||||
out.push_str(&flake_source.owner.clone()
|
||||
.context("Flake github uri does not contain an owner")?);
|
||||
out.push_str("/");
|
||||
out.push_str(&flake_source.repo.clone()
|
||||
.context("Flake github uri does not contain an repo")?);
|
||||
|
||||
if let Some(rev) = &flake_source.rev {
|
||||
out.push_str("/");
|
||||
out.push_str(&rev);
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn assemble_flake_tarball_uri(flake_source: &FlakeSource) -> Result<String> {
|
||||
let mut out = String::new();
|
||||
out.push_str(&flake_source.url.clone()
|
||||
.context("Flake tarball uri does not contain an url")?);
|
||||
|
||||
if let Some(rev) = &flake_source.rev {
|
||||
out.push_str("?rev=");
|
||||
out.push_str(rev);
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
trait FlakeUri {
|
||||
fn flake_uri(&self) -> Result<String>;
|
||||
}
|
||||
|
||||
impl FlakeUri for FlakeSource {
|
||||
fn flake_uri(&self) -> Result<String> {
|
||||
match self.r#type.as_str() {
|
||||
"git" => assemble_flake_git_uri(self),
|
||||
"github" => assemble_flake_github_uri(self),
|
||||
"tarball" => assemble_flake_tarball_uri(self),
|
||||
other => Err(anyhow!("Unsupported flake uri type {}", other)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct FlakeLockedInfo {
|
||||
lastModified: i64,
|
||||
narHash: String,
|
||||
owner: Option<String>,
|
||||
repo: Option<String>,
|
||||
r#ref: Option<String>,
|
||||
rev: Option<String>,
|
||||
r#type: String,
|
||||
url: Option<String>,
|
||||
}
|
||||
|
||||
impl FlakeUri for FlakeLockedInfo {
|
||||
fn flake_uri(&self) -> Result<String> {
|
||||
Into::<FlakeSource>::into(self).flake_uri()
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<FlakeSource> for &FlakeLockedInfo {
|
||||
fn into(self) -> FlakeSource {
|
||||
FlakeSource {
|
||||
owner: self.owner.clone(),
|
||||
repo: self.repo.clone(),
|
||||
r#ref: self.r#ref.clone(),
|
||||
rev: self.rev.clone(),
|
||||
r#type: self.r#type.clone(),
|
||||
url: self.url.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct FlakeLocks {
|
||||
nodes: HashMap<String, FlakeLocksNode>,
|
||||
root: String,
|
||||
version: u64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(untagged)]
|
||||
enum FlakeLocksNodeInputs {
|
||||
String(String),
|
||||
Array(Vec<String>),
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct FlakeLocksNode {
|
||||
inputs: Option<HashMap<String, FlakeLocksNodeInputs>>,
|
||||
locked: Option<FlakeLockedInfo>,
|
||||
original: Option<FlakeSource>,
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Cli {
|
||||
flake_uri: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let flake_metadata_raw = Command::new("nix")
|
||||
.arg("flake")
|
||||
.arg("metadata")
|
||||
.arg("--json")
|
||||
.arg(cli.flake_uri)
|
||||
.output()
|
||||
.context("Failed to fetch flake metadata")?;
|
||||
|
||||
let flake_metadata: FlakeMetadata = serde_json::from_slice(&flake_metadata_raw.stdout)
|
||||
.context("Failed to parse flake metadata")?;
|
||||
|
||||
println!("{}", flake_metadata.original.flake_uri()?);
|
||||
|
||||
for (input_name, input_data) in &flake_metadata.locks.nodes {
|
||||
if let Some(original_flake_source) = &input_data.original {
|
||||
println!("{}", original_flake_source.flake_uri()?);
|
||||
}
|
||||
if let Some(locked_flake_source) = &input_data.locked {
|
||||
println!("{}", locked_flake_source.flake_uri()?);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user