Skip to content

Commit 761b329

Browse files
authored
feat(chezmoi.io): Add feature. (#21)
1 parent 857ce32 commit 761b329

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

.github/workflows/test.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- atuin.sh
1818
- btop
1919
- bun.sh
20+
- chezmoi.io
2021
- deno.com
2122
- pkgx.sh
2223
- smallstep.com
@@ -47,6 +48,7 @@ jobs:
4748
- atuin.sh
4849
- btop
4950
- bun.sh
51+
- chezmoi.io
5052
- deno.com
5153
- pkgx.sh
5254
- smallstep.com
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "chezmoi.io",
3+
"id": "chezmoi.io",
4+
"version": "1.0.0",
5+
"description": "Install \"chezmoi\" binary",
6+
"documentationURL": "https://github.com/devcontainer-community/devcontainer-features/tree/main/src/chezmoi.io",
7+
"options": {
8+
"version": {
9+
"type": "string",
10+
"default": "latest",
11+
"proposals": [
12+
"latest"
13+
],
14+
"description": "Version of \"chezmoi\" to install."
15+
}
16+
}
17+
}

src/chezmoi.io/install.sh

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

test/chezmoi.io/test.sh

+18
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 "chezmoi --version | grep 'chezmoi'"
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)