Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clap to provide genealogos with file input #11

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
name = "genealogos"
version = "0.1.0"
edition = "2021"
authors = ["Tweag I/O"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.14", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde-cyclonedx = "0.8"
serde_json = "1.0"
uuid = { version = "1.6", features = ["v4"] }
33 changes: 19 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
mod model;
mod nixtract;

use std::fs::File;
use std::io::{self, BufRead};
use std::path::PathBuf;

use clap::Parser;
use serde_cyclonedx::cyclonedx::v_1_5 as cyclonedx;

use crate::model::Model;
use crate::nixtract::Nixtract;

mod model;
mod nixtract;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
file: PathBuf,
}

fn main() -> Result<(), io::Error> {
let mut input_reader = std::io::stdin().lock();
let args = Args::parse();

let file = File::open(args.file)?;

let mut entries = vec![];

loop {
let mut buffer = String::new();
match input_reader.read_line(&mut buffer) {
Ok(0) => break,
Ok(_n) => {
let entry: nixtract::NixtractEntry = serde_json::from_str(&buffer.trim()).unwrap();
entries.push(entry);
}
Err(_) => todo!(),
}
for line in io::BufReader::new(file).lines().flatten() {
let entry: nixtract::NixtractEntry = serde_json::from_str(line.trim()).unwrap();
entries.push(entry);
}
let nixtract: Nixtract = Nixtract { entries };

Expand Down
12 changes: 6 additions & 6 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ pub(crate) struct ModelDependency {
pub(crate) depends_on: Vec<String>,
}

impl Into<String> for ModelType {
fn into(self) -> String {
match self {
impl From<ModelType> for String {
fn from(val: ModelType) -> Self {
match val {
ModelType::Application => "application".to_owned(),
}
}
}

impl Into<String> for ModelExternalReferenceType {
fn into(self) -> String {
match self {
impl From<ModelExternalReferenceType> for String {
fn from(val: ModelExternalReferenceType) -> Self {
match val {
ModelExternalReferenceType::Website => "website".to_owned(),
}
}
Expand Down
38 changes: 26 additions & 12 deletions src/nixtract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//! - Parsing the incoming output of Nixtract
//! - Converting that input into the internal representation of Genealogos

// In this module, one might see that we do deserialize unused fields. This is
// to ensure we stay complient with nixtract output.

use serde::Deserialize;

use crate::model::{
Expand All @@ -17,34 +20,43 @@ pub(crate) struct Nixtract {

#[derive(Deserialize, Debug)]
pub(crate) struct NixtractEntry {
pub(crate) attribute_path: String,
pub(crate) derivation_path: String,
#[serde(rename(deserialize = "attribute_path"))]
pub(crate) _attribute_path: String,
#[serde(rename(deserialize = "derivation_path"))]
pub(crate) _derivation_path: String,
pub(crate) output_path: String,
pub(crate) outputs: Vec<NixtractOutput>,
pub(crate) name: String,
#[serde(rename(deserialize = "outputs"))]
pub(crate) _outputs: Vec<NixtractOutput>,
#[serde(rename(deserialize = "name"))]
pub(crate) _name: String,
pub(crate) parsed_name: NixtractParsedName,
pub(crate) nixpkgs_metadata: NixtractNixpkgsMetadata,
pub(crate) build_inputs: Vec<NixtractBuiltInput>,
}

#[derive(Deserialize, Debug)]
pub(crate) struct NixtractOutput {
pub(crate) name: String,
pub(crate) output_path: String,
#[serde(rename(deserialize = "name"))]
pub(crate) _name: String,
#[serde(rename(deserialize = "output_path"))]
pub(crate) _output_path: String,
}

#[derive(Deserialize, Debug)]
pub(crate) struct NixtractParsedName {
pub(crate) name: String,
pub(crate) version: String,
#[serde(rename(deserialize = "version"))]
pub(crate) _version: String,
}

#[derive(Deserialize, Debug)]
pub(crate) struct NixtractNixpkgsMetadata {
pub(crate) description: String,
pub(crate) pname: String,
#[serde(rename(deserialize = "pname"))]
pub(crate) _pname: String,
pub(crate) version: String,
pub(crate) broken: bool,
#[serde(rename(deserialize = "broken"))]
pub(crate) _broken: bool,
pub(crate) homepage: String,
pub(crate) licenses: Option<Vec<NixtractLicense>>,
}
Expand All @@ -58,8 +70,10 @@ pub(crate) struct NixtractLicense {

#[derive(Deserialize, Debug)]
pub(crate) struct NixtractBuiltInput {
pub(crate) attribute_path: String,
pub(crate) build_input_type: String,
#[serde(rename(deserialize = "attribute_path"))]
pub(crate) _attribute_path: String,
#[serde(rename(deserialize = "build_input_type"))]
pub(crate) _build_input_type: String,
pub(crate) output_path: Option<String>,
}

Expand All @@ -83,7 +97,7 @@ impl From<Nixtract> for Model {
.nixpkgs_metadata
.licenses
.as_ref()
.map(|v| v.into_iter().map(Into::into).collect());
.map(|v| v.iter().map(Into::into).collect());

ModelComponent {
r#type: ModelType::Application,
Expand Down
Loading