From dd5db71b1a76d141d91dd8cd26f9759a701738e2 Mon Sep 17 00:00:00 2001 From: Maxime Brunet Date: Tue, 29 Dec 2020 09:49:20 -0800 Subject: [PATCH] scripts: Support for alternative filename format --- Readme.md | 2 ++ scripts/openapi2jsonschema.py | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index a5c7078..98e027e 100644 --- a/Readme.md +++ b/Readme.md @@ -179,6 +179,8 @@ $ ./scripts/openapi2jsonschema.py https://raw.githubusercontent.com/aws/amazon-s JSON schema written to trainingjob_v1.json ``` +The `FILENAME_FORMAT` environment variable can be used to change the output file name (Available variables: `kind`, `group`, `version`) (Default: `{kind}_{version}`). + ### Speed comparison with Kubeval Running on a pretty large kubeconfigs setup, on a laptop with 4 cores: diff --git a/scripts/openapi2jsonschema.py b/scripts/openapi2jsonschema.py index f80abfa..c0beb21 100755 --- a/scripts/openapi2jsonschema.py +++ b/scripts/openapi2jsonschema.py @@ -104,10 +104,15 @@ for crdFile in sys.argv[1:]: if y["kind"] != "CustomResourceDefinition": continue + filename_format = os.getenv("FILENAME_FORMAT", "{kind}_{version}") filename = "" schemaJSON = "" if "spec" in y and "validation" in y["spec"] and "openAPIV3Schema" in y["spec"]["validation"]: - filename = y["spec"]["names"]["kind"].lower()+"_"+y["spec"]["version"].lower()+".json" + filename = filename_format.format( + kind=y["spec"]["names"]["kind"], + group=y["spec"]["group"].split(".")[0], + version=y["spec"]["version"], + ).lower() + ".json" schema = y["spec"]["validation"]["openAPIV3Schema"] schema = additional_properties(schema) @@ -116,7 +121,11 @@ for crdFile in sys.argv[1:]: elif "spec" in y and "versions" in y["spec"]: for version in y["spec"]["versions"]: if "schema" in version and "openAPIV3Schema" in version["schema"]: - filename = y["spec"]["names"]["kind"].lower()+"_"+version["name"].lower()+".json" + filename = filename_format.format( + kind=y["spec"]["names"]["kind"], + group=y["spec"]["group"].split(".")[0], + version=version["name"], + ).lower() + ".json" schema = version["schema"]["openAPIV3Schema"] schema = additional_properties(schema)