Skip to content

Commit a6593e8

Browse files
committed
shellcheck
1 parent e1331a9 commit a6593e8

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

scripts/install.sh

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
3+
34
APP=feature-installer
45

6+
# Color definitions
57
RED='\033[0;31m'
68
GREEN='\033[0;32m'
79
YELLOW='\033[1;33m'
@@ -10,33 +12,36 @@ NC='\033[0m' # No Color
1012

1113
requested_version=${VERSION:-}
1214

13-
os=$(uname -s | tr '[:upper:]' '[:upper:]')
15+
# Detect OS and architecture
16+
os=$(uname -s | tr '[:upper:]' '[:lower:]')
1417
if [[ "$os" == "darwin" ]]; then
1518
os="darwin"
19+
elif [[ "$os" == "linux" ]]; then
20+
os="Linux"
1621
fi
17-
arch=$(uname -m)
1822

23+
arch=$(uname -m)
1924
if [[ "$arch" == "aarch64" ]]; then
20-
arch="arm64"
25+
arch="arm64"
2126
elif [[ "$arch" == "x86_64" ]]; then
22-
arch="x86_64"
27+
arch="x86_64"
2328
fi
2429

2530
filename="${APP}_${os}_${arch}.tar.gz"
31+
echo "$filename"
2632

27-
echo $filename
28-
29-
33+
# Validate supported OS/architecture combinations
3034
case "$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+
;;
3842
esac
3943

44+
# Print colored messages based on level
4045
print_message() {
4146
local level=$1
4247
local message=$2
@@ -61,11 +66,10 @@ else
6166
fi
6267
mkdir -p "$INSTALL_DIR"
6368

69+
# Determine version and download URL
6470
if [ -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
@@ -75,15 +79,13 @@ else
7579
fi
7680

7781

82+
# Check if feature-installer is already installed and compare versions
7883
check_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
97100
download_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() {
106113
check_version
107114
download_and_install
108115

116+
# Add feature-installer directory to PATH in shell config files
109117
add_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
188196
fi
189197

190198
if [ -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"
193201
fi
194202

0 commit comments

Comments
 (0)