Skip to content

Commit e0d1df5

Browse files
authored
Add run-script feature (#255)
1 parent 75ccaea commit e0d1df5

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
| [rclone](https://github.com/devcontainer-community/devcontainer-features/tree/main/src/rclone.org) | `rclone` — sync files to/from cloud storage | gh release | 1.0.1 |
7878
| [restic.net](https://github.com/devcontainer-community/devcontainer-features/tree/main/src/restic.net) | `restic` — fast, encrypted, deduplicated backups | gh release | 1.0.1 |
7979
| [ripgrep](https://github.com/devcontainer-community/devcontainer-features/tree/main/src/ripgrep) | `rg` — fast grep alternative (ripgrep) | gh release | 1.0.1 |
80+
| [run-script](https://github.com/devcontainer-community/devcontainer-features/tree/main/src/run-script) | Run a script from a URL or inline text during devcontainer build | custom | 1.0.0 |
8081
| [schpet/linear-cli](https://github.com/devcontainer-community/devcontainer-features/tree/main/src/schpet-linear-cli) | `linear` — CLI to access linear.com issue tracker | gh release | 1.0.2 |
8182
| [smallstep.com](https://github.com/devcontainer-community/devcontainer-features/tree/main/src/smallstep.com) | `step` — zero-trust security toolkit and CA | gh release | 1.0.2 |
8283
| [socket.dev/sfw-free](https://github.com/devcontainer-community/devcontainer-features/tree/main/src/socket.dev-sfw-free) | `sfw` — network security proxy that blocks malicious dependencies | gh release | 1.0.0 |

src/run-script/NOTES.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# run-script
2+
3+
## Project
4+
5+
_No upstream project — this is a utility feature._
6+
7+
## Description
8+
9+
A utility feature that runs a custom script during devcontainer build. Accepts either a URL pointing to a script to download and execute, or an inline script supplied directly as text. Exactly one of `url` or `script` must be provided.
10+
11+
## Installation Method
12+
13+
No binary is installed. The feature downloads (via `wget` or `curl`) or writes the provided script to a temporary file, then executes it with `bash`. If neither `wget` nor `curl` is available, `curl` is installed automatically via `apt`.
14+
15+
## Other Notes
16+
17+
- If both `url` and `script` are provided the feature fails with an error.
18+
- If neither option is provided the feature exits successfully without doing anything.
19+
- The temporary script file is removed after execution.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "run-script",
3+
"id": "run-script",
4+
"version": "1.0.0",
5+
"description": "Run a script from a URL or inline text during devcontainer build",
6+
"documentationURL": "https://github.com/devcontainer-community/devcontainer-features/tree/main/src/run-script",
7+
"options": {
8+
"url": {
9+
"type": "string",
10+
"default": "",
11+
"description": "URL of a script to download and execute."
12+
},
13+
"script": {
14+
"type": "string",
15+
"default": "",
16+
"description": "Inline script text to execute."
17+
}
18+
}
19+
}

src/run-script/install.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
set -o errexit
3+
set -o pipefail
4+
set -o noclobber
5+
set -o nounset
6+
set -o allexport
7+
readonly name="run-script"
8+
9+
apt_get_update() {
10+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
11+
echo "Running apt-get update..."
12+
apt-get update -y
13+
fi
14+
}
15+
16+
apt_get_checkinstall() {
17+
if ! dpkg -s "$@" >/dev/null 2>&1; then
18+
apt_get_update
19+
DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends --no-install-suggests --option 'Debug::pkgProblemResolver=true' --option 'Debug::pkgAcquire::Worker=1' "$@"
20+
fi
21+
}
22+
23+
apt_get_cleanup() {
24+
apt-get clean
25+
rm -rf /var/lib/apt/lists/*
26+
}
27+
28+
echo_banner() {
29+
local text="$1"
30+
echo -e "\e[1m\e[97m\e[41m$text\e[0m"
31+
}
32+
33+
install() {
34+
if [ -n "${URL}" ] && [ -n "${SCRIPT}" ]; then
35+
printf >&2 '=== [ERROR] Both "url" and "script" options are provided. Please provide only one.\n'
36+
exit 1
37+
fi
38+
39+
if [ -z "${URL}" ] && [ -z "${SCRIPT}" ]; then
40+
echo "No script URL or inline script provided. Nothing to do."
41+
return 0
42+
fi
43+
44+
local tmpScript
45+
tmpScript="$(mktemp)"
46+
trap 'rm -f "${tmpScript}"' EXIT
47+
48+
if [ -n "${URL}" ]; then
49+
echo "Downloading script from: ${URL}"
50+
if command -v wget >/dev/null 2>&1; then
51+
wget -qO "${tmpScript}" "${URL}"
52+
elif command -v curl >/dev/null 2>&1; then
53+
curl -fsSL -o "${tmpScript}" "${URL}"
54+
else
55+
apt_get_checkinstall curl ca-certificates
56+
apt_get_cleanup
57+
curl -fsSL -o "${tmpScript}" "${URL}"
58+
fi
59+
else
60+
echo "Writing inline script to temporary file..."
61+
printf '%s' "${SCRIPT}" | tee "${tmpScript}" >/dev/null
62+
fi
63+
64+
echo "Executing script..."
65+
bash "${tmpScript}"
66+
}
67+
68+
echo_banner "devcontainer.community"
69+
echo "Running $name..."
70+
install "$@"
71+
echo "(*) Done!"

test/run-script/test.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
4+
set -e
5+
6+
# Optional: Import test library bundled with the devcontainer CLI
7+
# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib
8+
# Provides the 'check' and 'reportResults' commands.
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 "bash is available" bash -c "bash --version"
15+
16+
# Report results
17+
# If any of the checks above exited with a non-zero exit code, the test will fail.
18+
reportResults

0 commit comments

Comments
 (0)