File: lint-disallowed-functions-in-library.sh

package info (click to toggle)
golang-github-pion-datachannel 1.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 232 kB
  • sloc: makefile: 2
file content (48 lines) | stat: -rwxr-xr-x 1,263 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash

#
# DO NOT EDIT THIS FILE
#
# It is automatically copied from https://github.com/pion/.goassets repository.
#
# If you want to update the shared CI config, send a PR to
# https://github.com/pion/.goassets instead of this repository.
#

set -e

# Disallow usages of functions that cause the program to exit in the library code
SCRIPT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
if [ -f ${SCRIPT_PATH}/.ci.conf ]
then
  . ${SCRIPT_PATH}/.ci.conf
fi

EXCLUDE_DIRECTORIES=${DISALLOWED_FUNCTIONS_EXCLUDED_DIRECTORIES:-"examples"}
DISALLOWED_FUNCTIONS=('os.Exit(' 'panic(' 'Fatal(' 'Fatalf(' 'Fatalln(' 'fmt.Println(' 'fmt.Printf(' 'log.Print(' 'log.Println(' 'log.Printf(' 'print(' 'println(')

files=$(
  find "$SCRIPT_PATH/.." -name "*.go" \
    | grep -v -e '^.*_test.go$' \
    | while read file
    do
      excluded=false
      for ex in $EXCLUDE_DIRECTORIES
      do
        if [[ $file == */$ex/* ]]
        then
          excluded=true
          break
        fi
      done
      $excluded || echo "$file"
    done
)

for disallowedFunction in "${DISALLOWED_FUNCTIONS[@]}"
do
	if grep -e "\s$disallowedFunction" $files | grep -v -e 'nolint'; then
		echo "$disallowedFunction may only be used in example code"
		exit 1
	fi
done