-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.sh
More file actions
46 lines (41 loc) · 1.89 KB
/
Copy pathlib.sh
File metadata and controls
46 lines (41 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# lib.sh - shared helpers for the _db toolkit.
# Source AFTER db.conf so DBHOST/DBUSER/DBPASS/DBNAME/... are available.
# Optional config values default to empty so `set -u` scripts can rely on them.
: "${DBPORT:=}"
: "${DOCKER_MYSQLD_CONTAINER:=}"
: "${DOCKER_COMPOSE_SERVICE:=}"
# Pick the available Docker Compose CLI: v2 plugin (`docker compose`) preferred,
# legacy v1 (`docker-compose`) as a fallback.
COMPOSE="docker-compose"
if [ -n "$DOCKER_COMPOSE_SERVICE" ] && docker compose version >/dev/null 2>&1; then
COMPOSE="docker compose"
fi
# Build the client invocations WITHOUT the password (passed via MYSQL_PWD).
_port=""
[ -n "$DBPORT" ] && _port=" -P${DBPORT}"
MYSQL_OPTS="-h${DBHOST}${_port} -u${DBUSER} --default-character-set=utf8mb4"
MYSQL_CMD="mysql ${MYSQL_OPTS}"
MYSQLDUMP_CMD="mysqldump ${MYSQL_OPTS}"
# db_exec <tty|notty> <command-string>
#
# Runs <command-string> against the configured backend and forwards stdin and
# stdout, so callers can pipe SQL in (`echo ... | db_exec`) or capture output
# (`db_exec ... > dump.sql`). Backends:
# - DOCKER_MYSQLD_CONTAINER set -> `docker exec` into that container
# - DOCKER_COMPOSE_SERVICE set -> `$COMPOSE exec` into that service
# - neither set -> run the local client against DBHOST/DBPORT
# The password is provided via MYSQL_PWD, never on the command line.
db_exec() {
_mode=$1
_cmd=$2
if [ -n "$DOCKER_MYSQLD_CONTAINER" ]; then
if [ "$_mode" = tty ]; then _tty="-it"; else _tty="-i"; fi
docker exec $_tty -e MYSQL_PWD="$DBPASS" "$DOCKER_MYSQLD_CONTAINER" sh -c "$_cmd"
elif [ -n "$DOCKER_COMPOSE_SERVICE" ]; then
# -T disables pseudo-TTY allocation, required for piping/redirection.
if [ "$_mode" = tty ]; then _tty=""; else _tty="-T"; fi
$COMPOSE exec $_tty -e MYSQL_PWD="$DBPASS" "$DOCKER_COMPOSE_SERVICE" sh -c "$_cmd"
else
MYSQL_PWD="$DBPASS" bash -c "$_cmd"
fi
}