Created
April 26, 2016 14:20
-
-
Save kazukgw/48315b95f5ea3a069853c379e8a8fd7d to your computer and use it in GitHub Desktop.
fzf と docker と yml と
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_yaml () { | |
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') | |
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \ | |
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | | |
awk -F$fs '{ | |
indent = length($1)/2; | |
vname[indent] = $2; | |
for (i in vname) {if (i > indent) {delete vname[i]}} | |
if (length($3) > 0) { | |
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("___")} | |
printf("%s%s=\"%s\"\n", vn, $2, $3); | |
} | |
}' | |
return 0 | |
} | |
function fzf_add_header() { | |
echo $1 | |
while read x; do | |
echo $x | |
done | |
} | |
function dcom() { | |
local all="__ALL__" | |
local args=("$@") | |
local shifted=("${args[@]:1}") | |
case "$1" in | |
"") | |
docker-compose | |
;; | |
version | ps | up) docker-compose ${args[@]} ;; | |
login) | |
local selected=$(docker-compose ps | awk 'NR > 2 {print $1}' | fzf) | |
if [[ "$selected" = "" ]]; then | |
echo "container is not found..." | |
else | |
docker exec -it $selected /bin/bash | |
fi | |
;; | |
# rm ) | |
# local selected=$(docker-compose ps | \ | |
# awk 'NR > 2 {print $1}' | \ | |
# fzf_add_header $all | fzf) | |
# if [[ "$selected" = "$all" ]]; then | |
# docker-compose ${args[@]} | |
# else | |
# docker-compose ${args[@]} $selected | |
# fi | |
# ;; | |
build | kill | start | stop | restart | scale | pause | unpause | logs | rm) | |
local selected=$(parse_yaml docker-compose.yml | awk -F'___' '{print $1}' | uniq |\ | |
fzf_add_header $all | fzf) | |
if [[ "$selected" = "$all" ]]; then | |
docker-compose ${args[@]} | |
else | |
docker-compose ${args[@]} $selected | |
fi | |
;; | |
*) | |
local selected=$(docker-compose ps | awk 'NR > 2 {print $1}' | fzf) | |
docker-compose ${args[@]} $selected | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment