Skip to content

Commit b63152e

Browse files
committed
fix shellcheck and auto fix
1 parent 33bd4d9 commit b63152e

File tree

8 files changed

+43
-13
lines changed

8 files changed

+43
-13
lines changed

.githooks/pre-commit

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,24 +230,54 @@ pre_commit() {
230230
run_shellcheck() {
231231
local file="$1"
232232
echo "Running shellcheck on $file"
233-
if ! shellcheck --color=always -x "$file" -e SC2154 -e SC1091 -e SC1090 -e SC2148 -o require-variable-braces -P "scripts"; then
233+
234+
# Try to auto-fix issues
235+
local diff_output
236+
diff_output=$(shellcheck -f diff "$file" -e SC2154 -e SC1091 -e SC1090 -e SC2148 -o require-variable-braces -P "scripts" 2>&1)
237+
238+
if [[ "$diff_output" == *"Issues were detected, but none were auto-fixable"* ]]; then
234239
echo -e "${RED}shellcheck failed on $file${NO_COLOR}"
235-
exit 1
240+
echo "$file" >> "${SHELLCHECK_FAILED_FILE}"
241+
return 1
242+
elif echo "$diff_output" | git apply --allow-empty; then
243+
echo "Applied auto-fixes for $file"
244+
else
245+
echo -e "${RED}shellcheck failed on $file${NO_COLOR}"
246+
echo "$file" >> "${SHELLCHECK_FAILED_FILE}"
247+
return 1
236248
fi
237249
}
238250

239251
# Export function so it's available in subshells (for xargs)
240252
export -f run_shellcheck
241253

242254
start_shellcheck() {
255+
# Create temporary file to track failed files
256+
local failed_file
257+
failed_file=$(mktemp)
258+
export SHELLCHECK_FAILED_FILE="$failed_file"
259+
243260
# shellcheck disable=SC2016
244261
{
245262
find scripts -type f -name "*.sh"
246263
find scripts/dev/contexts -type f | grep -v private-context
247264
find scripts/funcs -type f
248265
find public/architectures -type f -name "*.sh"
249266
find docs/ -type f -name "*.sh"
250-
} | xargs -I {} -P 20 bash -c 'run_shellcheck "$1"' _ {}
267+
} | xargs -I {} -P 20 bash -c 'run_shellcheck "$1" || true' _ {}
268+
269+
# Show summary of failed files
270+
if [[ -s "$failed_file" ]]; then
271+
echo -e "\n${RED}Shellcheck failed on the following files:${NO_COLOR}"
272+
while read -r file; do
273+
echo -e " ${RED}${NO_COLOR} $file"
274+
done < "$failed_file"
275+
echo -e "\n${RED}Total: $(wc -l < "$failed_file") file(s) failed shellcheck${NO_COLOR}"
276+
rm -f "$failed_file"
277+
exit 1
278+
fi
279+
280+
rm -f "$failed_file"
251281
}
252282

253283
cmd=${1:-"pre-commit"}

scripts/dev/switch_context.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ if [[ -n "${PROJECT_DIR:-}" && -x "${PROJECT_DIR}/bin/kubectl" ]]; then
105105
KUBECTL_CMD="${PROJECT_DIR}/bin/kubectl"
106106
fi
107107

108-
if [[ "$KUBECTL_CMD" != "kubectl" ]] || which kubectl > /dev/null; then
108+
if [[ "${KUBECTL_CMD}" != "kubectl" ]] || which kubectl > /dev/null; then
109109
if [ "${CLUSTER_NAME-}" ]; then
110110
# The convention: the cluster name must match the name of kubectl context
111111
# We expect this not to be true if kubernetes cluster is still to be created (minikube/kops)
112-
if ! "$KUBECTL_CMD" config use-context "${CLUSTER_NAME}"; then
112+
if ! "${KUBECTL_CMD}" config use-context "${CLUSTER_NAME}"; then
113113
echo "Warning: failed to switch kubectl context to: ${CLUSTER_NAME}"
114114
echo "Does a matching Kubernetes context exist?"
115115
fi
116116

117117
# Setting the default namespace for current context
118-
"$KUBECTL_CMD" config set-context "$("$KUBECTL_CMD" config current-context)" "--namespace=${NAMESPACE}" &>/dev/null || true
118+
"${KUBECTL_CMD}" config set-context "$("${KUBECTL_CMD}" config current-context)" "--namespace=${NAMESPACE}" &>/dev/null || true
119119

120120
# shellcheck disable=SC2153
121121
echo "Current context: ${context} (kubectl context: ${CLUSTER_NAME}), namespace=${NAMESPACE}"

scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ chmod +x docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_s390x
3333
chmod +x docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_ppc64le
3434
chmod +x docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_arm64
3535

36-
cp docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_${ARCH} docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator || true
36+
cp docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_"${ARCH}" docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator || true
3737

3838
mkdir -p bin || true
3939
cp docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator bin/kubectl-mongodb || true

scripts/evergreen/e2e/build_tests_image_ibm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ cp requirements.txt docker/mongodb-kubernetes-tests/requirements.txt
99
cp -rf helm_chart docker/mongodb-kubernetes-tests/helm_chart
1010

1111
echo "Building mongodb-kubernetes-tests image with tag: ${BASE_REPO_URL}/mongodb-kubernetes-tests:${version_id}-$(arch)"
12-
cd docker/mongodb-kubernetes-tests
12+
cd docker/mongodb-kubernetes-tests || exit
1313
sudo podman buildx build --progress plain . -f Dockerfile -t "${BASE_REPO_URL}/mongodb-kubernetes-tests:${version_id}-$(arch)" --build-arg PYTHON_VERSION="${PYTHON_VERSION}"
1414
sudo podman push --authfile="/root/.config/containers/auth.json" "${BASE_REPO_URL}/mongodb-kubernetes-tests:${version_id}-$(arch)"

scripts/evergreen/setup_aws.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ install_aws_cli_pip() {
5858
${pip_cmd} install --user awscli
5959

6060
# Add ~/.local/bin to PATH if not already there (where pip --user installs)
61-
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
62-
export PATH="$HOME/.local/bin:$PATH"
61+
if [[ ":${PATH}:" != *":${HOME}/.local/bin:"* ]]; then
62+
export PATH="${HOME}/.local/bin:${PATH}"
6363
echo "Added ~/.local/bin to PATH"
6464
fi
6565

scripts/evergreen/setup_kind.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ arch_suffix=$(detect_architecture)
1212
latest_version="v0.27.0"
1313

1414
# Only proceed with installation if architecture is supported (amd64 or arm64)
15-
if [[ "$arch_suffix" == "amd64" || "$arch_suffix" == "arm64" ]]; then
15+
if [[ "${arch_suffix}" == "amd64" || "${arch_suffix}" == "arm64" ]]; then
1616
mkdir -p "${PROJECT_DIR}/bin/"
1717
echo "Saving kind to ${PROJECT_DIR}/bin"
1818
curl --retry 3 --silent -L "https://github.com/kubernetes-sigs/kind/releases/download/${latest_version}/kind-${os}-${arch_suffix}" -o kind

scripts/evergreen/setup_kubectl.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mv kubectl "${bindir}"
2222
echo "Downloading helm for ${ARCH}"
2323
helm_archive="${tmpdir}/helm.tgz"
2424
helm_version="v3.17.1"
25-
curl -s https://get.helm.sh/helm-${helm_version}-linux-${ARCH}.tar.gz --output "${helm_archive}"
25+
curl -s https://get.helm.sh/helm-${helm_version}-linux-"${ARCH}".tar.gz --output "${helm_archive}"
2626

2727
tar xfz "${helm_archive}" -C "${tmpdir}" &> /dev/null
2828
mv "${tmpdir}/linux-${ARCH}/helm" "${bindir}"

scripts/minikube/setup_minikube.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ setup_local_registry_and_custom_image() {
5050
registry_running=true
5151
fi
5252

53-
if ! $registry_running; then
53+
if ! ${registry_running}; then
5454
echo "Starting local container registry on port 5000..."
5555

5656
# Clean up any existing registry first

0 commit comments

Comments
 (0)