zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/scripts/check_logs.sh (about) 1 #!/bin/bash 2 3 # Colors for terminal 4 if test -t 1; then 5 # check if it supports colors 6 ncolors=$(tput colors) 7 if test -n "$ncolors" && test $ncolors -ge 8; then 8 NC="$(tput sgr0)" # no color 9 RED="$(tput setaf 1)" # red 10 WHITE="$(tput setaf 7)" # white 11 fi 12 fi 13 14 exception="(HTTP|OpenID|OAuth|TLS|API|ID)" 15 16 # the "nolint: check-logs" comment should be places on the last line that the linter matches 17 exclude_linter="(?!.*//nolint: check-logs)" 18 19 function lintLogContainingUpperCase { 20 word_char="[\.-0-9a-z ]" 21 capital_word="([a-z]*[A-Z][a-zA-Z]*)" 22 23 grep --with-filename -n -P "Msg[f]?\(\"(($word_char|$exception)*)(?!$exception)($capital_word)($exclude_linter)" $1 24 } 25 26 # lintLogStartingWithUpperCase searched for log messages that start with an upper case letter 27 function lintLogStartingWithUpperCase { 28 grep --with-filename -n "Msg[f]\?(\"[A-Z]" $1 | grep -v -P "Msg[f]?\(\"$exception($exclude_linter)" 29 } 30 31 # lintLogStartingWithComponent searches for log messages that starts with a component "component:" 32 function lintLogStartingWithComponent { 33 # We'll check for different functions that can generate errors or logs. If they start with 34 # a number words followed by ":", it's considered as starting with a component. 35 # Examples: '.Msgf("component:")', '.Errorf("com ponent:")', '.Msg("com-ponent:")' 36 grep --with-filename -n -E "(Errorf|errors.New|Msg[f]?)\(\"[a-zA-Z-]+( [a-zA-Z-]+){0,1}:($exclude_linter)" $1 37 } 38 39 # lintErrorLogsBeggining searches for log messages that don't start with "failed to" 40 function lintErrorLogsBeggining { 41 grep --with-filename -n -P "Error\(\)(?:.*)\n?.(?:.*)Msg[f]?\(\"(?!(failed to|failed due|invalid|unexpected|unsupported))($exclude_linter)" $1 42 } 43 44 function printLintError { 45 errReason=$1 46 errPathAndContent=$2 47 48 IFS=':' read -r errPath errLine errLineContent <<< "$errPathAndContent" 49 errLocation="$errPath:$errLine" 50 51 if test -t 1; then 52 echo -e "${WHITE}$errLocation${NC}: ${RED}$errReason${NC}\n\t$errLineContent" 53 else 54 echo "$errLocation: $errReason: $errLineContent" 55 fi 56 } 57 58 files=$(find . -name '*.go' | grep -v '_test.go') 59 60 found_linting_error=false 61 62 for file in $files 63 do 64 lintOutput=$(lintLogStartingWithUpperCase "$file") 65 if [ $? -eq 0 ]; then 66 found_linting_error=true 67 while IFS= read -r line; do 68 printLintError "Log message should not start with a CAPITAL letter" "$(echo $line | tr -s [:space:])" 69 done <<< "$lintOutput" 70 fi 71 72 lintOutput=$(lintLogStartingWithComponent "$file") 73 if [ $? -eq 0 ]; then 74 found_linting_error=true 75 while IFS= read -r line; do 76 printLintError "Log message should not start with the component (ex: 'component:', 'mixed component-with-dash:')" \ 77 "$(echo $line | tr -s [:space:])" 78 done <<< "$lintOutput" 79 fi 80 81 lintOutput=$(lintErrorLogsBeggining "$file") 82 if [ $? -eq 0 ]; then 83 found_linting_error=true 84 while IFS= read -r line; do 85 printLintError "Error messages should start with 'failed to'" \ 86 "$(echo $line | tr -s [:space:])" 87 done <<< "$lintOutput" 88 fi 89 done 90 91 if [ $found_linting_error = true ]; then 92 exit 1 93 fi 94 95 exit 0