Skip to content

Commit 4263f0a

Browse files
committed
feat(go-air): add go-air feature for installing air-verse/air
1 parent 4f4021a commit 4263f0a

9 files changed

Lines changed: 141 additions & 8 deletions

File tree

src/go-air/NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## OS Support
2+
3+
This Feature should work on recent versions of Debian/Ubuntu-based distributions with the `apt` package manager installed.
4+
5+
`bash` is required to execute the `install.sh` script.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "Air",
3+
"id": "go-air",
4+
"version": "1.0.0",
5+
"documentationURL": "http://github.com/omoxyz/devcontainer-features/tree/main/src/go-air",
6+
"description": "Install Air live reloader for Go apps.",
7+
"options": {
8+
"version": {
9+
"default": "latest",
10+
"description": "Select the version to install.",
11+
"proposals": [
12+
"latest"
13+
],
14+
"type": "string"
15+
}
16+
},
17+
"dependsOn": {
18+
"ghcr.io/devcontainers/features/go": {}
19+
}
20+
}

src/go-air/install.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
source ./utils.sh
4+
5+
AIR_VERSION=${VERSION:-"latest"}
6+
GITHUB_REPO=https://github.com/air-verse/air/
7+
8+
# Exit immediately if a command exits with a non-zero status.
9+
set -e
10+
11+
export DEBIAN_FRONTEND=noninteractive
12+
13+
version_list=$(git ls-remote --tags ${GITHUB_REPO})
14+
15+
versions=($(find_latest_versions $AIR_VERSION version_list "tags/v"))
16+
if [ $? -eq 1 ]; then
17+
echo "Can't find appropriate version"
18+
exit 1
19+
fi
20+
21+
latest_version=${versions[0]}
22+
prev_version=${versions[1]}
23+
24+
echo "Downloading air v${latest_version}...."
25+
26+
set +e
27+
28+
go install github.com/air-verse/air@v${latest_version}
29+
exit_code=$?
30+
31+
set -e
32+
33+
if [ "$exit_code" != "0" ]; then
34+
# Handle situation where git tags are ahead of what was is available to actually download
35+
echo "(!) air version ${latest_version} failed to download. Attempting to fall back to ${prev_version} to retry..."
36+
go install github.com/air-verse/air@v${prev_version}
37+
fi

src/go-air/utils.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
3+
# Find 2 latest versions that appropriate to requested version
4+
find_latest_versions() {
5+
local requested_version=$1
6+
local version_list=${!2}
7+
8+
# Version prefix such as "tags/v"
9+
local prefix_regex=${3:-''}
10+
11+
# Version number part separator such as "." in "1.0.0"
12+
local separator=${4:-"."}
13+
local escaped_separator=${separator//./\\.}
14+
15+
local suffix_regex=${5:-''}
16+
17+
# Format and sort version list
18+
local version_regex="${prefix_regex}\\K[0-9]+(${escaped_separator}[0-9]+){0,2}${suffix_regex}$"
19+
version_list="$(printf "%s\n" "${version_list[@]}" | grep -oP $version_regex| tr -d ' ' | tr $separator '.' | sort -rV)"
20+
21+
if [ "${requested_version}" = "latest" ]; then
22+
echo "$(echo "${version_list}" | head -n 2)"
23+
else
24+
# Try to get latest matching version
25+
26+
set +e
27+
local regex="^"
28+
29+
# Get major version or exit
30+
local major="$(echo "${requested_version}" | grep -oE '^[0-9]+')"
31+
if [ $major != '' ]; then
32+
regex="${regex}${major}"
33+
else
34+
echo "Invalid version \"${requested_version}\". Use \"latest\" or MAJOR[.MINOR][.PATCH]"
35+
return 1
36+
fi
37+
38+
# Get minor number or accept any
39+
local minor="$(echo "${requested_version}" | grep -oP '^[0-9]+\.\K[0-9]+')"
40+
regex="${regex}$([ "$minor" != '' ] && echo "${escaped_separator}${minor}" || echo "(${escaped_separator}[0-9]+)?")"
41+
42+
43+
# Get patch number or accept any
44+
local patch="$(echo "${requested_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+')"
45+
regex="${regex}$([ "$patch" != '' ] && echo "${escaped_separator}${patch}" || echo "(${escaped_separator}[0-9]+)?")"
46+
set -e
47+
48+
echo "$(echo "${version_list}" | grep -E -m 2 "^${regex}$")"
49+
fi
50+
}

src/lefthook/devcontainer-feature.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Lefthook",
33
"id": "lefthook",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"documentationURL": "http://github.com/omoxyz/devcontainer-features/tree/main/src/lefthook",
66
"description": "Install Lefthook fast polyglot Git hooks manager.",
77
"options": {
@@ -15,6 +15,7 @@
1515
},
1616
"installDirectlyFromGitHubRelease": {
1717
"type": "boolean",
18+
"description": "Install directly from Github release or from package manager (apt).",
1819
"default": true
1920
}
2021
}

src/lefthook/install.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/bash
1+
#!/usr/bin/env bash
22

33
source ./utils.sh
44

@@ -30,15 +30,15 @@ get_github_filename() {
3030
install_from_github() {
3131
local version_list=$(git ls-remote --tags ${GITHUB_REPO})
3232

33-
versions=($(find_latest_version $LEFTHOOK_VERSION version_list "tags/v"))
34-
latest_version=${versions[0]}
35-
prev_version=${versions[1]}
36-
33+
versions=($(find_latest_versions $LEFTHOOK_VERSION version_list "tags/v"))
3734
if [ $? -eq 1 ]; then
3835
echo "Can't find appropriate version"
3936
exit 1
4037
fi
4138

39+
latest_version=${versions[0]}
40+
prev_version=${versions[1]}
41+
4242
echo "Downloading lefthook v${latest_version}...."
4343

4444
check_packages wget
@@ -68,7 +68,7 @@ install_from_package_manager() {
6868
curl -1sLf 'https://dl.cloudsmith.io/public/evilmartians/lefthook/setup.deb.sh' | bash
6969

7070
local version_list=$(get_apt_versions lefthook)
71-
versions=($(find_latest_version $LEFTHOOK_VERSION version_list))
71+
versions=($(find_latest_versions $LEFTHOOK_VERSION version_list))
7272
latest_version=${versions[0]}
7373

7474
if [ $? -eq 1 ]; then

src/lefthook/utils.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env bash
2+
13
# Refresh the local package index if no package list entries are stored on the system.
24
apt_get_update() {
35
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
@@ -15,7 +17,8 @@ check_packages() {
1517
fi
1618
}
1719

18-
find_latest_version() {
20+
# Find 2 latest versions that appropriate to requested version
21+
find_latest_versions() {
1922
local requested_version=$1
2023
local version_list=${!2}
2124

test/go-air/scenarios.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"test": {
3+
"image": "mcr.microsoft.com/devcontainers/base:debian",
4+
"features": {
5+
"go-air": {}
6+
}
7+
}
8+
}

test/go-air/test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
check "air version" air -v
8+
9+
reportResults

0 commit comments

Comments
 (0)