-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.sh
42 lines (40 loc) · 1.05 KB
/
database.sh
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
#!/bin/zsh
function docker-db() {
local dbName=$1
local port=""
case $dbName in
{{#databases}}
{{shortName}})
local dbPath="{{dbPath}}"
local containerName={{containerName}}
port={{port}}
;;
{{/databases}}
*)
echo "no db up function found for: $1"
return false
;;
esac
if [[ "$2" == "stop" ]]; then
docker stop $containerName
elif [[ "$2" == "clear" ]]; then
docker stop $containerName
docker rm $containerName
elif [[ "$2" == "reset" ]]; then
docker stop $containerName
docker rm $containerName
_freePort $port $containerName
docker-compose -p $dbName -f $dbPath up -d
else
_freePort $port $containerName
docker-compose -p $dbName -f $dbPath up -d
fi
}
compdef '_values "docker dbs" {{short_string}}' docker-db
function _freePort() {
local toClose=`docker ps | awk -v port="0.0.0.0:$1->" '$0 ~ port { print $NF; exit }'`
if [ ! -z "$toClose" ] && [ $toClose != $2 ]; then
echo "stopping $toClose to free up port: $1"
docker stop $toClose
fi
}