Skip to content

Commit 4944c1e

Browse files
authored
Merge pull request #6 from go-waitfor/copilot/fix-60a5336a-ce4b-4294-a595-945dbb68f82b
Fix critical issues in install.sh script - syntax errors, shellcheck warnings, and add version detection fallback
2 parents ddced0e + f5902cc commit 4944c1e

1 file changed

Lines changed: 65 additions & 76 deletions

File tree

install.sh

Lines changed: 65 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@
22

33
# Copyright Tim Voronov 2020
44
version=$(curl -sI https://github.com/go-waitfor/cli/releases/latest | grep Location | awk -F"/" '{ printf "%s", $NF }' | tr -d '\r')
5-
if [ ! $version ]; then
6-
echo "Failed while attempting to install waitfor. Please manually install:"
7-
echo ""
8-
echo "1. Open your web browser and go to https://github.com/go-waitfor/cli/releases"
9-
echo "2. Download the latest release for your platform."
10-
echo "3. chmod +x ./waitfor"
11-
echo "4. mv ./waitfor /usr/local/bin"
12-
exit 1
5+
if [ ! "$version" ]; then
6+
echo "Unable to detect latest version, falling back to 'latest'"
7+
version="latest"
138
fi
149

1510
hasCli() {
16-
has=$(which waitfor)
17-
18-
if [ "$?" = "0" ]; then
11+
if which waitfor >/dev/null 2>&1; then
1912
echo
2013
echo "You already have waitfor!"
2114
export n=3
@@ -24,16 +17,12 @@ hasCli() {
2417
sleep $n
2518
fi
2619

27-
hasCurl=$(which curl)
28-
29-
if [ "$?" = "1" ]; then
20+
if ! which curl >/dev/null 2>&1; then
3021
echo "You need curl to use this script."
3122
exit 1
3223
fi
3324

34-
hasTar=$(which tar)
35-
36-
if [ "$?" = "1" ]; then
25+
if ! which tar >/dev/null 2>&1; then
3726
echo "You need tar to use this script."
3827
exit 1
3928
fi
@@ -42,15 +31,12 @@ hasCli() {
4231
checkHash(){
4332
sha_cmd="sha256sum"
4433

45-
if [ ! -x "$(command -v $sha_cmd)" ]; then
34+
if [ ! -x "$(command -v "$sha_cmd")" ]; then
4635
sha_cmd="shasum -a 256"
4736
fi
4837

49-
if [ -x "$(command -v $sha_cmd)" ]; then
50-
51-
(cd $targetDir && curl -sSL $baseUrl/waitfor_checksums.txt | $sha_cmd -c >/dev/null)
52-
if [ "$?" != "0" ]; then
53-
# rm $targetFile
38+
if [ -x "$(command -v "$sha_cmd")" ]; then
39+
if ! (cd "$targetDir" && curl -sSL "$baseUrl"/checksums.txt | "$sha_cmd" -c >/dev/null); then
5440
echo "Binary checksum didn't match. Exiting"
5541
exit 1
5642
fi
@@ -64,30 +50,31 @@ getPackage() {
6450
platform=""
6551
case $uname in
6652
"Darwin")
67-
platform="_darwin"
68-
;;
53+
platform="_darwin"
54+
;;
6955
"Linux")
70-
platform="_linux"
71-
;;
56+
platform="_linux"
57+
;;
58+
*)
59+
echo "Platform $uname is not supported. Exiting"
60+
exit 1
61+
;;
7262
esac
7363

74-
uname=$(uname -m)
64+
uname_arch=$(uname -m)
7565
arch=""
76-
case $uname in
66+
case $uname_arch in
7767
"x86_64")
78-
arch="_x86_64"
79-
;;
80-
esac
81-
case $uname in
82-
"aarch64")
83-
arch="_arm64"
84-
;;
85-
esac
86-
87-
if [ "$arch" == "" ]; then
88-
echo "${$arch} is not supported. Exiting"
68+
arch="_amd64"
69+
;;
70+
"aarch64"|"arm64")
71+
arch="_arm64"
72+
;;
73+
*)
74+
echo "Architecture $uname_arch is not supported. Exiting"
8975
exit 1
90-
fi
76+
;;
77+
esac
9178

9279
suffix=$platform$arch
9380
targetDir="/tmp/waitfor$suffix"
@@ -96,56 +83,58 @@ getPackage() {
9683
targetDir="$(pwd)/waitfor$suffix"
9784
fi
9885

99-
if [ ! -d $targetDir ]; then
100-
mkdir $targetDir
86+
if [ ! -d "$targetDir" ]; then
87+
mkdir "$targetDir"
10188
fi
10289

10390
targetFile="$targetDir/waitfor"
10491

105-
if [ -e $targetFile ]; then
106-
rm $targetFile
92+
if [ -e "$targetFile" ]; then
93+
rm "$targetFile"
10794
fi
10895

109-
baseUrl=https://github.com/go-waitfor/cli/releases/download/$version
96+
if [ "$version" = "latest" ]; then
97+
baseUrl=https://github.com/go-waitfor/cli/releases/latest/download
98+
else
99+
baseUrl=https://github.com/go-waitfor/cli/releases/download/$version
100+
fi
110101
url=$baseUrl/waitfor$suffix.tar.gz
111102
echo "Downloading package $url as $targetFile"
112103

113-
curl -sSL $url | tar xz -C $targetDir
114-
115-
if [ "$?" = "0" ]; then
104+
if ! curl -sSL "$url" | tar xz -C "$targetDir"; then
105+
echo "Failed to download or extract package"
106+
exit 1
107+
fi
116108

117-
# checkHash
109+
checkHash
118110

119-
chmod +x $targetFile
111+
chmod +x "$targetFile"
120112

121113
echo "Download complete."
122114

123-
if [ "$userid" != "0" ]; then
124-
echo
125-
echo "========================================================="
126-
echo "== As the script was run as a non-root user the =="
127-
echo "== following commands may need to be run manually =="
128-
echo "========================================================="
129-
echo
130-
echo " sudo cp $targetFile /usr/local/bin/waitfor"
131-
echo " rm -rf $targetDir"
132-
echo
133-
else
134-
echo
135-
echo "Running as root - Attempting to move $targetFile to /usr/local/bin"
136-
137-
mv $targetFile /usr/local/bin/waitfor
138-
139-
if [ "$?" = "0" ]; then
140-
echo "New version of waitfor installed to /usr/local/bin"
141-
fi
142-
143-
if [ -d $targetDir ]; then
144-
rm -rf $targetDir
145-
fi
146-
147-
waitfor version
115+
if [ "$userid" != "0" ]; then
116+
echo
117+
echo "========================================================="
118+
echo "== As the script was run as a non-root user the =="
119+
echo "== following commands may need to be run manually =="
120+
echo "========================================================="
121+
echo
122+
echo " sudo cp \"$targetFile\" /usr/local/bin/waitfor"
123+
echo " rm -rf \"$targetDir\""
124+
echo
125+
else
126+
echo
127+
echo "Running as root - Attempting to move \"$targetFile\" to /usr/local/bin"
128+
129+
if mv "$targetFile" /usr/local/bin/waitfor; then
130+
echo "New version of waitfor installed to /usr/local/bin"
148131
fi
132+
133+
if [ -d "$targetDir" ]; then
134+
rm -rf "$targetDir"
135+
fi
136+
137+
waitfor version
149138
fi
150139
}
151140

0 commit comments

Comments
 (0)