11#! /usr/bin/env bash
22set -euo pipefail
3+
34APP=feature-installer
45
6+ # Color definitions
57RED=' \033[0;31m'
68GREEN=' \033[0;32m'
79YELLOW=' \033[1;33m'
@@ -10,33 +12,36 @@ NC='\033[0m' # No Color
1012
1113requested_version=${VERSION:- }
1214
13- os=$( uname -s | tr ' [:upper:]' ' [:upper:]' )
15+ # Detect OS and architecture
16+ os=$( uname -s | tr ' [:upper:]' ' [:lower:]' )
1417if [[ " $os " == " darwin" ]]; then
1518 os=" darwin"
19+ elif [[ " $os " == " linux" ]]; then
20+ os=" Linux"
1621fi
17- arch=$( uname -m)
1822
23+ arch=$( uname -m)
1924if [[ " $arch " == " aarch64" ]]; then
20- arch=" arm64"
25+ arch=" arm64"
2126elif [[ " $arch " == " x86_64" ]]; then
22- arch=" x86_64"
27+ arch=" x86_64"
2328fi
2429
2530filename=" ${APP} _${os} _${arch} .tar.gz"
31+ echo " $filename "
2632
27- echo $filename
28-
29-
33+ # Validate supported OS/architecture combinations
3034case " $filename " in
3135 * " _Linux_" * )
3236 [[ " $arch " == " x86_64" || " $arch " == " arm64" ]] || exit 1
33- ;;
37+ ;;
3438 * )
3539 echo -e " ${RED} Unsupported OS/Arch: $os /$arch ${NC} "
3640 exit 1
37- ;;
41+ ;;
3842esac
3943
44+ # Print colored messages based on level
4045print_message () {
4146 local level=$1
4247 local message=$2
6166fi
6267mkdir -p " $INSTALL_DIR "
6368
69+ # Determine version and download URL
6470if [ -z " $requested_version " ]; then
6571 url=" https://github.com/devcontainer-community/feature-installer/releases/latest/download/$filename "
66- specific_version=$( curl -s https://api.github.com/repos/devcontainer-community/feature-installer/releases/latest | awk -F' "' ' /"tag_name": "/ {gsub(/^v/, "", $4); print $4}' )
67-
68- if [[ $? -ne 0 || -z " $specific_version " ]]; then
72+ if ! specific_version=$( curl -s https://api.github.com/repos/devcontainer-community/feature-installer/releases/latest | awk -F' "' ' /"tag_name": "/ {gsub(/^v/, "", $4); print $4}' ) || [[ -z " $specific_version " ]]; then
6973 echo -e " ${RED} Failed to fetch version information${NC} "
7074 exit 1
7175 fi
7579fi
7680
7781
82+ # Check if feature-installer is already installed and compare versions
7883check_version () {
7984 if command -v feature-installer > /dev/null 2>&1 ; then
80- feature_installer_path=$( which feature-installer)
81-
82-
8385 # # TODO: check if version is installed
8486 # installed_version=$(feature-installer version)
8587 installed_version=" 0.0.1"
86- installed_version=$( echo $installed_version | awk ' {print $2}' )
88+ installed_version=$( echo " $installed_version " | awk ' {print $2}' )
8789
8890 if [[ " $installed_version " != " $specific_version " ]]; then
8991 print_message info " Installed version: ${YELLOW} $installed_version ."
@@ -94,10 +96,15 @@ check_version() {
9496 fi
9597}
9698
99+ # Download and install the feature-installer binary
97100download_and_install () {
98101 print_message info " Downloading ${ORANGE} feature-installer ${GREEN} version: ${YELLOW} $specific_version ${GREEN} ..."
99102 mkdir -p featureinstallertmp && cd featureinstallertmp
100- curl -# -L -o " $filename " " $url "
103+ if ! curl -# -L -o " $filename " " $url " ; then
104+ print_message error " Failed to download $filename from $url "
105+ cd .. && rm -rf featureinstallertmp
106+ exit 1
107+ fi
101108 tar -xzf " $filename "
102109 mv feature-installer " $INSTALL_DIR "
103110 cd .. && rm -rf featureinstallertmp
@@ -106,13 +113,14 @@ download_and_install() {
106113check_version
107114download_and_install
108115
116+ # Add feature-installer directory to PATH in shell config files
109117add_to_path () {
110118 local config_file=$1
111119 local command=$2
112120
113121 if grep -Fxq " $command " " $config_file " ; then
114122 print_message info " Command already exists in $config_file , skipping write."
115- elif [[ -w $config_file ]]; then
123+ elif [[ -w " $config_file " ]]; then
116124 echo -e " \n# feature-installer" >> " $config_file "
117125 echo " $command " >> " $config_file "
118126 print_message info " Successfully added ${ORANGE} feature-installer ${GREEN} to \$ PATH in $config_file "
@@ -151,14 +159,14 @@ if [[ $EUID -ne 0 ]]; then
151159
152160 config_file=" "
153161 for file in $config_files ; do
154- if [[ -f $file ]]; then
155- config_file=$file
162+ if [[ -f " $file " ]]; then
163+ config_file=" $file "
156164 break
157165 fi
158166 done
159167
160- if [[ -z $config_file ]]; then
161- print_message error " No config file found for $current_shell . Checked files: ${config_files[@ ]} "
168+ if [[ -z " $config_file " ]]; then
169+ print_message error " No config file found for $current_shell . Checked files: ${config_files[* ]} "
162170 exit 1
163171 fi
164172
@@ -188,7 +196,7 @@ if [[ $EUID -ne 0 ]]; then
188196fi
189197
190198if [ -n " ${GITHUB_ACTIONS-} " ] && [ " ${GITHUB_ACTIONS} " == " true" ]; then
191- echo " $INSTALL_DIR " >> $GITHUB_PATH
199+ echo " $INSTALL_DIR " >> " $GITHUB_PATH "
192200 print_message info " Added $INSTALL_DIR to \$ GITHUB_PATH"
193201fi
194202
0 commit comments