pre-commit
changeset 25704 4c03dcd1e5aa
child 25705 d7bf4e5f34aa
equal deleted inserted replaced
25411:be3bc2647069 25704:4c03dcd1e5aa
       
     1 #!/bin/sh
       
     2 
       
     3 if ! [ -f "./exceptionrules/domains-variables.json" ]; then
       
     4     echo "File does not exist"
       
     5     exit 1
       
     6 fi
       
     7 staged_domains_variables=$(git show :./exceptionrules/domains-variables.json 2>/dev/null)
       
     8 
       
     9 if ! [ -n "$staged_domains_variables" ]; then
       
    10     echo "No changes in domains variables"
       
    11     exit 0
       
    12 fi
       
    13 
       
    14 if ! jq -e . >/dev/null 2>&1 <<<"$staged_domains_variables"; then
       
    15     echo "Error: Invalid JSON content"
       
    16     exit 1
       
    17 fi
       
    18 
       
    19 object=$(jq '.' <<< "$staged_domains_variables")
       
    20 keys=$(jq 'keys' <<< "$object")
       
    21 
       
    22 for key in $(jq -r '.[]' <<< "$keys"); do
       
    23     if ! [[ "$key" =~ ^[[:alnum:]_]+$ ]]; then
       
    24         echo "invalid key: $key, only alphanumeric characters and underscores are allowed"
       
    25         invalid=true
       
    26     fi
       
    27     elements=$(jq -r ".[\"$key\"][]" <<< "$object")
       
    28     for element in $elements; do
       
    29         if ! [[ "$element" =~ ^[[:alnum:]*][[:alnum:]*.-]+$ ]]; then
       
    30             echo "invalid domain in $key: $element"
       
    31             invalid=true
       
    32         fi
       
    33     done
       
    34 done
       
    35 
       
    36 if [ "$invalid" = true ]; then
       
    37     exit 1
       
    38 fi
       
    39 
       
    40 exit 0