generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Utkarsh Srivastava <[email protected]>
- Loading branch information
1 parent
da22eff
commit cb004b5
Showing
10 changed files
with
359 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,10 @@ | ||
check: | ||
golangci-lint run | ||
make: linux darwin windows | ||
|
||
check-clean-cache: | ||
golangci-lint cache clean | ||
darwin: | ||
nexe index.js -t darwin-x64 -o kubeopenapi-jsonschema-darwin | ||
|
||
protoc-setup: | ||
wget -P meshes https://raw.githubusercontent.com/layer5io/meshery/master/meshes/meshops.proto | ||
linux: | ||
nexe index.js -t linux-x64 -o kubeopenapi-jsonschema | ||
|
||
proto: | ||
protoc -I meshes/ meshes/meshops.proto --go_out=plugins=grpc:./meshes/ | ||
|
||
|
||
|
||
|
||
|
||
site: | ||
$(jekyll) serve --drafts --livereload | ||
|
||
build: | ||
$(jekyll) build --drafts | ||
|
||
docker: | ||
docker run --name site -d --rm -p 4000:4000 -v `pwd`:"/srv/jekyll" jekyll/jekyll:4.0.0 bash -c "bundle install; jekyll serve --drafts --livereload" | ||
windows: | ||
nexe index.js -t windows-x64 -o kubeopenapi-jsonschema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* CreateQuery generates a jsonpath based query | ||
* @param {string} query jsonpath query | ||
* @param {boolean} isKubernetes is the query to be generated for K8s CRD | ||
* @returns {string} generated query | ||
*/ | ||
function CreateQuery(query = "", isKubernetes = true) { | ||
if (isKubernetes || !query) | ||
return `$[?(@.kind=="CustomResourceDefinition")]..validation.openAPIV3Schema`; | ||
|
||
return query; | ||
} | ||
|
||
module.exports = CreateQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { dump } = require("js-yaml"); | ||
const jp = require("jsonpath"); | ||
|
||
/** | ||
* Output takes in the data that needs to be printed and | ||
* an output format | ||
* @param {*} data | ||
* @param {"json" | "yaml"} format output format | ||
*/ | ||
function Output(data, format = "json", filter = "", silent = false) { | ||
if (silent) return; | ||
|
||
data = jp.query(data, filter); | ||
if (format === "yaml") return console.log(dump(data)); | ||
if (format === "json") return console.log(JSON.stringify(data, null, 2)); | ||
} | ||
|
||
module.exports = Output; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// @ts-check | ||
const toJSONSchema = require("@openapi-contrib/openapi-schema-to-json-schema"); | ||
const yaml = require("js-yaml"); | ||
const { readFileSync, writeFileSync } = require("fs"); | ||
const { tmpdir } = require("os"); | ||
const path = require("path"); | ||
const jp = require("jsonpath"); | ||
|
||
/** | ||
* convertAllSchemasToJSONSchema takes in the OpenAPIV3 Schemas in an array | ||
* and return an array of an equivalent JSON Schema Draft 4 schemas | ||
* @param {any[]} schemas array of schemas in JSON format | ||
* @returns {any[]} JSON Schema draft 4 formatted schemas | ||
*/ | ||
function convertAllSchemasToJSONSchema(schemas) { | ||
if (Array.isArray(schemas)) | ||
return schemas.map((schema) => toJSONSchema(schema)); | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* readSchema will read schema file from the given location, it expects | ||
* the schema to be in JSON format | ||
* | ||
* readSchema will also apply the given jsonpath filter to the read schema | ||
* and will return only the filtered JSONs | ||
* @param {string} location | ||
* @param {string} query jsonpath based query | ||
* @returns {any[]} | ||
*/ | ||
function readSchema(location, query) { | ||
const data = readFileSync(location, "utf-8"); | ||
const parsed = JSON.parse(data); | ||
|
||
return jp.query(parsed, query); | ||
} | ||
|
||
/** | ||
* setupFiles takes the location of the files and convert them into json | ||
* and return the new location | ||
* @param {string} location | ||
* @param {"yaml" | "json"} type | ||
* @returns {string} location of the schema files | ||
*/ | ||
function setupFiles(location, type) { | ||
if (type === "json") return location; | ||
|
||
if (type === "yaml") { | ||
try { | ||
// Create a file name | ||
const filename = `ucnv-${Math.random().toString(36).substr(2, 5)}.json`; | ||
|
||
// Create destination path | ||
const dest = path.join(tmpdir(), filename); | ||
|
||
// Read file into memory and convert into json | ||
const doc = yaml.loadAll(readFileSync(location, "utf-8")); | ||
|
||
// Write the converted file to the disk | ||
writeFileSync(dest, JSON.stringify(doc)); | ||
|
||
return dest; | ||
} catch (error) { | ||
return ""; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* ToJSONSchema will convert he OpenAPIV3 based schema to JSONSchema Draft 4 schemas | ||
* @param {string} location location of the schemas in open api v3 format | ||
* @param {"yaml" | "json"} type encoding in which the openapi schema is present | ||
* @param {string} query jsonpath query to filter the read schemas | ||
*/ | ||
function ToJSONSchema(location, type = "yaml", query = "") { | ||
if (type !== "yaml" && type !== "json") | ||
throw Error('invalid type received: can be either "yaml" or "json"'); | ||
|
||
const source = setupFiles(location, type); | ||
|
||
const schemas = readSchema(source, query); | ||
|
||
return convertAllSchemasToJSONSchema(schemas); | ||
} | ||
|
||
module.exports = ToJSONSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// @ts-check | ||
const { program } = require("commander"); | ||
const CreateQuery = require("./helper/createQuery"); | ||
const Output = require("./helper/output"); | ||
const ToJSONSchema = require("./helper/toJSONSchema"); | ||
|
||
program | ||
.option( | ||
"-t, --type [type]", | ||
"set type of input, can be either yaml or json", | ||
"yaml" | ||
) | ||
.option("-l, --location <location>", "location of the schema") | ||
.option("-f, --filter [query]", "give a query if a OpenAPISchema is nested") | ||
.option("--kubernetes", "enable kubernetes specific filters", false) | ||
.option("-o [output-format]", "output format", "json") | ||
.option("--o-filter [output-filter]", "output filter query") | ||
.option("--silent", "skip output", false); | ||
|
||
program.parse(process.argv); | ||
|
||
const options = program.opts(); | ||
|
||
Output( | ||
ToJSONSchema( | ||
options.location, | ||
options.type, | ||
CreateQuery(options.filter, options.kubernetes) | ||
), | ||
options.o, | ||
options.oFilter, | ||
options.silent | ||
); |
Oops, something went wrong.