diff --git a/docgen/json/gen.sh b/docgen/json/gen.sh index 19149d5b..e853e22b 100755 --- a/docgen/json/gen.sh +++ b/docgen/json/gen.sh @@ -5,9 +5,11 @@ THIS_PATH="$(realpath "$(dirname "$0")")" SCHEMA_PATH="$(realpath "$THIS_PATH/../../schema")" DOCS_PATH="$THIS_PATH/docs" TEMPLATES_PATH="$THIS_PATH/templates" +VALID_CYCLONEDX_VERSIONS=(1.2 1.3 1.4 1.5 1.6) +DRAFT_CYCLONEDX_VERSIONS=() +echo "Deleting folder $DOCS_PATH" rm -f -R "$DOCS_PATH" -mkdir -p "$DOCS_PATH/"{1.2,1.3,1.4,1.5,1.6} # Check to see if generate-schema-doc is executable and is in the path. If not, install JSON Schema for Humans. if ! [ -x "$(command -v generate-schema-doc)" ] @@ -16,8 +18,31 @@ then pip3 install -r "$THIS_PATH/requirements.txt" fi +# Function to check if a version is in an array +version_in_list() { + local version="$1" + shift + local list=("$@") + for item in "${list[@]}"; do + if [[ "$item" == "$version" ]]; then + return 0 + fi + done + return 1 +} + generate () { version="$1" + + # Check if the version match a valid CycloneDX version or a draft under development + if ! version_in_list "$version" "${VALID_CYCLONEDX_VERSIONS[@]}" && ! version_in_list "$version" "${DRAFT_CYCLONEDX_VERSIONS[@]}"; then + echo "Failed: wrong CycloneDX version: $version" + exit 1 + fi + + echo "Create folder $DOCS_PATH/$version" + mkdir -p "$DOCS_PATH/$version" + title="CycloneDX v${version} JSON Reference" echo "Generating: $title" @@ -45,8 +70,33 @@ generate () { sed -i -e "s/\${version}/$version/g" "$DOCS_PATH/$version/index.html" } -generate 1.2 -generate 1.3 -generate 1.4 -generate 1.5 -generate 1.6 +USAGE_HELP="Generate HTML JSON Schema navigator for CyccloneDX +Usage: $0 : runs only for + $0 : loops over all valid and draft CycloneDX versions" + +# Main logic to handle the argument using a switch case +case "$#" in + 0) + # No arguments provided: Loop over all VALID_CYCLONEDX_VERSIONS and DRAFT_CYCLONEDX_VERSIONS + for version in "${VALID_CYCLONEDX_VERSIONS[@]}" "${DRAFT_CYCLONEDX_VERSIONS[@]}"; do + generate "$version" + done + ;; + 1) + case "$1" in + "-h"|"--help") + echo "Usage: $USAGE_HELP" + exit 1 + ;; + *) + # One argument provided: Call generate with the specific version + generate "$1" + ;; + esac + ;; + *) + # More than one argument provided: Show usage help + echo "Usage: $USAGE_HELP" + exit 1 + ;; +esac