aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2025-06-03 22:58:46 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2025-06-03 22:58:46 -0400
commitea783f67b7bfa8e49a02fbcc93572341bed38e55 (patch)
tree74503a2f5f71dfb5454bf679b0c2ee6876e34a90 /tools
parentRemove deprecated version field from docker compose files (diff)
downloadcli-ea783f67b7bfa8e49a02fbcc93572341bed38e55.zip
Add improvements to db.sh helper script
Diffstat (limited to 'tools')
-rwxr-xr-xtools/db.sh50
1 files changed, 39 insertions, 11 deletions
diff --git a/tools/db.sh b/tools/db.sh
index 27fe6d3..c554e48 100755
--- a/tools/db.sh
+++ b/tools/db.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
#
# Start/Stop test databases.
#
@@ -9,23 +9,51 @@
# Usage:
# $ ./tools/db.sh start <database-name>
# $ ./tools/db.sh stop <database-name>
-BASE_DIR=`dirname $0`/..
-ALLOWED_DB=("postgresql" "mysql" "mssql" "sqlite3")
+set +e
-if [[ ! " ${ALLOWED_DB[@]} " =~ " $2 " ]]; then
- echo "Error: Invalid database type. Allowed types are: ${ALLOWED_DB[@]}"
- exit 1
-fi
+BASE_DIR=$(dirname "$0")/..
+ALLOWED_DBS=("postgresql" "mysql" "mssql")
+ALLOWED_COMMANDS=("start" "stop")
-case "$1" in
+function main() {
+ local COMMAND="$1"
+ if ! elementIn "$COMMAND" "${ALLOWED_COMMANDS[@]}"; then
+ echo "Error: Invalid command. Allowed subcommands are: ${ALLOWED_COMMANDS[*]}" >&2
+ exit 1
+ fi
+ local DB_NAME="$2"
+ if ! elementIn "$DB_NAME" "${ALLOWED_DBS[@]}"; then
+ echo "Error: Invalid database type. Allowed types are: ${ALLOWED_DBS[*]}" >&2
+ exit 1
+ fi
+
+ case $COMMAND in
start)
- env $(cat "$BASE_DIR/.env.$2" | xargs) docker compose -f "$BASE_DIR/docker-compose.$2.yml" up -d
+ env $(cat "$BASE_DIR/.env.$DB_NAME" | xargs) docker compose -f "$BASE_DIR/docker-compose.$DB_NAME.yml" up -d
;;
stop)
- env $(cat "$BASE_DIR/.env.$2" | xargs) docker compose -f "$BASE_DIR/docker-compose.$2.yml" down
+ env $(cat "$BASE_DIR/.env.$DB_NAME" | xargs) docker compose -f "$BASE_DIR/docker-compose.$DB_NAME.yml" down
;;
*)
echo "Error: Invalid command. Use 'start' or 'stop'."
exit 1
;;
-esac
+ esac
+}
+
+# elementIn checks whether a element is in an array.
+#
+# Args:
+# $1: The element
+# $2: The array
+#
+# Returns:
+# 0 if the element is in the array; 1 otherwise.
+function elementIn() {
+ local e match="$1"
+ shift
+ for e; do [[ "$e" == "$match" ]] && return 0; done
+ return 1
+}
+
+main "$@"