Skip to content

Commit 6de8d8f

Browse files
hack restored
1 parent 3023a9c commit 6de8d8f

6 files changed

Lines changed: 288 additions & 0 deletions

File tree

hack/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Hack directory
2+
3+
This package contains various scripts that are used by Codeowners Validator developer.
4+
5+
## Purpose
6+
7+
This directory contains tools, such as Go fmt, Go lint, and Go vet, that help to maintain the source code compliant to Go best coding practices. It also includes utility scripts that generate code, and scripts executed on CI pipelines.

hack/compress.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
# Inspired by https://liam.sh/post/makefiles-for-go-projects
3+
4+
# standard bash error handling
5+
set -o nounset # treat unset variables as an error and exit immediately.
6+
set -o errexit # exit immediately when a command fails.
7+
set -E # needs to be set if we want the ERR trap
8+
9+
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
10+
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)
11+
readonly CURRENT_DIR
12+
readonly ROOT_PATH
13+
14+
# shellcheck source=./hack/lib/utilities.sh
15+
source "${CURRENT_DIR}/lib/utilities.sh" || {
16+
echo 'Cannot load CI utilities.'
17+
exit 1
18+
}
19+
20+
function main() {
21+
# This will find all files (not symlinks) with the executable bit set:
22+
# https://apple.stackexchange.com/a/116371
23+
binariesToCompress=$(find "${ROOT_PATH}/dist" -perm +111 -type f)
24+
25+
shout "Staring compression for: \n$binariesToCompress"
26+
27+
command -v upx >/dev/null || {
28+
echo 'UPX binary not found, skipping compression.'
29+
exit 1
30+
}
31+
32+
# I just do not like playing with xargs ¯\_(ツ)_/¯
33+
for i in $binariesToCompress; do
34+
upx --brute "$i"
35+
done
36+
}
37+
38+
main

hack/lib/utilities.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Library of useful utilities for CI purposes.
3+
#
4+
5+
readonly RED='\033[0;31m'
6+
readonly GREEN='\033[0;32m'
7+
readonly INVERTED='\033[7m'
8+
readonly NC='\033[0m' # No Color
9+
10+
readonly LIB_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
11+
12+
# Prints first argument as header. Additionally prints current date.
13+
shout() {
14+
echo -e "
15+
#################################################################################################
16+
# $(date)
17+
# $1
18+
#################################################################################################
19+
"
20+
}

hack/run-lint.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
3+
# standard bash error handling
4+
set -o nounset # treat unset variables as an error and exit immediately.
5+
set -o errexit # exit immediately when a command fails.
6+
set -E # needs to be set if we want the ERR trap
7+
8+
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
9+
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)
10+
GOLANGCI_LINT_VERSION="v1.55.2"
11+
TMP_DIR=$(mktemp -d)
12+
13+
readonly CURRENT_DIR
14+
readonly GOLANGCI_LINT_VERSION
15+
readonly ROOT_PATH
16+
readonly TMP_DIR
17+
18+
# shellcheck source=./hack/lib/utilities.sh
19+
source "${CURRENT_DIR}/lib/utilities.sh" || {
20+
echo 'Cannot load CI utilities.'
21+
exit 1
22+
}
23+
24+
host::install::golangci() {
25+
mkdir -p "${TMP_DIR}/bin"
26+
export PATH="${TMP_DIR}/bin:${PATH}"
27+
28+
shout "Install the golangci-lint ${GOLANGCI_LINT_VERSION} locally to a tempdir..."
29+
curl -sSfL -o "${TMP_DIR}/golangci-lint.sh" https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
30+
chmod 700 "${TMP_DIR}/golangci-lint.sh"
31+
32+
"${TMP_DIR}/golangci-lint.sh" -b "${TMP_DIR}/bin" ${GOLANGCI_LINT_VERSION}
33+
34+
echo -e "${GREEN}√ install golangci-lint${NC}"
35+
}
36+
37+
golangci::run_checks() {
38+
if [ -z "$(command -v golangci-lint)" ]; then
39+
echo "golangci-lint not found locally. Execute script with env variable INSTALL_DEPS=true"
40+
exit 1
41+
fi
42+
43+
GOT_VER=$(golangci-lint version --format=short 2>&1)
44+
if [[ "v${GOT_VER}" != "${GOLANGCI_LINT_VERSION}" ]]; then
45+
echo -e "${RED}✗ golangci-lint version mismatch, expected ${GOLANGCI_LINT_VERSION}, available ${GOT_VER} ${NC}"
46+
exit 1
47+
fi
48+
49+
shout "Run golangci-lint checks"
50+
51+
# shellcheck disable=SC2046
52+
golangci-lint run $(golangci::fix_if_requested) "${ROOT_PATH}/..."
53+
54+
echo -e "${GREEN}√ run golangci-lint${NC}"
55+
}
56+
57+
golangci::fix_if_requested() {
58+
if [[ "${LINT_FORCE_FIX:-x}" == "true" ]]; then
59+
echo "--fix"
60+
fi
61+
}
62+
63+
docker::run_dockerfile_checks() {
64+
shout "Run hadolint Dockerfile checks"
65+
docker run --rm -i hadolint/hadolint <"${ROOT_PATH}/Dockerfile"
66+
echo -e "${GREEN}√ run hadolint${NC}"
67+
}
68+
69+
shellcheck::run_checks() {
70+
shout "Run shellcheck checks"
71+
docker run --rm -v "$ROOT_PATH":/mnt koalaman/shellcheck:stable -x ./hack/*.sh
72+
echo -e "${GREEN}√ run shellcheck${NC}"
73+
}
74+
75+
main() {
76+
if [[ "${INSTALL_DEPS:-x}" == "true" ]]; then
77+
host::install::golangci
78+
fi
79+
80+
golangci::run_checks
81+
82+
docker::run_dockerfile_checks
83+
84+
shellcheck::run_checks
85+
}
86+
87+
main

hack/run-test-integration.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
3+
# standard bash error handling
4+
set -o nounset # treat unset variables as an error and exit immediately.
5+
set -o errexit # exit immediately when a command fails.
6+
set -E # needs to be set if we want the ERR trap
7+
8+
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
9+
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)
10+
TEST=""
11+
readonly CURRENT_DIR
12+
readonly ROOT_PATH
13+
14+
# shellcheck source=./hack/lib/utilities.sh
15+
source "${CURRENT_DIR}/lib/utilities.sh" || {
16+
echo 'Cannot load CI utilities.'
17+
exit 1
18+
}
19+
20+
pushd "${ROOT_PATH}" >/dev/null
21+
22+
# Exit handler. This function is called anytime an EXIT signal is received.
23+
# This function should never be explicitly called.
24+
function _trap_exit() {
25+
popd >/dev/null
26+
}
27+
trap _trap_exit EXIT
28+
29+
function print_info() {
30+
echo -e "${INVERTED}"
31+
echo "USER: ${USER:-"unknown"}"
32+
echo "PATH: ${PATH:-"unknown"}"
33+
echo "GOPATH: ${GOPATH:-"unknown"}"
34+
echo -e "${NC}"
35+
}
36+
37+
function test::integration() {
38+
shout "? go test integration"
39+
40+
# Check if tests passed
41+
# shellcheck disable=SC2046
42+
if ! go test -v -tags=integration $(test::run::specific) ./tests/integration/... $(test::update_golden); then
43+
echo -e "${RED}✗ go test integration\n${NC}"
44+
exit 1
45+
else
46+
echo -e "${GREEN}√ go test integration${NC}"
47+
fi
48+
}
49+
50+
function test::run::specific() {
51+
if [[ -n "${TEST}" ]]; then
52+
echo "-run=${TEST}"
53+
fi
54+
}
55+
function test::update_golden() {
56+
if [[ "${UPDATE_GOLDEN:-"false"}" == "true" ]]; then
57+
echo "-update"
58+
fi
59+
}
60+
61+
function main() {
62+
print_info
63+
64+
test::integration
65+
}
66+
67+
main

hack/run-test-unit.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
3+
# standard bash error handling
4+
set -o nounset # treat unset variables as an error and exit immediately.
5+
set -o errexit # exit immediately when a command fails.
6+
set -E # needs to be set if we want the ERR trap
7+
8+
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
9+
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)
10+
11+
readonly CURRENT_DIR
12+
readonly ROOT_PATH
13+
14+
# shellcheck source=./hack/lib/utilities.sh
15+
source "${CURRENT_DIR}/lib/utilities.sh" || {
16+
echo 'Cannot load CI utilities.'
17+
exit 1
18+
}
19+
20+
pushd "${ROOT_PATH}" >/dev/null
21+
22+
# Exit handler. This function is called anytime an EXIT signal is received.
23+
# This function should never be explicitly called.
24+
function _trap_exit() {
25+
popd >/dev/null
26+
}
27+
trap _trap_exit EXIT
28+
29+
function print_info() {
30+
echo -e "${INVERTED}"
31+
echo "USER: ${USER:-"unknown"}"
32+
echo "PATH: ${PATH:-"unknown"}"
33+
echo "GOPATH: ${GOPATH:-"unknown"}"
34+
echo -e "${NC}"
35+
}
36+
37+
function test::go_modules() {
38+
shout "? go mod tidy"
39+
go mod tidy
40+
STATUS=$(git status --porcelain go.mod go.sum)
41+
if [ -n "$STATUS" ]; then
42+
echo -e "${RED}✗ go mod tidy modified go.mod and/or go.sum${NC}"
43+
exit 1
44+
else
45+
echo -e "${GREEN}√ go mod tidy${NC}"
46+
fi
47+
}
48+
49+
function test::unit() {
50+
shout "? go test"
51+
52+
# Check if tests passed
53+
if ! go test -race -coverprofile="${ROOT_PATH}/coverage.txt" ./...; then
54+
echo -e "${RED}✗ go test\n${NC}"
55+
exit 1
56+
else
57+
echo -e "${GREEN}√ go test${NC}"
58+
fi
59+
}
60+
61+
function main() {
62+
print_info
63+
64+
test::go_modules
65+
66+
test::unit
67+
}
68+
69+
main

0 commit comments

Comments
 (0)