From 7feae074ccc7c4227b8518512088e764c919f16a Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Sat, 22 Jun 2019 23:48:50 +0300 Subject: [PATCH] hack: include more update and verify scripts update: - deps: calls go mod tidy verify: - build: calls build for ./cmd - deps: verifies if the current go.* files are valid - gotest: calls go tests on _test files if present - govet: calls go vet - shellcheck: executes shellcheck on all .sh files --- hack/update-all.sh | 25 +++++++++++ hack/update-deps.sh | 27 ++++++++++++ hack/verify-all.sh | 88 +++++++++++++++++++++++++++++++++++++++ hack/verify-build.sh | 26 ++++++++++++ hack/verify-deps.sh | 50 ++++++++++++++++++++++ hack/verify-gofmt.sh | 31 ++++++++++++++ hack/verify-gotest.sh | 27 ++++++++++++ hack/verify-govet.sh | 28 +++++++++++++ hack/verify-shellcheck.sh | 54 ++++++++++++++++++++++++ 9 files changed, 356 insertions(+) create mode 100755 hack/update-all.sh create mode 100755 hack/update-deps.sh create mode 100755 hack/verify-all.sh create mode 100755 hack/verify-build.sh create mode 100755 hack/verify-deps.sh create mode 100755 hack/verify-gofmt.sh create mode 100755 hack/verify-gotest.sh create mode 100755 hack/verify-govet.sh create mode 100755 hack/verify-shellcheck.sh diff --git a/hack/update-all.sh b/hack/update-all.sh new file mode 100755 index 0000000..8839e01 --- /dev/null +++ b/hack/update-all.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" +REPO_PATH=$(get_root_path) + +"${REPO_PATH}"/hack/update-deps.sh +"${REPO_PATH}"/hack/update-gofmt.sh diff --git a/hack/update-deps.sh b/hack/update-deps.sh new file mode 100755 index 0000000..e4137c9 --- /dev/null +++ b/hack/update-deps.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +set -o nounset +set -o errexit +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" +# cd to the root path +cd_root_path + +export GO111MODULE="on" +go mod tidy diff --git a/hack/verify-all.sh b/hack/verify-all.sh new file mode 100755 index 0000000..1d227dc --- /dev/null +++ b/hack/verify-all.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" + +# set REPO_PATH +REPO_PATH=$(get_root_path) +cd "${REPO_PATH}" + +# exit code, if a script fails we'll set this to 1 +res=0 + +# run all verify scripts, optionally skipping any of them + +if [[ "${VERIFY_WHITESPACE:-true}" == "true" ]]; then + echo "[*] Verifying whitespace..." + hack/verify-whitespace.sh || res=1 + cd "${REPO_PATH}" +fi + +if [[ "${VERIFY_SPELLING:-true}" == "true" ]]; then + echo "[*] Verifying spelling..." + hack/verify-spelling.sh || res=1 + cd "${REPO_PATH}" +fi + +if [[ "${VERIFY_GOFMT:-true}" == "true" ]]; then + echo "[*] Verifying gofmt..." + hack/verify-gofmt.sh || res=1 + cd "${REPO_PATH}" +fi + +if [[ "${VERIFY_GOLINT:-true}" == "true" ]]; then + echo "[*] Verifying golint..." + hack/verify-golint.sh || res=1 + cd "${REPO_PATH}" +fi + +if [[ "${VERIFY_GOVET:-true}" == "true" ]]; then + echo "[*] Verifying govet..." + hack/verify-govet.sh || res=1 + cd "${REPO_PATH}" +fi + +if [[ "${VERIFY_DEPS:-true}" == "true" ]]; then + echo "[*] Verifying deps..." + hack/verify-deps.sh || res=1 + cd "${REPO_PATH}" +fi + +if [[ "${VERIFY_GOTEST:-true}" == "true" ]]; then + echo "[*] Verifying gotest..." + hack/verify-gotest.sh || res=1 + cd "${REPO_PATH}" +fi + +if [[ "${VERIFY_BUILD:-true}" == "true" ]]; then + echo "[*] Verifying build..." + hack/verify-build.sh || res=1 + cd "${REPO_PATH}" +fi + +# exit based on verify scripts +if [[ "${res}" = 0 ]]; then + echo "" + echo "All verify checks passed, congrats!" +else + echo "" + echo "One or more verify checks failed! See output above..." +fi +exit "${res}" diff --git a/hack/verify-build.sh b/hack/verify-build.sh new file mode 100755 index 0000000..8284b60 --- /dev/null +++ b/hack/verify-build.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" + +# check if the code builds +cd_root_path +export GO111MODULE=on +go build ./cmd/... diff --git a/hack/verify-deps.sh b/hack/verify-deps.sh new file mode 100755 index 0000000..cacc490 --- /dev/null +++ b/hack/verify-deps.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" +# cd to the root path +cd_root_path + +# cleanup on exit +cleanup() { + echo "Cleaning up..." + mv go.mod.old go.mod + mv go.sum.old go.sum +} +trap cleanup EXIT + +echo "Verifying..." +# temporary copy the go mod and sum files +cp go.mod go.mod.old || exit +cp go.sum go.sum.old || exit + +# run update-deps.sh +export GO111MODULE="on" +./hack/update-deps.sh + +# compare the old and new files +DIFF0=$(diff -u go.mod go.mod.old) +DIFF1=$(diff -u go.sum go.sum.old) + +if [[ -n "${DIFF0}" ]] || [[ -n "${DIFF1}" ]]; then + echo "${DIFF0}" + echo "${DIFF1}" + echo "Check failed. Please run ./hack/update-deps.sh" + exit 1 +fi diff --git a/hack/verify-gofmt.sh b/hack/verify-gofmt.sh new file mode 100755 index 0000000..c49b850 --- /dev/null +++ b/hack/verify-gofmt.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" +# cd to the root path +cd_root_path + +# check for gofmt diffs +diff=$(git ls-files | grep "\.go" | grep -v "\/vendor" | xargs gofmt -s -d 2>&1) +if [[ -n "${diff}" ]]; then + echo "${diff}" + echo + echo "Check failed. Please run hack/update-gofmt.sh" +fi diff --git a/hack/verify-gotest.sh b/hack/verify-gotest.sh new file mode 100755 index 0000000..7f078d6 --- /dev/null +++ b/hack/verify-gotest.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" +# cd to the root path +cd_root_path + +# run go test +export GO111MODULE=on +go test ./... diff --git a/hack/verify-govet.sh b/hack/verify-govet.sh new file mode 100755 index 0000000..ccfe41f --- /dev/null +++ b/hack/verify-govet.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# CI script to run go vet over our code +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" +# cd to the root path +cd_root_path + +# run go vet +export GO111MODULE=on +go vet ./... diff --git a/hack/verify-shellcheck.sh b/hack/verify-shellcheck.sh new file mode 100755 index 0000000..2cd3d19 --- /dev/null +++ b/hack/verify-shellcheck.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# set -o errexit +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" +ROOT_PATH=$(get_root_path) + +# create a temporary directory +TMP_DIR=$(mktemp -d) + +# cleanup on exit +cleanup() { + echo "Cleaning up..." + rm -rf "${TMP_DIR}" +} +trap cleanup EXIT + +# install shellcheck (Linux-x64 only!) +cd "${TMP_DIR}" || exit +VERSION="shellcheck-v0.6.0" +DOWNLOAD_FILE="${VERSION}.linux.x86_64.tar.xz" +wget https://storage.googleapis.com/shellcheck/"${DOWNLOAD_FILE}" +tar xf "${DOWNLOAD_FILE}" +cd "${VERSION}" || exit + +echo "Running shellcheck..." +cd "${ROOT_PATH}" || exit +OUT="${TMP_DIR}/out.log" +FILES=$(find . -name "*.sh") +while read -r file; do + "${TMP_DIR}/${VERSION}/shellcheck" "$file" >> "${OUT}" 2>&1 +done <<< "$FILES" + +if [[ -s "${OUT}" ]]; then + echo "Found errors:" + cat "${OUT}" + exit 1 +fi