Skip to content

Commit 51bc783

Browse files
authored
feat: Add charmebracelet-gum feature (#66)
1 parent 3b2213c commit 51bc783

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- atuin.sh
1818
- btop
1919
- bun.sh
20+
- charmbracelet-gum
2021
- chezmoi.io
2122
- deno.com
2223
- feature-installer
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "charmbracelet/gum",
3+
"id": "gum",
4+
"version": "1.0.0",
5+
"description": "Install \"gum\" binary",
6+
"documentationURL": "https://github.com/devcontainer-community/devcontainer-features/tree/main/src/charmbracelet-gum",
7+
"options": {
8+
"version": {
9+
"type": "string",
10+
"default": "latest",
11+
"proposals": [
12+
"latest"
13+
],
14+
"description": "Version of \"gum\" to install."
15+
}
16+
}
17+
}

src/charmbracelet-gum/install.sh

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 githubRepository='charmbracelet/gum'
8+
readonly binaryName='gum'
9+
readonly versionArgument='--version'
10+
readonly downloadUrlTemplate='https://github.com/${githubRepository}/releases/download/v${version}/${name}_${version}_Linux_${architecture}.tar.gz'
11+
readonly binaryPathInArchiveTemplate='${name}_${version}_Linux_${architecture}/${binaryName}'
12+
readonly binaryTargetFolder='/usr/local/bin'
13+
readonly name="${githubRepository##*/}"
14+
apt_get_update() {
15+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
16+
echo "Running apt-get update..."
17+
apt-get update -y
18+
fi
19+
}
20+
apt_get_checkinstall() {
21+
if ! dpkg -s "$@" >/dev/null 2>&1; then
22+
apt_get_update
23+
DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends --no-install-suggests --option 'Debug::pkgProblemResolver=true' --option 'Debug::pkgAcquire::Worker=1' "$@"
24+
fi
25+
}
26+
apt_get_cleanup() {
27+
apt-get clean
28+
rm -rf /var/lib/apt/lists/*
29+
}
30+
check_curl_envsubst_file_tar_installed() {
31+
declare -a requiredAptPackagesMissing=()
32+
if ! [ -r '/etc/ssl/certs/ca-certificates.crt' ]; then
33+
requiredAptPackagesMissing+=('ca-certificates')
34+
fi
35+
if ! command -v curl >/dev/null 2>&1; then
36+
requiredAptPackagesMissing+=('curl')
37+
fi
38+
if ! command -v envsubst >/dev/null 2>&1; then
39+
requiredAptPackagesMissing+=('gettext-base')
40+
fi
41+
if ! command -v file >/dev/null 2>&1; then
42+
requiredAptPackagesMissing+=('file')
43+
fi
44+
if ! command -v tar >/dev/null 2>&1; then
45+
requiredAptPackagesMissing+=('tar')
46+
fi
47+
declare -i requiredAptPackagesMissingCount=${#requiredAptPackagesMissing[@]}
48+
if [ $requiredAptPackagesMissingCount -gt 0 ]; then
49+
apt_get_update
50+
apt_get_checkinstall "${requiredAptPackagesMissing[@]}"
51+
apt_get_cleanup
52+
fi
53+
}
54+
curl_check_url() {
55+
local url=$1
56+
local status_code
57+
status_code=$(curl -s -o /dev/null -w '%{http_code}' "$url")
58+
if [ "$status_code" -ne 200 ] && [ "$status_code" -ne 302 ]; then
59+
echo "Failed to download '$url'. Status code: $status_code."
60+
return 1
61+
fi
62+
}
63+
curl_download_stdout() {
64+
local url=$1
65+
curl \
66+
--silent \
67+
--location \
68+
--output '-' \
69+
--connect-timeout 5 \
70+
"$url"
71+
}
72+
curl_download_untar() {
73+
local url=$1
74+
local strip=$2
75+
local target=$3
76+
local bin_path=$4
77+
curl_download_stdout "$url" | tar \
78+
-xz \
79+
-f '-' \
80+
--strip-components="$strip" \
81+
-C "$target" \
82+
"$bin_path"
83+
}
84+
debian_get_arch() {
85+
arch=$(uname -m)
86+
# if [[ "$arch" == "aarch64" ]]; then
87+
# arch="arm64"
88+
# elif [[ "$arch" == "x86_64" ]]; then
89+
# arch="x64"
90+
# fi
91+
echo "$arch"
92+
# echo "$(dpkg --print-architecture)" --- IGNORE ---
93+
}
94+
echo_banner() {
95+
local text="$1"
96+
echo -e "\e[1m\e[97m\e[41m$text\e[0m"
97+
}
98+
github_list_releases() {
99+
if [ -z "$1" ]; then
100+
echo "Usage: list_github_releases <owner/repo>"
101+
return 1
102+
fi
103+
local repo="$1"
104+
local url="https://api.github.com/repos/$repo/releases"
105+
curl -s "$url" | grep -Po '"tag_name": "\K.*?(?=")' | grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' | sed 's/^v//'
106+
}
107+
github_get_latest_release() {
108+
if [ -z "$1" ]; then
109+
echo "Usage: get_latest_github_release <owner/repo>"
110+
return 1
111+
fi
112+
github_list_releases "$1" | head -n 1
113+
}
114+
utils_check_version() {
115+
local version=$1
116+
if ! [[ "${version:-}" =~ ^(latest|[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
117+
printf >&2 '=== [ERROR] Option "version" (value: "%s") is not "latest" or valid semantic version format "X.Y.Z" !\n' \
118+
"$version"
119+
exit 1
120+
fi
121+
}
122+
install() {
123+
utils_check_version "$VERSION"
124+
check_curl_envsubst_file_tar_installed
125+
readonly architecture="$(debian_get_arch)"
126+
readonly binaryTargetPathTemplate='${binaryTargetFolder}/${binaryName}'
127+
if [ "$VERSION" == 'latest' ] || [ -z "$VERSION" ]; then
128+
VERSION=$(github_get_latest_release "$githubRepository")
129+
fi
130+
readonly version="${VERSION:?}"
131+
readonly downloadUrl="$(echo -n "$downloadUrlTemplate" | envsubst)"
132+
curl_check_url "$downloadUrl"
133+
readonly binaryPathInArchive="$(echo -n "$binaryPathInArchiveTemplate" | envsubst)"
134+
readonly stripComponents="$(echo -n "$binaryPathInArchive" | awk -F'/' '{print NF-1}')"
135+
readonly binaryTargetPath="$(echo -n "$binaryTargetPathTemplate" | envsubst)"
136+
curl_download_untar "$downloadUrl" "$stripComponents" "$binaryTargetFolder" "$binaryPathInArchive"
137+
chmod 755 "$binaryTargetPath"
138+
apt_get_cleanup
139+
}
140+
echo_banner "devcontainer.community"
141+
echo "Installing $name..."
142+
install "$@"
143+
echo "(*) Done!"

test/charmbracelet-gum/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 "execute command" bash -c "gum --version | grep 'gum'"
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)