Skip to content
This repository was archived by the owner on Mar 26, 2021. It is now read-only.

Commit b2164fe

Browse files
committed
Add repo-infra scripts to verify go source code
1 parent 0a5f5e0 commit b2164fe

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

hack/go-tools/verify-gofmt.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2018 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -euo pipefail
17+
18+
find_files() {
19+
find . -not \( \
20+
\( \
21+
-wholename '*/vendor/*' \
22+
\) -prune \
23+
\) -name '*.go'
24+
}
25+
26+
GOFMT="gofmt -s"
27+
bad_files=$(find_files | xargs $GOFMT -l)
28+
if [[ -n "${bad_files}" ]]; then
29+
echo "!!! '$GOFMT' needs to be run on the following files: "
30+
echo "${bad_files}"
31+
exit 1
32+
fi

hack/go-tools/verify-govet.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2018 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -euo pipefail
17+
18+
go vet -v $(go list ./... | grep -v /vendor/)

hack/verify-go-src.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2018 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -euo pipefail
17+
18+
# This script is intended to be used via subtree in a top-level directory:
19+
# <repo>/
20+
# repo-infra/
21+
# verify/
22+
# Or via vendoring and passing root directory as vendor/repo-infra/verify-*.sh --rootdir **full path to your repo dir**
23+
# <repo>/
24+
# vendor/
25+
# repo-infra/
26+
# ...
27+
#
28+
29+
30+
SILENT=true
31+
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
32+
33+
# Convert long opts to short ones to read through getopts
34+
for arg in "$@"; do
35+
shift
36+
case "$arg" in
37+
"--rootdir") set -- "$@" "-r";;
38+
*)
39+
set -- "$@" "$arg"
40+
;;
41+
esac
42+
done
43+
44+
OPTIND=1
45+
while getopts "vr:" opt; do
46+
case ${opt} in
47+
v)
48+
SILENT=false
49+
;;
50+
r)
51+
REPO_ROOT=${OPTARG}
52+
;;
53+
\?)
54+
echo "Invalid flag: -${OPTARG}" >&2
55+
exit 1
56+
;;
57+
esac
58+
done
59+
60+
shift "$(($OPTIND-1))"
61+
62+
echo "Working directory: ${REPO_ROOT}"
63+
64+
GO_TOOLS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/go-tools"
65+
66+
function run-cmd {
67+
if ${SILENT}; then
68+
"$@" &> /dev/null
69+
else
70+
"$@"
71+
fi
72+
}
73+
74+
# Some useful colors.
75+
if [[ -z "${color_start-}" ]]; then
76+
declare -r color_start="\033["
77+
declare -r color_red="${color_start}0;31m"
78+
declare -r color_yellow="${color_start}0;33m"
79+
declare -r color_green="${color_start}0;32m"
80+
declare -r color_norm="${color_start}0m"
81+
fi
82+
83+
function run-checks {
84+
local -r pattern=$1
85+
local -r runner=$2
86+
87+
for t in $(ls ${pattern})
88+
do
89+
echo -e "Verifying ${t}"
90+
local start=$(date +%s)
91+
cd $REPO_ROOT && run-cmd "${runner}" "${t}" && tr=$? || tr=$?
92+
local elapsed=$(($(date +%s) - ${start}))
93+
if [[ ${tr} -eq 0 ]]; then
94+
echo -e "${color_green}SUCCESS${color_norm} ${t}\t${elapsed}s"
95+
else
96+
echo -e "${color_red}FAILED${color_norm} ${t}\t${elapsed}s"
97+
ret=1
98+
fi
99+
done
100+
}
101+
102+
if ${SILENT} ; then
103+
echo "Running in silent mode, run with -v if you want to see script logs."
104+
fi
105+
106+
ret=0
107+
run-checks "${GO_TOOLS_DIR}/*.sh" bash
108+
exit ${ret}

0 commit comments

Comments
 (0)