pre-commit
author eyeokg <k.galczynski@eyeo.com>
Tue, 01 Oct 2024 08:02:53 +0200
changeset 25704 4c03dcd1e5aa
child 25705 d7bf4e5f34aa
permissions -rwxr-xr-x
Initial commit for domains variables

#!/bin/sh

if ! [ -f "./exceptionrules/domains-variables.json" ]; then
    echo "File does not exist"
    exit 1
fi
staged_domains_variables=$(git show :./exceptionrules/domains-variables.json 2>/dev/null)

if ! [ -n "$staged_domains_variables" ]; then
    echo "No changes in domains variables"
    exit 0
fi

if ! jq -e . >/dev/null 2>&1 <<<"$staged_domains_variables"; then
    echo "Error: Invalid JSON content"
    exit 1
fi

object=$(jq '.' <<< "$staged_domains_variables")
keys=$(jq 'keys' <<< "$object")

for key in $(jq -r '.[]' <<< "$keys"); do
    if ! [[ "$key" =~ ^[[:alnum:]_]+$ ]]; then
        echo "invalid key: $key, only alphanumeric characters and underscores are allowed"
        invalid=true
    fi
    elements=$(jq -r ".[\"$key\"][]" <<< "$object")
    for element in $elements; do
        if ! [[ "$element" =~ ^[[:alnum:]*][[:alnum:]*.-]+$ ]]; then
            echo "invalid domain in $key: $element"
            invalid=true
        fi
    done
done

if [ "$invalid" = true ]; then
    exit 1
fi

exit 0