nutools/cx-conndev

87 lines
2.4 KiB
Plaintext
Raw Permalink Normal View History

2019-05-21 09:40:15 +04:00
#!/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]
2019-06-19 23:22:17 +04:00
$scriptname -c|-d [-uUSER [-pPASSWORD]] ...
2019-05-21 09:40:15 +04:00
OPTIONS
-h, -s, --service SERVICE
2019-06-19 14:50:34 +04:00
Spécifier le nom du service. La valeur par défaut est db
-c, --mysql-cmd
2019-06-19 14:50:34 +04:00
Lancer 'mysql -hSERVICE_IP' avec les arguments supplémentaires fournis
sur la ligne de commande
2019-06-19 23:22:17 +04:00
-d, --mysqldump-cmd
Lancer 'mysqldump --databases --add-drop-database -hSERVICE_IP' avec
les arguments supplémentaires fournis sur la ligne de commande
2019-06-19 14:50:34 +04:00
-u, --user USER
-p, --password PASSWORD
2019-06-19 23:22:17 +04:00
Paramètres supplémentaires utilisés uniquement avec les options -c et -d"
2019-05-21 09:40:15 +04:00
}
service=db
mysql_cmd=
2019-06-19 23:22:17 +04:00
mysqldump_cmd=
2019-06-19 14:50:34 +04:00
user=
password=
2019-05-21 09:40:15 +04:00
args=(
--help '$exit_with display_help'
-h:,-s:,--service: service=
-c,--mysql-cmd mysql_cmd=1
2019-06-19 23:22:17 +04:00
-d,--mysqldump-cmd mysqldump_cmd=1
2019-06-19 14:50:34 +04:00
-u:,--user: user=
-p:,--password: password=
2019-05-21 09:40:15 +04:00
)
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"
###
2019-07-15 09:11:52 +04:00
function die_not_found() { die "$service: service introuvable. vérifiez que le projet est actuellement up"; }
2019-05-24 11:49:11 +04:00
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"
2019-05-21 09:40:15 +04:00
if [ -n "$mysql_cmd" ]; then
2019-06-19 14:50:34 +04:00
mysql -h"$ip" ${user:+-u"$user"} ${password:+-p"$password"} "$@"
2019-06-19 23:22:17 +04:00
elif [ -n "$mysqldump_cmd" ]; then
mysqldump --databases --add-drop-database -h"$ip" ${user:+-u"$user"} ${password:+-p"$password"} "$@"
2019-06-19 14:50:34 +04:00
else
mysql_cmd="$(php bin/conndev.php "$ip" "$@")"
edebug "mysql_cmd: $mysql_cmd"
2019-05-21 09:40:15 +04:00
eval "$mysql_cmd"
2019-06-19 14:50:34 +04:00
fi