Skip to content
This repository was archived by the owner on Sep 8, 2024. It is now read-only.

Commit fd12c88

Browse files
authored
Refactor/shellcheck bin (#3019)
* Shellcheck and update mycroft-config * Shellcheck and update mycroft-pip * Shellcheck and update mycroft-cli-client * Shellcheck and update mycroft-help * Shellcheck and update mycroft-listen * Shellcheck and update mycroft-mic-test * Shellcheck and update mycroft-msk * Shellcheck and update mycroft-msm * Shellcheck and update mycroft-say-to * Shellcheck and update mycroft-skill-testrunner * Shellcheck and update mycroft-speak * Shellcheck and update mycroft-start * Shellcheck and update mycroft-stop * Add shellcheck step to github actions This runs most of the shellcheck tests. The excludes are: - SC1091: Avoids errors when shellcheck can't find sourced file - SC2034: Unused variables, for example colors that aren't used yet - SC2012: use of ls, from what I can see in our case this is fine (wc -l) The version is locked to latest master as of November 2 2021
1 parent 0ad093a commit fd12c88

14 files changed

Lines changed: 62 additions & 53 deletions

.github/workflows/unit_test.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,14 @@ jobs:
6666
run: if [[ ${{ matrix.python-version }} == 3.9 ]]; then bash <(curl -s https://codecov.io/bash); fi
6767
env:
6868
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
69+
shellcheck:
70+
name: Shellcheck
71+
runs-on: ubuntu-latest
72+
steps:
73+
- uses: actions/checkout@v2
74+
- name: Shell check mycroft tools
75+
uses: ludeeus/action-shellcheck@ceeca77f6538b97991ca2c2a2ebe1ab64e573b5e
76+
env:
77+
SHELLCHECK_OPTS: -x --exclude SC2034 --exclude SC2012 --exclude SC1091
78+
with:
79+
scandir: ./bin

bin/mycroft-cli-client

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ DIR="$( dirname "$SOURCE" )"
2121
source "$DIR/../venv-activate.sh" -q
2222

2323
# Invoke the Command Line Interface
24-
python -m mycroft.client.text $@
24+
python -m mycroft.client.text "$@"

bin/mycroft-config

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
SOURCE="${BASH_SOURCE[0]}"
18-
cd -P "$( dirname "$SOURCE" )"
18+
cd -P "$( dirname "$SOURCE" )" || exit
1919
DIR="$( pwd )"
2020
script=${0}
2121
script=${script##*/}
@@ -49,7 +49,7 @@ function help() {
4949

5050
VIEWER="nano --syntax=json --view"
5151
if [ -z "$EDITOR" ] ; then
52-
if [ $( which sensible-editor ) ] ; then
52+
if which sensible-editor > /dev/null ; then
5353
EDITOR="sensible-editor"
5454
else
5555
EDITOR="nano --syntax=json --tempfile"
@@ -82,14 +82,14 @@ function validate_config_file() {
8282
return 0
8383
fi
8484

85-
echo -n ${BLUE}
85+
echo -n "${BLUE}"
8686

8787
# Remove any comments (lines starting with # or //) found in the file and
8888
# Use jq to validate and output errors
8989
sed 's/^\s*[#\/].*$//g' "$1" | sed '/^$/d' | jq -e "." > /dev/null
9090
result=$?
9191

92-
echo -n ${RESET}
92+
echo -n "${RESET}"
9393

9494
#xxx echo "RESULT=$result for $1"
9595
return $result
@@ -99,7 +99,7 @@ _conf_file="${XDG_CONFIG_HOME:-$HOME/.config}/mycroft/mycroft.conf"
9999
function name_to_path() {
100100
case ${1} in
101101
"system") _conf_file="/etc/mycroft/mycroft.conf" ;;
102-
"user") _conf_file=$(readlink -f ${XDG_CONFIG_HOME:-$HOME/.config}/mycroft/mycroft.conf) ;;
102+
"user") _conf_file=$(readlink -f "${XDG_CONFIG_HOME:-$HOME/.config}/mycroft/mycroft.conf") ;;
103103
"default") _conf_file="$DIR/../mycroft/configuration/mycroft.conf" ;;
104104
"remote") _conf_file="$HOME/.cache/mycroft/web_cache.json" ;;
105105

@@ -113,12 +113,12 @@ function name_to_path() {
113113
################################################################
114114

115115
function edit_config() {
116-
name_to_path $1
117-
validate_config_file $_conf_file
116+
name_to_path "$1"
117+
validate_config_file "$_conf_file"
118118
rc=$?
119119
if [ $rc -ne 0 ] ; then
120120
echo "${YELLOW}WARNING: ${RESET}Configuration file did not pass validation before edits."
121-
read -p "Review errors above and press ENTER to continue with editing."
121+
read -r -p "Review errors above and press ENTER to continue with editing."
122122
fi
123123

124124
if [ -f "${_conf_file}" ] ; then
@@ -128,7 +128,7 @@ function edit_config() {
128128
echo "}" >> "${TEMP}/mycroft.json"
129129
fi
130130

131-
while [ 1 ] ; do
131+
while true ; do
132132
case $1 in
133133
system | user)
134134
# Allow user to edit
@@ -150,19 +150,18 @@ function edit_config() {
150150
fi
151151

152152
# file was changed, validate changes
153-
validate_config_file $TEMP/mycroft.json
154-
if [ $? -ne 0 ] ; then
153+
if validate_config_file $TEMP/mycroft.json > /dev/null ; then
154+
key="S"
155+
else
155156
echo "${YELLOW}WARNING: ${RESET}Configuration file does not pass validation, see errors above."
156157
echo "Press X to abandon changes, S to force save, any other key to edit again."
157-
read -N1 -s key
158-
else
159-
key="S"
158+
read -r -N1 -s key
160159
fi
161160

162161
case $key in
163162
[Ss])
164163
echo "Saving..."
165-
mv $TEMP/mycroft.json $_conf_file
164+
mv $TEMP/mycroft.json "$_conf_file"
166165
signal_reload_config
167166
break
168167
;;
@@ -180,11 +179,11 @@ function signal_reload_config() {
180179
source "$DIR/../venv-activate.sh" -q
181180

182181
# Post a messagebus notification to reload the config file
183-
output=$(python -m mycroft.messagebus.send "configuration.updated" "{}")
182+
python -m mycroft.messagebus.send "configuration.updated" "{}" > /dev/null
184183
}
185184

186185
function show_config() {
187-
name_to_path $1
186+
name_to_path "$1"
188187

189188
# Use jq to display formatted nicely (after stripping out comments)
190189
sed 's/^\s*[#\/].*$//g' "${_conf_file}" | sed '/^$/d' | jq "."
@@ -201,7 +200,7 @@ function get_config() {
201200
json_config=$( source "$DIR/../venv-activate.sh" -q && python -c "import json; from mycroft.configuration import Configuration; print(json.dumps(Configuration.get()))" )
202201

203202
# Read the given variable from the mix
204-
echo ${json_config} | jq -r "${value}"
203+
echo "${json_config}" | jq -r "${value}"
205204
}
206205

207206
function set_config() {
@@ -212,27 +211,26 @@ function set_config() {
212211
value=".${value}"
213212
fi
214213

215-
jq "${value} = \"$2\"" $_conf_file > "${TEMP}/~mycroft.conf"
216-
if [ $? -eq 0 ] ; then
214+
if jq "${value} = \"$2\"" "$_conf_file" > "${TEMP}/~mycroft.conf" ; then
217215
# Successful update, replace the config file
218-
mv "${TEMP}/~mycroft.conf" $_conf_file
216+
mv "${TEMP}/~mycroft.conf" "$_conf_file"
219217
signal_reload_config
220218
fi
221219
}
222220

223221
_opt=$1
224222
case ${_opt} in
225223
"edit")
226-
edit_config $2
224+
edit_config "$2"
227225
;;
228226
"reload")
229227
signal_reload_config
230228
;;
231229
"show")
232-
show_config $2
230+
show_config "$2"
233231
;;
234232
"get")
235-
get_config $2
233+
get_config "$2"
236234
;;
237235
"set")
238236
set_config "$2" "$3"

bin/mycroft-help

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
SOURCE="${BASH_SOURCE[0]}"
18-
cd -P "$( dirname "$SOURCE" )"/..
18+
cd -P "$( dirname "$SOURCE" )"/.. || exit
1919
DIR="$( pwd )"
2020

2121
echo -e "\e[36mMycroft\e[0m is your open source voice assistant. Full source"

bin/mycroft-listen

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
SOURCE="${BASH_SOURCE[0]}"
18-
cd -P "$( dirname "$SOURCE" )"
18+
cd -P "$( dirname "$SOURCE" )" || exit
1919
DIR="$( pwd )"
2020

2121
# Enter the Mycroft venv

bin/mycroft-mic-test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
SOURCE="${BASH_SOURCE[0]}"
18-
cd -P "$( dirname "$SOURCE" )"
18+
cd -P "$( dirname "$SOURCE" )" || exit
1919
DIR="$( pwd )"
2020

2121
restart=0
@@ -32,7 +32,7 @@ fi
3232

3333

3434
# Launch the standard audiotest
35-
"$DIR/../start-mycroft.sh" audiotest $@
35+
"$DIR/../start-mycroft.sh" audiotest "$@"
3636

3737

3838
if [ $restart -eq 1 ]

bin/mycroft-msk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ DIR="$( dirname "$SOURCE" )"
2121
source "$DIR/../venv-activate.sh" -q
2222

2323
# Invoke the Mycroft Skills Kit from within the venv
24-
msk $@
24+
msk "$@"

bin/mycroft-msm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ DIR="$( dirname "$SOURCE" )"
2121
source "$DIR/../venv-activate.sh" -q
2222

2323
# Invoke the Mycroft Skills Manager (msm) within the venv
24-
msm $@
24+
msm "$@"

bin/mycroft-pip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ DIR="$( dirname "$SOURCE" )"
2121
source "$DIR/../venv-activate.sh" -q
2222

2323
# Install pip packages within the Mycroft venv
24-
pip $@
24+
pip "$@"

bin/mycroft-say-to

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
SOURCE="${BASH_SOURCE[0]}"
18-
cd -P "$( dirname "$SOURCE" )"
18+
cd -P "$( dirname "$SOURCE" )" || exit
1919
DIR="$( pwd )"
2020

2121
# Enter the Mycroft venv
@@ -25,5 +25,5 @@ source "$DIR/../venv-activate.sh" -q
2525
set -- "${1:-$(</dev/stdin)}" "${@:2}"
2626

2727
# Send a message to be spoken
28-
data="$@"
28+
data="$*"
2929
output=$(python -m mycroft.messagebus.send "recognizer_loop:utterance" "{\"utterances\": [\"$data\"], \"lang\": \"en-us\"}")

0 commit comments

Comments
 (0)