#!/bin/bash

# Exit immediately when a command fails
set -o errexit
# Error on unset variables
set -o nounset
# Only exit with zero if all commands of the pipeline exit successfully
set -o pipefail

# Source configuration
GOSEC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
source "${GOSEC_DIR}/go_build_config.sh"

if [[ ! $(command -v gosec) ]]; then
    CGO_ENABLED=0 GO111MODULE=on go install -ldflags "-s -w -extldflags '-static'" github.com/securego/gosec/v2/cmd/gosec
fi

if [[ ! $(command -v gosec) ]]; then
    echo "Unable to find gosec in \$PATH"
    exit 1
fi

cd "${GOSEC_DIR}/../"
gosec "./..."

#  gosec -exclude-dir=rules -exclude-dir=cmd ./...
# gosec -tests ./...
# gosec can ignore generated go files with default generated code comment.
# // Code generated by some generator DO NOT EDIT.
# gosec -exclude-generated ./...
#it is possible to annotate the code with a #nosec comment.
#import "md5" // #nosec
#    /* #nosec */
#    if x > y {
#        h := md5.New() // this will also be ignored
#    }
