Skip to content

Commit 957d695

Browse files
authored
Merge pull request #2211 from chrischdi/pr-shellcheck-refactor
🌱 Use shellcheck binary instead of self-built docker image
2 parents 2fb4fd7 + 0000163 commit 957d695

File tree

7 files changed

+92
-208
lines changed

7 files changed

+92
-208
lines changed

Makefile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ GO_APIDIFF_BIN := go-apidiff
129129
GO_APIDIFF := $(abspath $(TOOLS_BIN_DIR)/$(GO_APIDIFF_BIN)-$(GO_APIDIFF_VER))
130130
GO_APIDIFF_PKG := github.com/joelanford/go-apidiff
131131

132+
SHELLCHECK_VER := v0.9.0
133+
132134
KPROMO_VER := v4.0.4
133135
KPROMO_BIN := kpromo
134136
KPROMO := $(abspath $(TOOLS_BIN_DIR)/$(KPROMO_BIN)-$(KPROMO_VER))
@@ -315,7 +317,6 @@ generate-e2e-templates: ## Generate e2e cluster templates
315317
lint: $(GOLANGCI_LINT) ## Lint the codebase
316318
$(MAKE) lint-go-full
317319
$(MAKE) lint-markdown
318-
$(MAKE) lint-shell
319320

320321
GOLANGCI_LINT_EXTRA_ARGS ?= --fast=true
321322
.PHONY: lint-go
@@ -330,10 +331,6 @@ lint-go-full: lint-go ## Run slower linters to detect possible issues
330331
lint-markdown: ## Lint the project's markdown
331332
docker run --rm -v "$$(pwd)":/build$(DOCKER_VOL_OPTS) gcr.io/cluster-api-provider-vsphere/extra/mdlint:0.17.0 -- /md/lint -i _releasenotes .
332333

333-
.PHONY: lint-shell
334-
lint-shell: ## Lint the project's shell scripts
335-
docker run --rm -t -v "$$(pwd)":/build:ro gcr.io/cluster-api-provider-vsphere/extra/shellcheck
336-
337334
.PHONY: lint-fix
338335
lint-fix: $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linter
339336
GOLANGCI_LINT_EXTRA_ARGS="--fast=false --fix" $(MAKE) lint-go
@@ -344,10 +341,10 @@ APIDIFF_OLD_COMMIT ?= $(shell git rev-parse origin/main)
344341
apidiff: $(GO_APIDIFF) ## Check for API differences
345342
$(GO_APIDIFF) $(APIDIFF_OLD_COMMIT) --print-compatible
346343

347-
ALL_VERIFY_CHECKS = boilerplate modules gen conversions doctoc flavors
344+
ALL_VERIFY_CHECKS = boilerplate shellcheck modules gen conversions doctoc flavors
348345

349346
.PHONY: verify
350-
verify: $(addprefix verify-,$(ALL_VERIFY_CHECKS)) lint-markdown lint-shell ## Run all verify-* targets
347+
verify: $(addprefix verify-,$(ALL_VERIFY_CHECKS)) lint-markdown ## Run all verify-* targets
351348

352349
.PHONY: verify-modules
353350
verify-modules: generate-modules ## Verify go modules are up to date
@@ -382,6 +379,10 @@ verify-doctoc: generate-doctoc
382379
verify-boilerplate: ## Verify boilerplate text exists in each file
383380
TRACE=$(TRACE) ./hack/verify-boilerplate.sh
384381

382+
.PHONY: verify-shellcheck
383+
verify-shellcheck: ## Verify shell files
384+
TRACE=$(TRACE) ./hack/verify-shellcheck.sh $(SHELLCHECK_VER)
385+
385386
.PHONY: verify-container-images
386387
verify-container-images: ## Verify container images
387388
TRACE=$(TRACE) ./hack/verify-container-images.sh

hack/check-shell.sh

Lines changed: 0 additions & 28 deletions
This file was deleted.

hack/match-release-tag.sh

Lines changed: 0 additions & 108 deletions
This file was deleted.

hack/tools/shellcheck/Dockerfile

Lines changed: 0 additions & 31 deletions
This file was deleted.

hack/tools/shellcheck/Makefile

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#!/bin/bash
2-
1+
#!/usr/bin/env bash
32
# Copyright 2019 The Kubernetes Authors.
43
#
54
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,8 +13,7 @@
1413
# See the License for the specific language governing permissions and
1514
# limitations under the License.
1615

17-
set -o errexit
18-
set -o nounset
19-
set -o pipefail
20-
21-
find . -path ./vendor -prune -o -name "*.*sh" -type f -print0 | xargs -0 shellcheck "${@}"
16+
# get_root_path returns the root path of the project source tree
17+
get_root_path() {
18+
git rev-parse --show-toplevel
19+
}

hack/verify-shellcheck.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2019 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 -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
if [[ "${TRACE-0}" == "1" ]]; then
21+
set -o xtrace
22+
fi
23+
24+
if [ $# -ne 1 ]; then
25+
echo 1>&2 "$0: usage: ./verify-shellcheck.sh <version>"
26+
exit 2
27+
fi
28+
29+
VERSION=${1}
30+
31+
OS="unknown"
32+
if [[ "${OSTYPE}" == "linux"* ]]; then
33+
OS="linux"
34+
elif [[ "${OSTYPE}" == "darwin"* ]]; then
35+
OS="darwin"
36+
fi
37+
38+
# shellcheck source=./hack/utils.sh
39+
source "$(dirname "$0")/utils.sh"
40+
ROOT_PATH=$(get_root_path)
41+
42+
# create a temporary directory
43+
TMP_DIR=$(mktemp -d)
44+
OUT="${TMP_DIR}/out.log"
45+
46+
# cleanup on exit
47+
cleanup() {
48+
ret=0
49+
if [[ -s "${OUT}" ]]; then
50+
echo "Found errors:"
51+
cat "${OUT}"
52+
ret=1
53+
fi
54+
echo "Cleaning up..."
55+
rm -rf "${TMP_DIR}"
56+
exit ${ret}
57+
}
58+
trap cleanup EXIT
59+
60+
61+
SHELLCHECK="./$(dirname "$0")/tools/bin/shellcheck/${VERSION}/shellcheck"
62+
63+
if [ ! -f "$SHELLCHECK" ]; then
64+
# install buildifier
65+
cd "${TMP_DIR}" || exit
66+
DOWNLOAD_FILE="shellcheck-${VERSION}.${OS}.x86_64.tar.xz"
67+
curl -L "https://github.com/koalaman/shellcheck/releases/download/${VERSION}/${DOWNLOAD_FILE}" -o "${TMP_DIR}/shellcheck.tar.xz"
68+
tar xf "${TMP_DIR}/shellcheck.tar.xz"
69+
cd "${ROOT_PATH}"
70+
mkdir -p "$(dirname "$0")/tools/bin/shellcheck/${VERSION}"
71+
mv "${TMP_DIR}/shellcheck-${VERSION}/shellcheck" "$SHELLCHECK"
72+
fi
73+
74+
echo "Running shellcheck..."
75+
cd "${ROOT_PATH}" || exit
76+
FILES=$(find . -name "*.sh")
77+
while read -r file; do
78+
"$SHELLCHECK" -x "$file" >> "${OUT}" 2>&1
79+
done <<< "$FILES"

0 commit comments

Comments
 (0)