Skip to content

Commit

Permalink
prefer [[ over [, and = over == in Bash
Browse files Browse the repository at this point in the history
quote string comparison RHS
  • Loading branch information
jaskfla committed Apr 6, 2024
1 parent fb478f3 commit a27a4fb
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DIR=$(pwd "$0")

cd scripts
export PGPASSWORD=$DB_PASSWORD
if [[ "$1" == "--force" || "$1" == "-f" ]]; then
if [[ $1 = '--force' || $1 = '-f' ]]; then
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT build_analytics_table(true);"
else
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT build_analytics_table();"
Expand Down
2 changes: 1 addition & 1 deletion packages/data-api/scripts/installMvRefreshModule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DIR=$(pwd "$0")
export PGPASSWORD=$DB_PG_PASSWORD
MV_REFRESH_EXISTS=`psql -p $DB_PORT -X -A -h $DB_URL -d $DB_NAME -U $DB_PG_USER -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$DB_MV_USER'"`

if [ "$MV_REFRESH_EXISTS" == "$DB_MV_USER" ]; then
if [[ $MV_REFRESH_EXISTS = "$DB_MV_USER" ]]; then
echo "Fast Refresh module already exists, skipping installation"
else
git submodule update --init scripts/pg-mv-fast-refresh
Expand Down
8 changes: 4 additions & 4 deletions packages/data-api/scripts/patchMvRefreshModule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ DIR=$(pwd "$0")

COMMAND=$1

if [[ "$COMMAND" == "" ]]; then
if [[ $COMMAND = '' ]]; then
echo "Error: missing patch command! Must be one of: up, down, create"
exit 1
fi

if [[ "$COMMAND" == "create" ]]; then
if [[ $COMMAND = 'create' ]]; then
echo "Enter patch name: "
read PATCH_NAME
fi

VERSION=$2

if [[ "$VERSION" == "" ]]; then
if [[ $VERSION = '' ]]; then
echo "Version unspecified, defaulting to database mvrefresh version"

# Set default port in case it wasn't in .env
Expand All @@ -27,7 +27,7 @@ if [[ "$VERSION" == "" ]]; then
VERSION_SQL_FUNC="SELECT mv\$version()"
VERSION=`psql -p $DB_PORT -X -A -h $DB_URL -d $DB_NAME -U $DB_USER -t -c "$VERSION_SQL_FUNC"`

if [[ "$VERSION" == "" ]]; then
if [[ $VERSION = '' ]]; then
echo "Error: failed to detect mvrefresh version from database"
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion packages/data-api/scripts/refreshAnalyticsTable.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -e

if [[ "$1" == "--full" || "$1" == "-f" ]]; then
if [[ $1 = '--full' || $1 = '-f' ]]; then
./scripts/fullRefreshAnalyticsTable.sh
else
./scripts/fastRefreshAnalyticsTable.sh
Expand Down
2 changes: 1 addition & 1 deletion packages/data-api/scripts/uninstallMvRefreshModule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DIR=$(pwd "$0")
export PGPASSWORD=$DB_PG_PASSWORD
MV_REFRESH_EXISTS=`psql -X -A -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_PG_USER -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$DB_MV_USER'"`

if [ "$MV_REFRESH_EXISTS" != "$DB_MV_USER" ]; then
if [[ $MV_REFRESH_EXISTS != "$DB_MV_USER" ]]; then
echo "Fast Refresh module does not exist, skipping uninstallation"
else
git submodule update --init scripts/pg-mv-fast-refresh
Expand Down
3 changes: 1 addition & 2 deletions packages/data-lake-api/scripts/checkTestDataLakeExists.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ DIR=$(pwd "$0")
# Set default port in case it wasn't in .env
: "${DATA_LAKE_DB_PORT:=5432}"

if [ "$(PGPASSWORD=$DB_PG_PASSWORD psql -p $DATA_LAKE_DB_PORT -X -A -h $DATA_LAKE_DB_URL -U $DB_PG_USER -t -c "SELECT 1 FROM pg_database WHERE datname='$DATA_LAKE_DB_NAME'" )" = '1' ]
then
if [[ "$(PGPASSWORD=$DB_PG_PASSWORD psql -p $DATA_LAKE_DB_PORT -X -A -h $DATA_LAKE_DB_URL -U $DB_PG_USER -t -c "SELECT 1 FROM pg_database WHERE datname='$DATA_LAKE_DB_NAME'" )" = '1' ]]; then
exit 0
fi

Expand Down
2 changes: 1 addition & 1 deletion packages/data-lake-api/scripts/setupTestDataLake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DIR=$(pwd "$0")

TUPAIA_USER_EXISTS=`PGPASSWORD=$DB_PG_PASSWORD psql -p $DATA_LAKE_DB_PORT -X -A -h $DATA_LAKE_DB_URL -U $DB_PG_USER -t -c "SELECT rolname FROM pg_catalog.pg_roles WHERE rolname = '$DATA_LAKE_DB_USER'"`

if [ -z "$TUPAIA_USER_EXISTS" ]; then
if [[ -z $TUPAIA_USER_EXISTS ]]; then
PGPASSWORD=$DB_PG_PASSWORD psql -h $DATA_LAKE_DB_URL -p $DATA_LAKE_DB_PORT -U $DB_PG_USER -c "CREATE ROLE $DATA_LAKE_DB_USER LOGIN PASSWORD '$DATA_LAKE_DB_PASSWORD'"
fi

Expand Down
8 changes: 4 additions & 4 deletions packages/database/scripts/dumpDatabase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ identity_file=""
server="dev"
target_dir="."

while [ "$1" != "" ]; do
while [[ $1 != '' ]]; do
case $1 in
-s | --server)
shift
Expand All @@ -57,7 +57,7 @@ while [ "$1" != "" ]; do
exit
;;
*)
if [ "$identity_file" == "" ]; then
if [[ $identity_file = '' ]]; then
identity_file=$1
shift
else
Expand All @@ -68,12 +68,12 @@ while [ "$1" != "" ]; do
esac
done

if [ "$identity_file" == "" ]; then
if [[ $identity_file = '' ]]; then
print_help
exit 1
fi

if [ "$DB_PG_USER" == "" ] || [ "$DB_PG_PASSWORD" == "" ]; then
if [[ $DB_PG_USER = '' || $DB_PG_PASSWORD = '' ]]; then
echo -e "${RED}Missing Postgres user credential env vars in @tupaia/database .env file.${RESET} Check Bitwarden for variables and add them to the .env file"
exit 1
fi
Expand Down
6 changes: 3 additions & 3 deletions packages/database/scripts/setupTestDatabase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ DIR=$(pwd "$0")
TUPAIA_USER_EXISTS=`PGPASSWORD=$DB_PG_PASSWORD psql -p $DB_PORT -X -A -h $DB_URL -U $DB_PG_USER -t -c "SELECT rolname FROM pg_catalog.pg_roles WHERE rolname = '$DB_USER'"`
IS_RDS=`PGPASSWORD=$DB_PG_PASSWORD psql -p $DB_PORT -X -A -h $DB_URL -U $DB_PG_USER -t -c "SELECT rolname FROM pg_catalog.pg_roles WHERE rolname = 'rds_superuser'"`

if [ -z "$TUPAIA_USER_EXISTS" ]; then
if [[ -z $TUPAIA_USER_EXISTS ]]; then
PGPASSWORD=$DB_PG_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_PG_USER -c "CREATE ROLE $DB_USER LOGIN SUPERUSER PASSWORD '$DB_PASSWORD'"
fi

PGPASSWORD=$DB_PG_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_PG_USER -c "DROP DATABASE IF EXISTS $DB_NAME"
PGPASSWORD=$DB_PG_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_PG_USER -c "CREATE DATABASE $DB_NAME WITH OWNER $DB_USER"
PGPASSWORD=$DB_PG_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_PG_USER -c "CREATE EXTENSION IF NOT EXISTS postgis"

if [ -z "$IS_RDS" ]; then
if [[ -z $IS_RDS ]]; then
PGPASSWORD=$DB_PG_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_PG_USER -c "ALTER USER $DB_USER WITH SUPERUSER"
else
PGPASSWORD=$DB_PG_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_PG_USER -c "GRANT rds_superuser TO $DB_USER"
fi

PGPASSWORD=$DB_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_USER -d $DB_NAME -f ./src/__tests__/testData/testDataDump.sql

if [ -z "$IS_RDS" ]; then
if [[ -z $IS_RDS ]]; then
PGPASSWORD=$DB_PG_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_PG_USER -c "ALTER USER $DB_USER WITH NOSUPERUSER"
else
PGPASSWORD=$DB_PG_PASSWORD psql -h $DB_URL -p $DB_PORT -U $DB_PG_USER -c "REVOKE rds_superuser FROM $DB_USER"
Expand Down
6 changes: 3 additions & 3 deletions packages/devops/scripts/ci/validateBranchName.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ function validate_name_ending() {
local branch_name=$1

for suffix in ${SUBDOMAIN_SUFFIXES[@]}; do
if [[ "$branch_name" == *$suffix ]]; then
if [[ $branch_name = *$suffix ]]; then
log_error "❌ Invalid branch name ending: '$suffix'"
exit 1
fi
# api is one of our suffixes so makes sure [branch]-api doesn't match any other api suffixes
if [[ "$suffix" == *-api && $branch_name-api == *$suffix ]]; then
if [[ $suffix = *-api && $branch_name-api = *$suffix ]]; then
log_error "❌ Invalid branch name ending: '$suffix'"
exit 1
fi
Expand All @@ -49,7 +49,7 @@ function validate_name_chars() {
fi

for character in ${INVALID_CHARS[@]}; do
if [[ "$branch_name" == *"$character"* ]]; then
if [[ $branch_name = *"$character"* ]]; then
log_error "❌ Invalid character in branch name: '$character'"
exit 1
fi
Expand Down
6 changes: 3 additions & 3 deletions packages/devops/scripts/ci/validateNewMigrations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ function validate_migrations(){
local errors="";

while read -r migration_name; do
if [[ "$migration_name" == "" ]]; then
if [[ $migration_name = '' ]]; then
break
fi
errors="$errors$(check_migration_outdated "$migration_name")"
done <<< "$new_migration_names_in_string"

if [[ "$errors" != "" ]]; then
if [[ $errors != '' ]]; then
echo $errors;
exit 1;
fi
Expand All @@ -64,7 +64,7 @@ current_branch_name=$(get_branch_name)
origin_branch_name="master"

# Skip validation if current branch name is master
if [[ "$current_branch_name" == "$origin_branch_name" ]]; then
if [[ $current_branch_name = "$origin_branch_name" ]]; then
echo "Skipping validation step while current branch is the same as origin"
exit 0
fi
Expand Down
12 changes: 6 additions & 6 deletions scripts/bash/downloadEnvironmentVariables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ COLLECTION_ID=$(bw get collection "$COLLECTION_PATH" | jq .id)
echo

# Can provide one or more packages as command line arguments, or will default to all
if [ -z $2 ]; then
if [[ -z $2 ]]; then
PACKAGES=$(${DIR}/getPackagesWithEnvFiles.sh)
echo -e "${BLUE}==>️${RESET} ${BOLD}Fetching environment variables for all packages${RESET}"
else
Expand All @@ -38,7 +38,7 @@ load_env_file_from_bw () {
# checkout deployment specific env vars, or dev as fallback
DEPLOYMENT_ENV_VARS=$(bw list items --search ${FILE_NAME}.${DEPLOYMENT_NAME}.env | jq --raw-output "map(select(.collectionIds[] | contains ($COLLECTION_ID))) | .[] .notes")

if [ -n "$DEPLOYMENT_ENV_VARS" ]; then
if [[ -n $DEPLOYMENT_ENV_VARS ]]; then
echo "$DEPLOYMENT_ENV_VARS" > ${ENV_FILE_PATH}
else
DEV_ENV_VARS=$(bw list items --search ${FILE_NAME}.dev.env | jq --raw-output "map(select(.collectionIds[] | contains ($COLLECTION_ID))) | .[] .notes")
Expand All @@ -49,14 +49,14 @@ load_env_file_from_bw () {
# deployment name (e.g. [deployment-name]-api.tupaia.org -> specific-deployment-api.tupaia.org)
sed -i -e "s/\[deployment-name\]/${DEPLOYMENT_NAME}/g" "${ENV_FILE_PATH}"

if [[ "${DEPLOYMENT_NAME}" == *-e2e || "${DEPLOYMENT_NAME}" == e2e ]]; then
if [[ $DEPLOYMENT_NAME = *-e2e || $DEPLOYMENT_NAME = 'e2e' ]]; then
# Update e2e environment variables
if [[ ${FILE_NAME} == "aggregation" ]]; then
if [[ ${FILE_NAME} == 'aggregation' ]]; then
sed -i -e 's/^AGGREGATION_URL_PREFIX="?dev-"?$/AGGREGATION_URL_PREFIX=e2e-/g' ${ENV_FILE_PATH}
fi
fi

if [[ "${DEPLOYMENT_NAME}" == dev ]]; then
if [[ $DEPLOYMENT_NAME = 'dev' ]]; then
# Update dev specific environment variables
# (removes ###DEV_ONLY### prefixes, leaving the key=value pair uncommented)
# (after removing prefix, if there are duplicate keys, dotenv uses the last one in the file)
Expand All @@ -72,7 +72,7 @@ for PACKAGE in $PACKAGES; do
# Only download the env file if there is an example file in the package. If there isn’t, this
# means it is a package that doesn’t need env vars
has_example_env_in_package=$(find $DIR/../../packages/$PACKAGE -type f -name '*.env.example' | wc -l)
if [ $has_example_env_in_package -eq 1 ]; then
if [[ $has_example_env_in_package -eq 1 ]]; then
load_env_file_from_bw $PACKAGE $DIR/../../packages/$PACKAGE ""
fi
done
Expand Down
8 changes: 4 additions & 4 deletions scripts/bash/getInternalDependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

DIR=$(dirname "$0")
if [ "$1" != "" ]; then
if [[ $1 != '' ]]; then
# pop the package_path off, and interpret the rest as dependencies that have been checked earlier
# in the recursion
package_path=$1
Expand All @@ -12,14 +12,14 @@ fi
dependencies_already_visited=($@)

# if no package.json entrypoint is specified, just return all internal dependencies
if [ -z ${package_path} ]; then
if [[ -z $package_path ]]; then
echo "types" "utils" "tsutils" "ui-components" "ui-chart-components" "ui-map-components" "server-utils" "access-policy" "admin-panel" "aggregator" "api-client" "auth" "database" "data-api" "dhis-api" "data-lake-api" "expression-parser" "indicators" "weather-api" "kobo-api" "superset-api" "data-broker" "server-boilerplate"
exit 0
fi

# we are getting internal dependencies for a specific package.json
internal_dependencies=($(sed -n '/"dependencies": {/,/}/p' ${PWD}/${package_path}/package.json | grep -o '@tupaia/[^"]*": "[0-9\.]*"' | cut -d / -f 2 | cut -d \" -f 1))
if [ ${#internal_dependencies[@]} -eq 0 ]; then
if [[ ${#internal_dependencies[@]} -eq 0 ]]; then
exit 0 # no internal dependencies of this package, early return
fi

Expand All @@ -41,7 +41,7 @@ unset array_without_gaps
for dependency in ${internal_dependencies[@]}; do

nested_dependencies=($(${DIR}/getInternalDependencies.sh "${package_path}/../${dependency}" ${dependencies_already_visited[@]} ${internal_dependencies[@]} ))
if [ ${#nested_dependencies[@]} -eq 0 ]; then
if [[ ${#nested_dependencies[@]} -eq 0 ]]; then
continue
fi

Expand Down
4 changes: 2 additions & 2 deletions scripts/bash/mergeEnvForDB.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ common_files="$file1 $file2 $file3 $file4"

# Remove files that don't exist
for file in $common_files; do
if [ ! -f "$file" ]; then
if [[ ! -f $file ]]; then
common_files=$(echo "$common_files" | sed "s|$file||g")
fi
done
Expand All @@ -40,7 +40,7 @@ merged_content="$(cat $common_files)"

# Process command line arguments, overwriting values if present
for var in $(env); do
if [[ "$var" == *=* ]]; then
if [[ $var = *=* ]]; then
key="${var%%=*}"
value="${var#*=}"
# Override values from command line
Expand Down
2 changes: 1 addition & 1 deletion scripts/bash/pm2startInline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
cd "$(dirname "${BASH_SOURCE[0]}")"
. ./ansiControlSequences.sh

if [ -z "$1" ]; then
if [[ -z $1 ]]; then
echo -e "Usage: ${BOLD}yarn start-stack${RESET} ${UNDERLINE}stack${RESET}"
echo
echo -e "All ${UNDERLINE}stack${RESET}s:"
Expand Down
8 changes: 4 additions & 4 deletions scripts/docker/downloadEnvironmentVariables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ DIR=$(dirname "$0")


# can provide one or more packages as command line arguments, or will default to all
if [ -z $3 ]; then
if [[ -z $3 ]]; then
echo "Fetching all .env files"
PACKAGES=$(${DIR}/../bash/getPackagesWithEnvFiles.sh)
else
Expand All @@ -55,14 +55,14 @@ for PACKAGE in $PACKAGES; do
# name (e.g. [deployment-name]-api.tupaia.org -> specific-deployment-api.tupaia.org)
sed -i -e "s/\[deployment-name\]/${DEPLOYMENT_NAME}/g" ${ENV_FILE_PATH}

if [[ "${DEPLOYMENT_NAME}" == *-e2e || "${DEPLOYMENT_NAME}" == e2e ]]; then
if [[ $DEPLOYMENT_NAME = *-e2e || $DEPLOYMENT_NAME = 'e2e' ]]; then
# Update e2e environment variables
if [[ ${PACKAGE} == "central-server" || ${PACKAGE} == "web-config-server" ]]; then
if [[ $PACKAGE = 'central-server' || $PACKAGE = 'web-config-server' ]]; then
sed -i -E 's/^AGGREGATION_URL_PREFIX="?dev-"?$/AGGREGATION_URL_PREFIX=e2e-/g' ${ENV_FILE_PATH}
fi
fi

if [[ "${DEPLOYMENT_NAME}" == dev ]]; then
if [[ $DEPLOYMENT_NAME = 'dev' ]]; then
# Update dev specific environment variables
# (removes ###DEV_ONLY### prefixes, leaving the key=value pair uncommented)
# (after removing prefix, if there are duplicate keys, dotenv uses the last one in the file)
Expand Down

0 comments on commit a27a4fb

Please sign in to comment.