Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: unify Bash code style #5568

Open
wants to merge 27 commits into
base: dev
Choose a base branch
from
Open

style: unify Bash code style #5568

wants to merge 27 commits into from

Conversation

jaskfla
Copy link
Contributor

@jaskfla jaskfla commented Apr 6, 2024

Took some time over the weekend to read up on Bash. This ended up being a logical place for me to apply some of those learnings. Completely understandable if no one wants to review this, especially considering it touches some deployment scripts.

Tip

This is one to review commit-by-commit.

I’ve only made changes to shell scripts specifically targeting Bash. Plain sh scripts are untouched. Changes are largely informed by ShellCheck, shellharden and BashPitfalls.

Changes

Note

Other than prettifying a few outputs to the terminal, this is a pretty pure refactor with no material/logical/algorithmic changes.

  • Prefer the more portable #!/usr/bin/env bash over #!/bin/bash.
    • Exception: When -l is set. I’m not sure why we use -l in shell scripts, and I don’t believe set -l is a valid option, so leaving these untouched.
  • Double-quote variables to prevent word splitting.
    • e.g. We have many instances where commands would break if there is a space anywhere in a file/directory path.
    • Exceptions: Variable assignments and within [[ ... ]].
  • Avoid braces ${...} when expanding variables unless meaningfully more readable (or necessary, like in "${foo}bar").
  • Prefer $(...) over `...` for command substitutions.
  • Prefer [[ ... ]] over [ ... ] for tests.
  • Prefer (( ... )) over [[ ... ]] for arithmetic comparisons.
  • Prefer = over == in tests.
  • Prefer new += syntax for concatenation of strings and arrays.
  • Prefer . over source command.
    • Conflicted about this one. I find source eminently more readable, but . is more portable. However, in Bash, they are equivalent, so there is a real argument for us to prefer source in Bash scripts. ❗ Open to opinions!
  • Prefer iterating over arrays instead of manipulating whitespace-delimited strings and exploiting word splitting.
  • Introduces ansiControlSequences.sh for convenience variables like ${RED} and ${RESET} for prettifying stdout. This honours the NO_COLOR environment variable if the user has it set.
  • Prettifies the output of download-env-vars, backendStartDev.sh and dumpDatabase.sh, patchMvRefereshModule.sh.

(Sorry about all the force pushes. I squashed a lot of commits to try and keep the commit history clean 😅)

@jaskfla jaskfla force-pushed the shellharden branch 2 times, most recently from 89d8fa2 to 993558a Compare April 6, 2024 20:59
@@ -1,5 +1,4 @@
#!/bin/bash
DIR=$(dirname "$0")


DIR=$(pwd "$0")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediately overwritten in line 4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-04-05T213533

@jaskfla jaskfla changed the title tweak: unify code style in Bash scripts style: unify code style in Bash scripts Apr 6, 2024
Comment on lines 15 to 20
echo -e "${RED}Error: $DB_NAME database does not exist!${RESET}"
echo "To create it, get the .env file from LastPass then run:"
echo 'To create it, make sure you have the environment variables from Bitwarden and run:'
echo
echo -e " ${BOLD}yarn workspace @tupaia/database setup-test-database${RESET}"
echo
echo -e "If you’re missing environment variables, see ${MAGENTA}https://beyond-essential.slab.com/posts/tupaia-monorepo-setup-v5egpdpq#hvfnz-set-environment-variables${RESET}."
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-04-07T091529

@jaskfla jaskfla force-pushed the shellharden branch 12 times, most recently from 07069e7 to d794119 Compare April 7, 2024 11:36
@jaskfla jaskfla force-pushed the shellharden branch 3 times, most recently from cf05c60 to 1e92207 Compare April 7, 2024 12:00
@@ -49,7 +49,6 @@ for dependency in ${internal_dependencies[@]}; do
done

# remove any duplicates
deduplicated_union=()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never used and not exported

@jaskfla jaskfla force-pushed the shellharden branch 3 times, most recently from e96bc0a to b68f4df Compare April 9, 2024 10:30
@jaskfla jaskfla marked this pull request as ready for review April 10, 2024 21:57
@jaskfla jaskfla force-pushed the shellharden branch 3 times, most recently from 7e25192 to 1ed8e57 Compare April 11, 2024 00:24
# Conflicts:
#	packages/data-api/scripts/installMvRefreshModule.sh
#	packages/data-lake-api/scripts/checkTestDataLakeExists.sh
#	packages/database/scripts/checkTestDatabaseExists.sh
#	packages/database/scripts/dumpDatabase.sh
#	packages/database/scripts/setupTestDatabase.sh
#	scripts/bash/downloadEnvironmentVariables.sh
#	scripts/bash/getPackagesWithEnvFiles.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant