github.skymusic.top/operator-framework/operator-sdk@v0.8.2/hack/lib/test_lib.sh (about) 1 #!/usr/bin/env bash 2 3 source hack/lib/common.sh 4 5 function listPkgs() { 6 go list ./cmd/... ./pkg/... ./test/... | grep -v generated 7 } 8 9 function listFiles() { 10 # make it work with composed gopath 11 for gopath in ${GOPATH//:/ }; do 12 if [[ "$(pwd)" =~ "$gopath" ]]; then 13 GOPATH="$gopath" 14 break 15 fi 16 done 17 # pipeline is much faster than for loop 18 listPkgs | xargs -I {} find "${GOPATH}/src/{}" -name '*.go' | grep -v generated 19 } 20 21 #=================================================================== 22 # FUNCTION trap_add () 23 # 24 # Purpose: prepends a command to a trap 25 # 26 # - 1st arg: code to add 27 # - remaining args: names of traps to modify 28 # 29 # Example: trap_add 'echo "in trap DEBUG"' DEBUG 30 # 31 # See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal 32 #=================================================================== 33 function trap_add() { 34 trap_add_cmd=$1; shift || fatal "${FUNCNAME} usage error" 35 new_cmd= 36 for trap_add_name in "$@"; do 37 # Grab the currently defined trap commands for this trap 38 existing_cmd=`trap -p "${trap_add_name}" | awk -F"'" '{print $2}'` 39 40 # Define default command 41 [ -z "${existing_cmd}" ] && existing_cmd="echo exiting @ `date`" 42 43 # Generate the new command 44 new_cmd="${trap_add_cmd};${existing_cmd}" 45 46 # Assign the test 47 trap "${new_cmd}" "${trap_add_name}" || \ 48 fatal "unable to add to trap ${trap_add_name}" 49 done 50 } 51 52 # add_go_mod_replace adds a "replace" directive from $1 to $2 with an 53 # optional version version $3 to the current working directory's go.mod file. 54 function add_go_mod_replace() { 55 local from_path="${1:?first path in replace statement is required}" 56 local to_path="${2:?second path in replace statement is required}" 57 local version="${3:-}" 58 59 if [[ ! -d "$to_path" && -z "$version" ]]; then 60 echo "second replace path $to_path requires a version be set because it is not a directory" 61 exit 1 62 fi 63 if [[ ! -e go.mod ]]; then 64 echo "go.mod file not found in $(pwd)" 65 exit 1 66 fi 67 68 # If $to_path is a directory, it needs a `go.mod` file that specifies the 69 # module name to make the go toolchain happy. 70 # 71 # TODO: remove the below if statement once 72 # https://github.com/operator-framework/operator-sdk/pull/1566 is merged, 73 # which updates the SDK to use go modules. 74 if [[ -d "${to_path}" && ! -e "${to_path}/go.mod" ]]; then 75 echo "module ${from_path}" > "${to_path}/go.mod" 76 trap_add "rm ${to_path}/go.mod" EXIT 77 fi 78 # Check if a replace line already exists. If it does, remove. If not, append. 79 if grep -q "${from_path} =>" go.mod; then 80 sed -E -i 's|^.+'"${from_path} =>"'.+$||g' go.mod 81 fi 82 # Do not use "go mod edit" so formatting stays the same. 83 local replace="replace ${from_path} => ${to_path}" 84 if [[ -n "$version" ]]; then 85 replace="$replace $version" 86 fi 87 echo "$replace" >> go.mod 88 }