Skip to content

Commit f2cc875

Browse files
Add ShellCheck feature (#3)
Most useful for… this project!
2 parents 40a439e + c960555 commit f2cc875

File tree

9 files changed

+218
-0
lines changed

9 files changed

+218
-0
lines changed

.github/workflows/test.yml

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
matrix:
1414
features:
1515
- actionlint
16+
- postgresql-client
17+
- shellcheck
1618
baseImage:
1719
- debian:latest
1820
- ubuntu:latest
@@ -29,6 +31,8 @@ jobs:
2931
matrix:
3032
features:
3133
- actionlint
34+
- postgresql-client
35+
- shellcheck
3236
continue-on-error: true
3337
steps:
3438
- uses: actions/checkout@v4

.shellcheckrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Enable all optional checks
2+
enable=all
3+
4+
# Follow source statements even when the file is not specified as input
5+
external-sources=true
6+
7+
# Disable check-extra-masked-returns
8+
# https://www.shellcheck.net/wiki/SC2312
9+
disable=SC2312

src/shellcheck/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# shellcheck
2+
3+
Install [ShellCheck](https://www.shellcheck.net), a static analysis tool for shell scripts.
4+
5+
## Usage
6+
7+
```json
8+
"features": {
9+
"ghcr.io/CargoSense/devcontainer-features/shellcheck:1": {}
10+
}
11+
```
12+
13+
## Options
14+
15+
| Option ID | Description | Type | Default Value |
16+
|:--------------|:---------------------------------------------|:-------|:-----------------|
17+
| `version` | The ShellCheck version to install. | string | `os-provided` |
18+
| `installPath` | The path where ShellCheck will be installed. | string | `/usr/local/bin` |
19+
20+
## OS Support
21+
22+
This Feature should work on recent versions of Debian/Ubuntu and Linux distributions using the [apt](https://wiki.debian.org/AptCLI) management tool.
23+
24+
`bash` is required to execute the `install.sh` script.
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "ShellCheck",
3+
"id": "shellcheck",
4+
"version": "1.0.0",
5+
"description": "Install ShellCheck, a static analysis tool for shell scripts.",
6+
"options": {
7+
"version": {
8+
"type": "string",
9+
"proposals": [
10+
"latest",
11+
"os-provided"
12+
],
13+
"default": "os-provided",
14+
"description": "Select or enter a ShellCheck version."
15+
},
16+
"installPath": {
17+
"type": "string",
18+
"default": "/usr/local/bin",
19+
"description": "The path where ShellCheck will be installed."
20+
}
21+
},
22+
"installsAfter": [
23+
"ghcr.io/devcontainers/features/common-utils"
24+
]
25+
}

src/shellcheck/install.sh

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env sh
2+
3+
set -e
4+
5+
SHELLCHECK_VERSION="${VERSION:-"os-provided"}"
6+
INSTALL_PATH="${INSTALLPATH:-"/usr/local/bin"}"
7+
8+
if [ "$(id -u)" -ne 0 ]; then
9+
printf 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
10+
exit 1
11+
fi
12+
13+
14+
if [ "${SHELLCHECK_VERSION}" = "os-provided" ]; then
15+
apt update --yes
16+
apt install --no-install-recommends --yes shellcheck
17+
18+
exit 0
19+
fi
20+
21+
curl_installed=""
22+
23+
if ! type curl >/dev/null 2>&1; then
24+
apt update --yes
25+
apt install --no-install-recommends --yes curl ca-certificates xz-utils
26+
27+
curl_installed="true"
28+
fi
29+
30+
if [ "${SHELLCHECK_VERSION}" = "latest" ]; then
31+
SHELLCHECK_VERSION="$(curl -s --head https://github.com/koalaman/shellcheck/releases/latest | sed -nr 's/location:.*\/v(.+)/\1/ip' | tr -d '\r')"
32+
fi
33+
34+
machine="$(uname -m)"
35+
case "${machine}" in
36+
aarch64) arch="aarch64" ;;
37+
arm*) arch="armv6" ;;
38+
x86_64) arch="x86_64" ;;
39+
*)
40+
echo "Could not determine arch from machine hardware name '${machine}'" >&2
41+
exit 1
42+
;;
43+
esac
44+
45+
# https://github.com/koalaman/shellcheck/releases/download/v0.9.0/shellcheck-v0.9.0.linux.aarch64.tar.xz
46+
file="shellcheck-v${SHELLCHECK_VERSION}.linux.${arch}.tar.xz"
47+
url="https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/${file}"
48+
49+
curl -sSL "${url}" | tar --strip-components=1 -Jxvf - -C "${INSTALL_PATH}" "shellcheck-v${SHELLCHECK_VERSION}/shellcheck"
50+
51+
if [ -n "${curl_installed}" ]; then
52+
apt purge curl xz-utils --autoremove --yes
53+
fi

test/shellcheck/scenarios.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"shellcheck-version": {
3+
"image": "debian:latest",
4+
"features": {
5+
"shellcheck": {
6+
"version": "0.8.0"
7+
}
8+
}
9+
},
10+
11+
"shellcheck-install-path": {
12+
"image": "debian:latest",
13+
"features": {
14+
"shellcheck": {
15+
"installPath": "/usr/bin"
16+
}
17+
}
18+
}
19+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# Optional: Import test library bundled with the devcontainer CLI
6+
# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib
7+
# Provides the 'check' and 'reportResults' commands.
8+
# shellcheck source=/dev/null
9+
source dev-container-features-test-lib
10+
11+
# Feature-specific tests
12+
# The 'check' command comes from the dev-container-features-test-lib. Syntax is...
13+
# check <LABEL> <cmd> [args...]
14+
check "which shellcheck" bash -c "which shellcheck | grep /usr/bin/shellcheck"
15+
16+
# Report result
17+
# If any of the checks above exited with a non-zero exit code, the test will fail.
18+
reportResults

test/shellcheck/shellcheck-version.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# Optional: Import test library bundled with the devcontainer CLI
6+
# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib
7+
# Provides the 'check' and 'reportResults' commands.
8+
# shellcheck source=/dev/null
9+
source dev-container-features-test-lib
10+
11+
# Feature-specific tests
12+
# The 'check' command comes from the dev-container-features-test-lib. Syntax is...
13+
# check <LABEL> <cmd> [args...]
14+
check "version" bash -c "shellcheck --version | grep 0.8.0"
15+
16+
# Report result
17+
# If any of the checks above exited with a non-zero exit code, the test will fail.
18+
reportResults

test/shellcheck/test.sh

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
3+
# This test file will be executed against an auto-generated devcontainer.json that
4+
# includes the 'shellcheck' Feature with no options.
5+
#
6+
# For more information, see: https://github.com/devcontainers/cli/blob/main/docs/features/test.md
7+
#
8+
# Eg:
9+
# {
10+
# "image": "<..some-base-image...>",
11+
# "features": {
12+
# "shellcheck": {}
13+
# },
14+
# "remoteUser": "root"
15+
# }
16+
#
17+
# Thus, the value of all options will fall back to the default value in the
18+
# Feature's 'devcontainer-feature.json'.
19+
#
20+
# These scripts are run as 'root' by default. Although that can be changed
21+
# with the '--remote-user' flag.
22+
#
23+
# This test can be run with the following command:
24+
#
25+
# devcontainer features test \
26+
# --features shellcheck \
27+
# --remote-user root \
28+
# --skip-scenarios \
29+
# --base-image mcr.microsoft.com/devcontainers/base:ubuntu \
30+
# /path/to/this/repo
31+
32+
set -e
33+
34+
# Optional: Import test library bundled with the devcontainer CLI
35+
# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib
36+
# Provides the 'check' and 'reportResults' commands.
37+
# shellcheck source=/dev/null
38+
source dev-container-features-test-lib
39+
40+
# Feature-specific tests
41+
# The 'check' command comes from the dev-container-features-test-lib. Syntax is...
42+
# check <LABEL> <cmd> [args...]
43+
check "version" shellcheck --version
44+
check "which shellcheck" bash -c "which shellcheck | grep /usr/bin/shellcheck"
45+
46+
# Report result
47+
# If any of the checks above exited with a non-zero exit code, the test will fail.
48+
reportResults

0 commit comments

Comments
 (0)