Skip to content

Commit e08cc97

Browse files
Copilotsebst
andauthored
Add k9scli.io feature (#118)
* Initial plan * Add k9scli.io devcontainer feature Co-authored-by: sebst <592313+sebst@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sebst <592313+sebst@users.noreply.github.com>
1 parent 59f716e commit e08cc97

4 files changed

Lines changed: 198 additions & 0 deletions

File tree

‎.github/workflows/test.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
- feature-installer
3131
- helix-editor.com
3232
- jj-vcs.dev
33+
- k9scli.io
3334
- mosh.org
3435
- keybase.io
3536
- neovim.io
@@ -83,6 +84,7 @@ jobs:
8384
- feature-installer
8485
- helix-editor.com
8586
- jj-vcs.dev
87+
- k9scli.io
8688
- mosh.org
8789
- keybase.io
8890
- neovim.io
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "k9scli.io",
3+
"id": "k9scli.io",
4+
"version": "1.0.0",
5+
"description": "Install \"k9s\" binary",
6+
"documentationURL": "https://github.com/devcontainer-community/devcontainer-features/tree/main/src/k9scli.io",
7+
"options": {
8+
"version": {
9+
"type": "string",
10+
"default": "latest",
11+
"proposals": [
12+
"latest"
13+
],
14+
"description": "Version of \"k9s\" to install."
15+
}
16+
}
17+
}

‎src/k9scli.io/install.sh‎

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

‎test/k9scli.io/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 "k9s version | grep '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)