commit 0ed44a333cbc1e8994d3a9211660fc7879a4abc5 parent 0d1bcca7348361912b1d0c1146723ef09dc97d88 Author: Tomas Nemec <nemi@skaut.cz> Date: Mon, 3 Jan 2022 08:43:55 +0100 update Diffstat:
A | md | | | 164 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 164 insertions(+), 0 deletions(-)
diff --git a/md b/md @@ -0,0 +1,164 @@ +#!/usr/bin/env zsh + +MDCFG="$XDG_CONFIG_HOME/mdcfg" + +[ -f $MDCFG ] || exit 1 + +local p_start=0 +local p_restart=0 +local p_attach='' +local p_stop=0 +local p_info=0 +local p_debug=0 + +local opt +while getopts da:srthi opt; do + case $opt in + d) + p_debug=1 + ;; + a) + p_attach=$OPTARG + ;; + s) + p_start=1 + ;; + t) + p_stop=1 + ;; + r) + p_restart=1 + ;; + i) + p_info=1 + ;; + h) + cat <<EOF +Dtach shortucts + +USAGE: + md [FLAGS] [<app>...] + +FLAGS: + -d Print debug messages + -a <app> Attach + -s Start + -r Restart + -i <app> Print info about app w/o proceeding + -h Print usage w/o proceeding +EOF + exit 0 + ;; + esac +done +((OPTIND > 1)) && shift $((OPTIND - 1)) +typeset -a apps +apps=($*) + +E() { + print $@ >&2 + exit 1 +} + +D() { + [[ $p_debug -eq 1 ]] && print $@ +} + +D Actions\\n debug: $p_debug\\n info: $p_info\\n start: $p_start\\n stop: $p_stop\\n restart: $p_restart\\n attach: $p_attach + +# LOAD CONFIG +. $MDCFG + +info() { + local app="$1" + local cwd="$2" + local cmd="$3" + cat <<EOF +$app + CWD: $cwd + CMD: $cmd +EOF +} + +[[ $p_info -eq 1 && $#apps -eq 0 ]] && apps=(${(k)serves}) +typeset -a valid +for app in ${apps[@]}; do + local cwd="${cwds[$app]}" + local serve_cmd="${serves[$app]}" + [[ -z $cwd || -z $serve_cmd ]] && E "$app not found." + [[ $p_info -eq 1 ]] && info "$app" "$cwd" "$serve_cmd" + valid=($app $valid) +done +[[ $p_info -eq 1 ]] && exit 0 +D Valid apps: $valid + +spath() { + name="$1" + echo "/tmp/dpgw_$name" +} + +start() { + local app="$1" + local cmd="$2" + local cwd="$3" + session=$(spath $app) + (cd $cwd && dtach -n $session zsh -c "$cmd") +} + +stop() { + local app="$1" + local session=$(spath $app) + if [[ -e $session ]]; then + echo -ne '\x03' | dtach -p $session + else + E $session not exist. + fi +} + +restart() { + local app="$1" + local cmd="$2" + local cwd="$3" + local session=$(spath $app) + stop $app + while test -e $session; do sleep 0.1; done + start $app "$cmd" "$cwd" +} + +attach() { + local app="$1" + local session=$(spath $app) + if [[ -e $session ]]; then + dtach -a $session -r winch + else + E $session not exist. + fi +} + +# TODO(tms) 23.11.21: List +# TODO(tms) 23.11.21: Possibility to stop all dpgw_* sessions + +if [[ $p_stop -eq 1 ]]; then + for app in ${valid[@]}; do + D Stopping $app + stop $app + done +fi + +if [[ $p_start -eq 1 ]]; then + for app in ${valid[@]}; do + D Starting $app + start $app "${serves[$app]}" "${cwds[$app]}" + done +fi + +if [[ $p_restart -eq 1 ]]; then + for app in ${valid[@]}; do + D Restarting $app + restart $app "${serves[$app]}" "${cwds[$app]}" + done +fi + +if [[ -n $p_attach ]]; then + attach $p_attach +fi