Skip to content

Commit a85d1d6

Browse files
committed
Bring install script into ShellCheck compliance
ShellCheck is a static analysis tool for shell scripts.
1 parent df1398a commit a85d1d6

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed

.github/workflows/lint-shell.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Lint shell scripts
2+
3+
on:
4+
push:
5+
paths:
6+
- ".github/workflows/lint-shell.yml"
7+
- "**.sh"
8+
pull_request:
9+
paths:
10+
- ".github/workflows/lint-shell.yml"
11+
- "**.sh"
12+
13+
jobs:
14+
lint:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v2
20+
21+
- name: Install Taskfile
22+
uses: arduino/actions/setup-taskfile@master
23+
with:
24+
repo-token: ${{ secrets.GITHUB_TOKEN }}
25+
version: 3.x
26+
27+
- name: Lint shell scripts
28+
run: task shell:lint

Taskfile.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ tasks:
203203
cmds:
204204
- npx {{ .PRETTIER }} --write "**/*.md"
205205

206+
shell:lint:
207+
desc: Lint shell scripts
208+
cmds:
209+
# https://github.com/koalaman/shellcheck
210+
- |
211+
shopt -s globstar # Needed to check all scripts recursively.
212+
shellcheck ./**/*.sh
213+
206214
shell:check-formatting:
207215
desc: Format shell scripts
208216
cmds:

install.sh

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ PROJECT_OWNER="arduino"
1717
PROJECT_NAME="arduino-lint"
1818

1919
# BINDIR represents the local bin location, defaults to ./bin.
20-
LBINDIR=""
20+
EFFECTIVE_BINDIR=""
2121
DEFAULT_BINDIR="$PWD/bin"
2222

2323
fail() {
@@ -28,16 +28,18 @@ fail() {
2828
initDestination() {
2929
if [ -n "$BINDIR" ]; then
3030
if [ ! -d "$BINDIR" ]; then
31+
# The second instance of $BINDIR is intentionally a literal in this message.
32+
# shellcheck disable=SC2016
3133
fail "$BINDIR "'($BINDIR)'" folder not found. Please create it before continuing."
3234
fi
33-
LBINDIR="$BINDIR"
35+
EFFECTIVE_BINDIR="$BINDIR"
3436
else
3537
if [ ! -d "$DEFAULT_BINDIR" ]; then
3638
mkdir "$DEFAULT_BINDIR"
3739
fi
38-
LBINDIR="$DEFAULT_BINDIR"
40+
EFFECTIVE_BINDIR="$DEFAULT_BINDIR"
3941
fi
40-
echo "Installing in $LBINDIR"
42+
echo "Installing in $EFFECTIVE_BINDIR"
4143
}
4244

4345
initArch() {
@@ -99,12 +101,12 @@ get() {
99101
echo "Getting $GET_URL"
100102
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
101103
GET_HTTP_RESPONSE=$(curl -sL --write-out 'HTTPSTATUS:%{http_code}' "$GET_URL")
102-
GET_HTTP_STATUS_CODE=$(echo $GET_HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
104+
GET_HTTP_STATUS_CODE=$(echo "$GET_HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
103105
GET_BODY=$(echo "$GET_HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
104106
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
105107
TMP_FILE=$(mktemp)
106-
GET_BODY=$(wget --server-response --content-on-error -q -O - "$GET_URL" 2>$TMP_FILE || true)
107-
GET_HTTP_STATUS_CODE=$(cat $TMP_FILE | awk '/^ HTTP/{print $2}')
108+
GET_BODY=$(wget --server-response --content-on-error -q -O - "$GET_URL" 2>"$TMP_FILE" || true)
109+
GET_HTTP_STATUS_CODE=$(awk '/^ HTTP/{print $2}' "$TMP_FILE")
108110
fi
109111
if [ "$GET_HTTP_STATUS_CODE" != 200 ]; then
110112
echo "Request failed with HTTP status code $GET_HTTP_STATUS_CODE"
@@ -119,14 +121,14 @@ getFile() {
119121
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
120122
GETFILE_HTTP_STATUS_CODE=$(curl -s -w '%{http_code}' -L "$GETFILE_URL" -o "$GETFILE_FILE_PATH")
121123
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
122-
GETFILE_BODY=$(wget --server-response --content-on-error -q -O "$GETFILE_FILE_PATH" "$GETFILE_URL")
123-
GETFILE_HTTP_STATUS_CODE=$(cat $TMP_FILE | awk '/^ HTTP/{print $2}')
124+
wget --server-response --content-on-error -q -O "$GETFILE_FILE_PATH" "$GETFILE_URL"
125+
GETFILE_HTTP_STATUS_CODE=$(awk '/^ HTTP/{print $2}' "$TMP_FILE")
124126
fi
125127
echo "$GETFILE_HTTP_STATUS_CODE"
126128
}
127129

128130
downloadFile() {
129-
if [ -z $1 ]; then
131+
if [ -z "$1" ]; then
130132
checkLatestVersion TAG
131133
else
132134
TAG=$1
@@ -146,7 +148,7 @@ downloadFile() {
146148
echo "Trying to find a release using the GitHub API."
147149
LATEST_RELEASE_URL="https://api.github.com/repos/${PROJECT_OWNER}/$PROJECT_NAME/releases/tags/$TAG"
148150
echo "LATEST_RELEASE_URL=$LATEST_RELEASE_URL"
149-
get LATEST_RELEASE_JSON $LATEST_RELEASE_URL
151+
get LATEST_RELEASE_JSON "$LATEST_RELEASE_URL"
150152
# || true forces this command to not catch error if grep does not find anything
151153
DOWNLOAD_URL=$(echo "$LATEST_RELEASE_JSON" | grep 'browser_' | cut -d\" -f4 | grep "$ARDUINO_LINT_DIST") || true
152154
if [ -z "$DOWNLOAD_URL" ]; then
@@ -168,9 +170,9 @@ installFile() {
168170
tar xf "$ARDUINO_LINT_TMP_FILE" -C "$ARDUINO_LINT_TMP"
169171
fi
170172
ARDUINO_LINT_TMP_BIN="$ARDUINO_LINT_TMP/$PROJECT_NAME"
171-
cp "$ARDUINO_LINT_TMP_BIN" "$LBINDIR"
172-
rm -rf $ARDUINO_LINT_TMP
173-
rm -f $ARDUINO_LINT_TMP_FILE
173+
cp "$ARDUINO_LINT_TMP_BIN" "$EFFECTIVE_BINDIR"
174+
rm -rf "$ARDUINO_LINT_TMP"
175+
rm -f "$ARDUINO_LINT_TMP_FILE"
174176
}
175177

176178
bye() {
@@ -185,19 +187,22 @@ testVersion() {
185187
set +e
186188
ARDUINO_LINT="$(which $PROJECT_NAME)"
187189
if [ "$?" = "1" ]; then
188-
echo "$PROJECT_NAME not found. You might want to add "$LBINDIR" to your "'$PATH'
190+
# $PATH is intentionally a literal in this message.
191+
# shellcheck disable=SC2016
192+
echo "$PROJECT_NAME not found. You might want to add \"$EFFECTIVE_BINDIR\" to your "'$PATH'
189193
else
190194
# Convert to resolved, absolute paths before comparison
191195
ARDUINO_LINT_REALPATH="$(cd -- "$(dirname -- "$ARDUINO_LINT")" && pwd -P)"
192-
LBINDIR_REALPATH="$(cd -- "$LBINDIR" && pwd -P)"
193-
if [ "$ARDUINO_LINT_REALPATH" != "$LBINDIR_REALPATH" ]; then
194-
echo "An existing $PROJECT_NAME was found at $ARDUINO_LINT. Please prepend "$LBINDIR" to your "'$PATH'" or remove the existing one."
196+
EFFECTIVE_BINDIR_REALPATH="$(cd -- "$EFFECTIVE_BINDIR" && pwd -P)"
197+
if [ "$ARDUINO_LINT_REALPATH" != "$EFFECTIVE_BINDIR_REALPATH" ]; then
198+
# shellcheck disable=SC2016
199+
echo "An existing $PROJECT_NAME was found at $ARDUINO_LINT. Please prepend \"$EFFECTIVE_BINDIR\" to your "'$PATH'" or remove the existing one."
195200
fi
196201
fi
197202

198203
set -e
199-
ARDUINO_LINT_VERSION=$($LBINDIR/$PROJECT_NAME version)
200-
echo "$ARDUINO_LINT_VERSION installed successfully in $LBINDIR"
204+
ARDUINO_LINT_VERSION="$("$EFFECTIVE_BINDIR/$PROJECT_NAME" version)"
205+
echo "$ARDUINO_LINT_VERSION installed successfully in $EFFECTIVE_BINDIR"
201206
}
202207

203208
# Execution
@@ -209,6 +214,6 @@ set -e
209214
initArch
210215
initOS
211216
initDownloadTool
212-
downloadFile $1
217+
downloadFile "$1"
213218
installFile
214219
testVersion

0 commit comments

Comments
 (0)