Skip to content

Commit

Permalink
Add option to output json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Feb 19, 2024
1 parent aac7dfb commit c921551
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
63 changes: 58 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ flume = "0.11.0"
log = "0.4.20"
num_cpus = "1.16.0"
rayon = "1.8.1"
schemars = "0.8.16"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
shell-escape = "0.1.5"
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ struct Args {
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,

/// Output the json schema
#[arg(long, default_value_t = false)]
output_schema: bool,

/// Write the output to a file instead of stdout or explicitly use `-` for stdout
#[arg()]
output_path: Option<String>,
Expand All @@ -59,7 +63,14 @@ fn main() -> Result<(), Box<dyn Error>> {
.filter_level(opts.verbose.log_level_filter())
.init();

main_with_args(opts)
// If schema is requested, print the schema and return
if opts.output_schema {
let schema = schemars::schema_for!(nixtract::DerivationDescription);
println!("{}", serde_json::to_string_pretty(&schema)?);
Ok(())
} else {
main_with_args(opts)
}
}

fn main_with_args(opts: Args) -> Result<(), Box<dyn Error>> {
Expand Down
15 changes: 8 additions & 7 deletions src/nix/describe_derivation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{collections::HashMap, process::Command};

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::lib::Lib;
use crate::error::{Error, Result};

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, JsonSchema)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct DerivationDescription {
pub attribute_path: String,
Expand All @@ -19,21 +20,21 @@ pub struct DerivationDescription {
pub build_inputs: Vec<BuiltInput>,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, JsonSchema)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct Output {
pub name: String,
pub output_path: Option<String>,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, JsonSchema)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct ParsedName {
pub name: String,
pub version: String,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, JsonSchema)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct NixpkgsMetadata {
pub description: String,
Expand All @@ -44,23 +45,23 @@ pub struct NixpkgsMetadata {
pub licenses: Option<Vec<License>>,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, JsonSchema)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct Source {
pub git_repo_url: String,
// Revision or tag of the git repo
pub rev: String,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, JsonSchema)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct License {
// Not all licenses in nixpkgs have an associated spdx id
pub spdx_id: Option<String>,
pub full_name: String,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, JsonSchema)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct BuiltInput {
pub attribute_path: String,
Expand Down

0 comments on commit c921551

Please sign in to comment.