87 lines
2.4 KiB
Bash
Executable File
87 lines
2.4 KiB
Bash
Executable File
#!/bin/bash
|
|
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
source "$(dirname "$0")/lib/ulib/ulib" || exit 1
|
|
urequire DEFAULTS
|
|
|
|
function display_help() {
|
|
uecho "$scriptname: se connecter à la base MySQL
|
|
|
|
USAGE
|
|
$scriptname [DATABASE]
|
|
$scriptname -c|-d [-uUSER [-pPASSWORD]] ...
|
|
|
|
OPTIONS
|
|
-h, -s, --service SERVICE
|
|
Spécifier le nom du service. La valeur par défaut est db
|
|
-c, --mysql-cmd
|
|
Lancer 'mysql -hSERVICE_IP' avec les arguments supplémentaires fournis
|
|
sur la ligne de commande
|
|
-d, --mysqldump-cmd
|
|
Lancer 'mysqldump --databases --add-drop-database -hSERVICE_IP' avec
|
|
les arguments supplémentaires fournis sur la ligne de commande
|
|
-u, --user USER
|
|
-p, --password PASSWORD
|
|
Paramètres supplémentaires utilisés uniquement avec les options -c et -d"
|
|
}
|
|
|
|
service=db
|
|
mysql_cmd=
|
|
mysqldump_cmd=
|
|
user=
|
|
password=
|
|
args=(
|
|
--help '$exit_with display_help'
|
|
-h:,-s:,--service: service=
|
|
-c,--mysql-cmd mysql_cmd=1
|
|
-d,--mysqldump-cmd mysqldump_cmd=1
|
|
-u:,--user: user=
|
|
-p:,--password: password=
|
|
)
|
|
parse_args "$@"; set -- "${args[@]}"
|
|
|
|
[ -n "$service" ] || die "Vous devez spécifier le nom du service"
|
|
|
|
found=
|
|
first=1
|
|
while true; do
|
|
if [ -f composer.json ]; then
|
|
found=1
|
|
break
|
|
fi
|
|
first=
|
|
cd ..
|
|
if [ "$(pwd)" == "$HOME" ]; then
|
|
die "Cette commande ne peut être lancée que depuis un projet Composer"
|
|
fi
|
|
done
|
|
if [ -z "$first" ]; then
|
|
enote "Le répertoire du projet est $(ppath . ~)"
|
|
fi
|
|
|
|
[ -f bin/conndev.php ] || die "Impossible de trouver le script compagnon conndev.php"
|
|
|
|
###
|
|
|
|
function die_not_found() { die "$service: service introuvable"; }
|
|
setx cid=docker-compose ps -q "$service" 2>/dev/null || die_not_found
|
|
[ -n "$cid" ] || die_not_found
|
|
edebug "$service id: $cid"
|
|
|
|
function die_not_found() { die "$service: $cid: ip introuvable"; }
|
|
setx ip=docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$cid" || die_not_found
|
|
[ -n "$ip" ] || die_not_found
|
|
edebug "$service ip: $ip"
|
|
|
|
if [ -n "$mysql_cmd" ]; then
|
|
mysql -h"$ip" ${user:+-u"$user"} ${password:+-p"$password"} "$@"
|
|
|
|
elif [ -n "$mysqldump_cmd" ]; then
|
|
mysqldump --databases --add-drop-database -h"$ip" ${user:+-u"$user"} ${password:+-p"$password"} "$@"
|
|
|
|
else
|
|
mysql_cmd="$(php bin/conndev.php "$ip" "$@")"
|
|
edebug "mysql_cmd: $mysql_cmd"
|
|
|
|
eval "$mysql_cmd"
|
|
fi
|