From 2a5a325779f0d38455fa4324e9e45d234360193f Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sun, 16 Jan 2022 15:09:40 +0100 Subject: [PATCH] fix: avoid potential bugs (#1999) * fix: use double quotes * fix: use $(...) * fix: remove redundant 'cd -' * style: avoid using $? * style: avoid using $? * fix: revert changes * fix: use safe cd --- apps/librelingo-standalone-server/build.sh | 6 +++--- apps/web/scripts/fetchPhoto.sh | 9 ++++----- apps/web/scripts/fetchPhotos.sh | 2 +- apps/web/scripts/installExternalCourse.sh | 4 ++-- scripts/exportAllYamlCourses.sh | 5 ++--- scripts/exportYamlCourse.sh | 9 ++++++--- scripts/updateAudioForAllYamlCourses.sh | 4 ++-- scripts/updateAudioForYamlCourse.sh | 9 ++++++--- scripts/updatePypiReadmeFiles.sh | 11 +++++++---- scripts/verifyPypiReadmeFiles.sh | 8 +++----- scripts/verifyTestCourseJsonFiles.sh | 5 ++--- 11 files changed, 38 insertions(+), 34 deletions(-) diff --git a/apps/librelingo-standalone-server/build.sh b/apps/librelingo-standalone-server/build.sh index 9dcf542734c6..0a637439cc42 100755 --- a/apps/librelingo-standalone-server/build.sh +++ b/apps/librelingo-standalone-server/build.sh @@ -3,7 +3,7 @@ DIRNAME=$(dirname "$0") DEFAULT_OUTPUT_DIR="$DIRNAME/bin" if [ -n "$1" ]; then - WEBSITE_DIR="$1" + WEBSITE_DIR="$1" else echo "This script packs the LibreLingo website into a single executable file" echo "Usage: build.sh WEBSITE-DIR [OUTPUT-DIR]" @@ -24,9 +24,9 @@ function cleanup { trap cleanup EXIT # copy the website files to the build directory -cp -r "$WEBSITE_DIR" "$BUILD_DIR/static" +cp -r "$WEBSITE_DIR" "$BUILD_DIR/static" # copy the go files to the build directory -cd $DIRNAME +cd "$DIRNAME" cp -r ./* "$BUILD_DIR" set -eux diff --git a/apps/web/scripts/fetchPhoto.sh b/apps/web/scripts/fetchPhoto.sh index f194bdb2bb3b..71d1d041a6c8 100755 --- a/apps/web/scripts/fetchPhoto.sh +++ b/apps/web/scripts/fetchPhoto.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash -url=`echo $1 | cut -d',' -f2` -image_name=`echo $1 | cut -d',' -f1` -image_id=`echo $url | cut -d'/' -f5` +url=$(echo "$1" | cut -d',' -f2) +image_name=$(echo "$1" | cut -d',' -f1) +image_id=$(echo "$url" | cut -d'/' -f5) download_url="https://unsplash.com/photos/$image_id/download" if [ -e "./static/images/$image_name.jpg" ]; then @@ -11,7 +11,7 @@ fi echo "Fetching URL $url" echo "Image ID $image_id" -wget $download_url -O "/tmp/$image_id.jpg" +wget "$download_url" -O "/tmp/$image_id.jpg" convert "/tmp/$image_id.jpg" -resize 512x -resize 'x512<' -gravity center -crop 512x512+0+0 +repage "/tmp/$image_id\_cropped.jpg" convert "/tmp/$image_id.jpg" -resize 240x -resize 'x240<' -gravity center -crop 240x240+0+0 +repage "/tmp/$image_id\_tiny.jpg" convert "/tmp/$image_id.jpg" -resize 100x -resize 'x100<' -gravity center -crop 100x100+0+0 +repage "/tmp/$image_id\_tinier.jpg" @@ -21,4 +21,3 @@ jpegoptim "/tmp/$image_id\_tinier.jpg" -d "./static/images/" -s -m55 mv "./static/images/$image_id\_cropped.jpg" "./static/images/$image_name.jpg" mv "./static/images/$image_id\_tiny.jpg" "./static/images/$image_name""_tiny.jpg" mv "./static/images/$image_id\_tinier.jpg" "./static/images/$image_name""_tinier.jpg" - diff --git a/apps/web/scripts/fetchPhotos.sh b/apps/web/scripts/fetchPhotos.sh index 84d4caa87992..0de7f97a692e 100755 --- a/apps/web/scripts/fetchPhotos.sh +++ b/apps/web/scripts/fetchPhotos.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash tail -n +2 ../../docs/image_attributions.csv | while read -r photo_description; do \ - ./scripts/fetchPhoto.sh $photo_description; \ + ./scripts/fetchPhoto.sh "$photo_description"; \ done diff --git a/apps/web/scripts/installExternalCourse.sh b/apps/web/scripts/installExternalCourse.sh index 59b700ae5138..127134b1ed0c 100755 --- a/apps/web/scripts/installExternalCourse.sh +++ b/apps/web/scripts/installExternalCourse.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash wget "$1" -O temp.zip -unzip temp.zip "*/course/*" -d ../../courses/$2 -mv ../../courses/$2/*/course/* ../../courses/$2 +unzip temp.zip "*/course/*" -d ../../courses/"$2" +mv ../../courses/"$2"/*/course/* ../../courses/"$2" rm temp.zip diff --git a/scripts/exportAllYamlCourses.sh b/scripts/exportAllYamlCourses.sh index 06b197866dfd..f996a29d4a93 100755 --- a/scripts/exportAllYamlCourses.sh +++ b/scripts/exportAllYamlCourses.sh @@ -2,8 +2,8 @@ ANY_FAILED="0" for d in ./courses/*/ ; do - d=$(echo $d | sed 's/[^/]*\/[^/]*\///' | sed 's/\///') - ./scripts/exportYamlCourse.sh $d + d=$(echo "$d" | sed 's/[^/]*\/[^/]*\///' | sed 's/\///') + ./scripts/exportYamlCourse.sh "$d" if [ "$?" -eq "1" ]; then ANY_FAILED="1" fi @@ -13,4 +13,3 @@ if [ "$ANY_FAILED" -eq "1" ]; then echo "😿 Could not export all yaml courses" exit 1 fi - diff --git a/scripts/exportYamlCourse.sh b/scripts/exportYamlCourse.sh index 9c09482f6769..da5241e73397 100755 --- a/scripts/exportYamlCourse.sh +++ b/scripts/exportYamlCourse.sh @@ -1,13 +1,16 @@ #!/bin/bash echo -en "⏳ Exporting course $1" -cd ./apps/librelingo_json_export/ > /dev/null -poetry run export-cli ../../courses/$1 ../../apps/web/src/courses/$1 +cd ./apps/librelingo_json_export/ || +{ + echo -en "\r⚠️ Wrong folder structure" + exit 1 +} +poetry run export-cli ../../courses/"$1" ../../apps/web/src/courses/"$1" if [ $? -eq 0 ]; then echo -en "\r✅ Exported course $1" else echo -en "\r⚠️ Couldn't export course $1" exit 1 fi -cd - > /dev/null echo diff --git a/scripts/updateAudioForAllYamlCourses.sh b/scripts/updateAudioForAllYamlCourses.sh index 8dce890892f7..262117912ecc 100755 --- a/scripts/updateAudioForAllYamlCourses.sh +++ b/scripts/updateAudioForAllYamlCourses.sh @@ -1,6 +1,6 @@ #!/bin/bash for d in ./courses/*/ ; do - d=$(echo $d | sed 's/[^/]*\/[^/]*\///' | sed 's/\///') - ./scripts/updateAudioForYamlCourse.sh $d "$@" + d=$(echo "$d" | sed 's/[^/]*\/[^/]*\///' | sed 's/\///') + ./scripts/updateAudioForYamlCourse.sh "$d" "$@" done diff --git a/scripts/updateAudioForYamlCourse.sh b/scripts/updateAudioForYamlCourse.sh index 09fb793316e4..efe5a5fd642b 100755 --- a/scripts/updateAudioForYamlCourse.sh +++ b/scripts/updateAudioForYamlCourse.sh @@ -1,12 +1,15 @@ #!/bin/bash echo -en "⏳ Updating audio for course $1" -cd ./apps/librelingo_audios/ > /dev/null -poetry run python librelingo_audios/cli.py ../../courses/$1 ../../apps/web/static/voice $1 "${@:2}" +cd ./apps/librelingo_audios/ || +{ + echo -en "\r⚠️ Wrong folder structure" + exit 1 +} +poetry run python librelingo_audios/cli.py ../../courses/"$1" ../../apps/web/static/voice "$1" "${@:2}" if [ $? -eq 0 ]; then echo -en "\r✅ Updated audio for course $1" else echo -en "\r⚠️ Couldn't update audio for course $1" fi -cd - > /dev/null echo diff --git a/scripts/updatePypiReadmeFiles.sh b/scripts/updatePypiReadmeFiles.sh index 596b350a6cc1..ec145d7746a9 100755 --- a/scripts/updatePypiReadmeFiles.sh +++ b/scripts/updatePypiReadmeFiles.sh @@ -1,11 +1,14 @@ #!/bin/bash for directory in $(find apps/ -name pyproject.toml | xargs -n1 dirname); do - echo "Updating README.md for Python package '$(basename $directory)'..." - cd $directory + echo "Updating README.md for Python package '$(basename "$directory")'..." + cd "$directory" || + { + echo -en "\r⚠️ Could not enter $directory" + exit 1 + } poetry install - make README.md -B $1 + make README.md -B "$1" cd - echo done - diff --git a/scripts/verifyPypiReadmeFiles.sh b/scripts/verifyPypiReadmeFiles.sh index 7da774c3c69f..69b29e70319a 100755 --- a/scripts/verifyPypiReadmeFiles.sh +++ b/scripts/verifyPypiReadmeFiles.sh @@ -2,19 +2,17 @@ ./scripts/updatePypiReadmeFiles.sh -B -git diff --exit-code --name-only - -if [ "$?" -eq "0" ]; then +if git diff --exit-code --name-only; then echo "🎉 Amazing, PyPi readme files are up to date" else echo "😿 Outdated PyPi readme files detected." echo "The following files were outdated:" - git diff --name-only + git diff --name-only echo echo "See the full list of changes:" git diff echo "Run the following script to automatically update the readme files:" echo echo "./scripts/updatePypiReadmeFiles.sh" - exit 1 + exit 1 fi diff --git a/scripts/verifyTestCourseJsonFiles.sh b/scripts/verifyTestCourseJsonFiles.sh index 57c5c6185dbf..ab4ec0445a86 100755 --- a/scripts/verifyTestCourseJsonFiles.sh +++ b/scripts/verifyTestCourseJsonFiles.sh @@ -1,9 +1,8 @@ #!/bin/bash yarn exportCourse test -git diff --exit-code --name-only -if [ "$?" -eq "0" ]; then +if git diff --exit-code --name-only; then echo "🎉 Amazing, test course JSON files are up to date" else echo "😿 Outdated test course JSON files detected." @@ -13,7 +12,7 @@ else echo "See the full list of changes:" git diff echo "Run the following script to automatically update the JSON files:" - echo + echo echo "yarn exportAllCourses" exit 1 fi