Skip to content

Commit c17061b

Browse files
authored
feat: check scripts with shellcheck (#70)
1 parent db135c5 commit c17061b

17 files changed

Lines changed: 71 additions & 40 deletions

File tree

.devcontainer/post_create_command.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ npm install -g @devcontainers/cli
33
pre-commit install
44

55
scripts/create_builder.sh
6+
7+
sudo apt-get update && sudo apt-get install -y shellcheck

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ repos:
1515
hooks:
1616
- id: yamlfmt
1717
args: [--mapping, '2', --offset, '2', --sequence, '4']
18+
19+
- repo: https://github.com/jumanjihouse/pre-commit-hooks
20+
rev: 38980559e3a605691d6579f96222c30778e5a69e # 3.0.0
21+
hooks:
22+
- id: shellcheck

.shellcheckrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
external-sources=true
2+
3+
# Enable all checks as long as it works
4+
# This might create surprises when updating shellcheck and it adds new checks.
5+
# If this becomes annoying we have seletively enable checks
6+
enable=all
7+
8+
# required checks, no reason not to fix them except difficulty and fear to break code
9+
disable=SC2046
10+
11+
# optional checks, fixes might not be easy and better not break the code
12+
disable=SC2292,SC2154,SC2312

scripts/build.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -euxo pipefail
33

4-
if [[ "$#" -lt 1 || "$1" != "--arm64" && "$1" != "--amd64" ]]; then
4+
if [[ "$#" -lt 1 || "${1}" != "--arm64" && "${1}" != "--amd64" ]]; then
55
echo "Error: First parameter must be --arm64 or --amd64."
66
exit 1
77
fi
@@ -11,11 +11,11 @@ if [ "$#" -lt 2 ]; then
1111
exit 1
1212
fi
1313

14-
ARCH_OPTION="$1"
14+
ARCH_OPTION="${1}"
1515
shift
1616

1717
ARCH="amd64"
18-
if [[ "$ARCH_OPTION" == "--arm64" ]]; then
18+
if [[ "${ARCH_OPTION}" == "--arm64" ]]; then
1919
ARCH="arm64"
2020
fi
2121

@@ -24,7 +24,7 @@ for LABEL in "$@"; do
2424
LABELS+=("${LABEL}")
2525
done
2626

27-
echo "Building all labels (${LABELS[@]}) for architecture: ${ARCH}"
27+
echo "Building all labels (" "${LABELS[@]}" ") for architecture: ${ARCH}"
2828

2929
# Prepare image names with tags (each tag includes a label and the architecture)
3030
IMAGES=()
@@ -37,8 +37,8 @@ DEVCONTAINER_CALL="devcontainer build --workspace-folder src/s-core-devcontainer
3737

3838
# Append image names to the build command
3939
for IMAGE in "${IMAGES[@]}"; do
40-
DEVCONTAINER_CALL+=" $IMAGE"
40+
DEVCONTAINER_CALL+=" ${IMAGE}"
4141
done
4242

4343
# Execute the build for the specific architecture
44-
eval "$DEVCONTAINER_CALL --platform linux/${ARCH}"
44+
eval "${DEVCONTAINER_CALL} --platform linux/${ARCH}"

scripts/create_builder.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ check_proxy_config() {
88

99
# Check if HTTP_PROXY is set in environment but not in builder
1010
if [ -n "${HTTP_PROXY:-}" ]; then
11-
if ! echo "$builder_info" | grep -q "HTTP_PROXY=${HTTP_PROXY}"; then
11+
if ! echo "${builder_info}" | grep -q "HTTP_PROXY=${HTTP_PROXY}"; then
1212
return 1
1313
fi
1414
fi
1515

1616
# Check if HTTPS_PROXY is set in environment but not in builder
1717
if [ -n "${HTTPS_PROXY:-}" ]; then
18-
if ! echo "$builder_info" | grep -q "HTTPS_PROXY=${HTTPS_PROXY}"; then
18+
if ! echo "${builder_info}" | grep -q "HTTPS_PROXY=${HTTPS_PROXY}"; then
1919
return 1
2020
fi
2121
fi
@@ -25,6 +25,8 @@ check_proxy_config() {
2525

2626
# Check if builder exists and has correct proxy configuration
2727
if docker buildx inspect multiarch &>/dev/null; then
28+
# shellcheck disable=SC2310
29+
# it is an optional rule, enabled via --enable all
2830
if ! check_proxy_config; then
2931
echo "Builder 'multiarch' exists but has incorrect proxy configuration. Recreating..."
3032
docker buildx rm multiarch

scripts/merge.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ done
2323

2424
# Create and push the merged multiarch manifest for each tag; each tag combines all architecture-specific tags into one tag
2525
for LABEL in "${LABELS[@]}"; do
26-
echo "Merging all architectures (${ARCHITECTURES[@]}) into single tag: ${LABEL}"
26+
echo "Merging all architectures (" "${ARCHITECTURES[@]}" ") into single tag: ${LABEL}"
2727

2828
MANIFEST_MERGE_CALL="docker buildx imagetools create -t ghcr.io/eclipse-score/devcontainer:${LABEL}"
2929

3030
for ARCH in "${ARCHITECTURES[@]}"; do
3131
MANIFEST_MERGE_CALL+=" ghcr.io/eclipse-score/devcontainer:${LABEL}-${ARCH}"
3232
done
3333

34-
eval "$MANIFEST_MERGE_CALL"
34+
eval "${MANIFEST_MERGE_CALL}"
3535
done

scripts/publish.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -euxo pipefail
33

4-
if [[ "$#" -lt 1 || "$1" != "--arm64" && "$1" != "--amd64" ]]; then
4+
if [[ "$#" -lt 1 || "${1}" != "--arm64" && "${1}" != "--amd64" ]]; then
55
echo "Error: First parameter must be --arm64 or --amd64."
66
exit 1
77
fi
@@ -11,11 +11,11 @@ if [ "$#" -lt 2 ]; then
1111
exit 1
1212
fi
1313

14-
ARCH_OPTION="$1"
14+
ARCH_OPTION="${1}"
1515
shift
1616

1717
ARCH="amd64"
18-
if [[ "$ARCH_OPTION" == "--arm64" ]]; then
18+
if [[ "${ARCH_OPTION}" == "--arm64" ]]; then
1919
ARCH="arm64"
2020
fi
2121

@@ -24,7 +24,7 @@ for LABEL in "$@"; do
2424
LABELS+=("${LABEL}")
2525
done
2626

27-
echo "Building all tags (${LABELS[@]}) for architecture: ${ARCH}"
27+
echo "Building all tags (" "${LABELS[@]}" ") for architecture: ${ARCH}"
2828
# Prepare image names with tags (each tag includes a label and an architecture)
2929
IMAGES=()
3030
for LABEL in "${LABELS[@]}"; do
@@ -36,8 +36,8 @@ DEVCONTAINER_CALL="devcontainer build --push --workspace-folder src/s-core-devco
3636

3737
# Append image names to the build command
3838
for IMAGE in "${IMAGES[@]}"; do
39-
DEVCONTAINER_CALL+=" $IMAGE"
39+
DEVCONTAINER_CALL+=" ${IMAGE}"
4040
done
4141

4242
# Execute the build and push all tags for the specific architecture
43-
eval "$DEVCONTAINER_CALL --platform linux/${ARCH}"
43+
eval "${DEVCONTAINER_CALL} --platform linux/${ARCH}"

src/s-core-devcontainer/.devcontainer/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ LABEL dev.containers.features="common"
2020

2121
# Unset proxy variables for all login shells
2222
COPY unset-proxy.sh /etc/profile.d/unset-proxy.sh
23-
RUN chmod +x /etc/profile.d/unset-proxy.sh
2423

2524
RUN userdel -f -r ubuntu

src/s-core-devcontainer/.devcontainer/bazel-feature/install.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ COPY_TARGET="${FEATURES_DIR}/$(basename "${SCRIPT_DIR%%_*}")"
1010
cp -R "${SCRIPT_DIR}" "${COPY_TARGET}"
1111
rm -f "${COPY_TARGET}/devcontainer-features.env" "${COPY_TARGET}/devcontainer-features-install.sh"
1212

13+
# shellcheck disable=SC2034
14+
# used by apt-get only inside this script
1315
DEBIAN_FRONTEND=noninteractive
1416

1517
# Read tool versions + metadata into environment variables
@@ -34,7 +36,7 @@ if [ "${ARCHITECTURE}" = "arm64" ]; then
3436
SHA256SUM="${bazelisk_arm64_sha256}"
3537
fi
3638
curl -L "https://github.com/bazelbuild/bazelisk/releases/download/v${bazelisk_version}/bazelisk-${BAZELISK_VARIANT}.deb" -o /tmp/bazelisk.deb
37-
echo "${SHA256SUM} /tmp/bazelisk.deb" | sha256sum -c - || exit -1
39+
echo "${SHA256SUM} /tmp/bazelisk.deb" | sha256sum -c - || exit 1
3840
apt-get install -y --no-install-recommends --fix-broken /tmp/bazelisk.deb
3941
rm /tmp/bazelisk.deb
4042

@@ -60,7 +62,7 @@ if [ "${ARCHITECTURE}" = "arm64" ]; then
6062
SHA256SUM="${buildifier_arm64_sha256}"
6163
fi
6264
curl -L "https://github.com/bazelbuild/buildtools/releases/download/v${buildifier_version}/buildifier-linux-${BUILDIFIER_VARIANT}" -o /usr/local/bin/buildifier
63-
echo "${SHA256SUM} /usr/local/bin/buildifier" | sha256sum -c - || exit -1
65+
echo "${SHA256SUM} /usr/local/bin/buildifier" | sha256sum -c - || exit 1
6466
chmod +x /usr/local/bin/buildifier
6567

6668
# Starlark Language Server, directly from GitHub (apparently no APT repository available)
@@ -71,7 +73,7 @@ if [ "${ARCHITECTURE}" = "arm64" ]; then
7173
SHA256SUM="${starpls_arm64_sha256}"
7274
fi
7375
curl -L "https://github.com/withered-magic/starpls/releases/download/v${starpls_version}/starpls-linux-${STARPLS_VARIANT}" -o /usr/local/bin/starpls
74-
echo "${SHA256SUM} /usr/local/bin/starpls" | sha256sum -c - || exit -1
76+
echo "${SHA256SUM} /usr/local/bin/starpls" | sha256sum -c - || exit 1
7577
chmod +x /usr/local/bin/starpls
7678

7779
# Code completion for C++ code of Bazel projects
@@ -83,7 +85,7 @@ SHA256SUM="${bazel_compile_commands_amd64_sha256}"
8385
if [ "${ARCHITECTURE}" = "arm64" ]; then
8486
SHA256SUM="${bazel_compile_commands_arm64_sha256}"
8587
fi
86-
echo "${SHA256SUM} /tmp/bazel-compile-commands.deb" | sha256sum -c - || exit -1
88+
echo "${SHA256SUM} /tmp/bazel-compile-commands.deb" | sha256sum -c - || exit 1
8789
apt-get install -y --no-install-recommends --fix-broken /tmp/bazel-compile-commands.deb
8890
rm /tmp/bazel-compile-commands.deb
8991

src/s-core-devcontainer/.devcontainer/bazel-feature/install_matching_bazel_version.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ set -eo pipefail
33

44
. /devcontainer/features/bazel/bazel_setup.sh || true
55

6-
if [ -f .bazelversion ] && [ "$(cat .bazelversion)" != "$INSTALLED_BAZEL_VERSION" ]; then
6+
if [ -f .bazelversion ] && [ "$(cat .bazelversion)" != "${INSTALLED_BAZEL_VERSION}" ]; then
77
# Pre-install the matching Bazel version, setup the bash command completion
88
USE_BAZEL_VERSION=$(cat .bazelversion)
99

1010
min_bazel_version_for_bash_option="8.4.0"
1111
bash=""
12-
if [ "$(printf '%s\n' "$min_bazel_version_for_bash_option" "$USE_BAZEL_VERSION" | sort -V | head -n1)" = "$min_bazel_version_for_bash_option" ]; then
12+
if [ "$(printf '%s\n' "${min_bazel_version_for_bash_option}" "${USE_BAZEL_VERSION}" | sort -V | head -n1)" = "${min_bazel_version_for_bash_option}" ]; then
1313
bash="bash"
1414
fi
1515

16+
# shellcheck disable=SC2248
17+
# without quotes is intentional: $bash might be empty and then an empty string is passed to the command, which will fail
1618
bazel help completion ${bash} > /tmp/bazel-complete.bash
1719
sudo mv /tmp/bazel-complete.bash /etc/bash_completion.d/bazel-complete.bash
18-
echo "INSTALLED_BAZEL_VERSION=$USE_BAZEL_VERSION" | sudo tee /devcontainer/features/bazel/bazel_setup.sh
20+
echo "INSTALLED_BAZEL_VERSION=${USE_BAZEL_VERSION}" | sudo tee /devcontainer/features/bazel/bazel_setup.sh
1921
fi

0 commit comments

Comments
 (0)