|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# - Ensure single `tags` are used(openapi-generator doesn't work well with multiple tags) |
| 4 | +# - Ensure Pascal case is used for `name` field |
| 5 | + |
| 6 | +set -e pipefail |
| 7 | + |
| 8 | +haproxy_spec="./build/haproxy_spec.yaml" |
| 9 | +found="false" |
| 10 | +countTags=0 |
| 11 | +lineNo=0 |
| 12 | + |
| 13 | +# checkCamelCaseInNameField checks if usage of camel case name is supported |
| 14 | +# and if not, it exit with error code 1 |
| 15 | +checkCamelCaseInNameField() |
| 16 | +{ |
| 17 | + local name="$(echo $@ | grep '^name: ')" |
| 18 | + if [ -n "$name" ]; then |
| 19 | + local upperCased="$(echo "$name" | cut -d ":" -f 2 | grep -e '[[:upper:]]')" |
| 20 | + |
| 21 | + if [ -n "$upperCased" ]; then |
| 22 | + # Add camel case names we want to support |
| 23 | + local allowedCamelCaseNameFields=" HAProxy Support X-Runtime-Actions forceDelete forceSync" |
| 24 | + |
| 25 | + local allowed="$(echo "$allowedCamelCaseNameFields" | grep "$upperCased")" |
| 26 | + if [ -z "$allowed" ] ; then |
| 27 | + echo "Camel case \"$trimLine\" used at line no: $lineNo. Use Pascal case instead." |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | + fi |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +while IFS= read -r line |
| 35 | +do |
| 36 | + lineNo=$((lineNo+1)) |
| 37 | + # skip root tags |
| 38 | + if [ "$line" = "tags:" ]; then |
| 39 | + continue |
| 40 | + fi |
| 41 | + |
| 42 | + trimLine="${line#"${line%%[![:space:]]*}"}" |
| 43 | + |
| 44 | + # ensure camel case is not used in name fields |
| 45 | + checkCamelCaseInNameField $trimLine |
| 46 | + |
| 47 | + if [ "$found" = "true" ] && [ -n "$trimLine" ] && [ -z "$(echo "$trimLine" | cut -d "-" -f 1)" ]; then |
| 48 | + countTags=$((countTags+1)) |
| 49 | + else |
| 50 | + found="false" |
| 51 | + countTags=0 |
| 52 | + fi |
| 53 | + |
| 54 | + if [ "$countTags" -gt 1 ]; then |
| 55 | + echo "Multiple tags are not supported. Additional tag: $line at line no: $lineNo" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + |
| 59 | + # handle only tags from path |
| 60 | + if [ "$trimLine" = "tags:" ]; then |
| 61 | + found="true" |
| 62 | + fi |
| 63 | +done < "$haproxy_spec" |
| 64 | + |
| 65 | +echo "Linting YAML PASSED" |
| 66 | +exit 0 |
0 commit comments