diff --git a/scwrypts/remote/configure b/scwrypts/remote/configure index aa66928..2626fe7 100755 --- a/scwrypts/remote/configure +++ b/scwrypts/remote/configure @@ -2,5 +2,5 @@ ##################################################################### MAIN() { - EDIT "$REMOTE_CONNECTIONS_FILE" + utils.io.edit "$REMOTE_CONNECTIONS_FILE" } diff --git a/scwrypts/remote/connect b/scwrypts/remote/connect index b06c65a..89a1d69 100755 --- a/scwrypts/remote/connect +++ b/scwrypts/remote/connect @@ -5,7 +5,7 @@ use config --group remote USAGE__options=" -c, --command override configured remote command - -s, --shell override configured remote login shell + -s, --shell override configured remote login shell -t, --type one of the following connection types: - ssh (default) simple ssh execution - xserver ssh connection with remote-xserver settings @@ -30,7 +30,7 @@ USAGE__description=" MAIN() { [ $REMOTE__TARGET ] \ - || ERROR 'missing REMOTE__TARGET context; this must be run through scwrypts' \ + || echo.error 'missing REMOTE__TARGET context; this must be run through scwrypts' \ || return 1 local CONNECTION_TYPE=ssh @@ -45,38 +45,38 @@ MAIN() { do local _S=1 case $1 in - -t | --type ) ((_S+=1)); CONNECTION_TYPE=$2 ;; - -c | --command ) ((_S+=1)); REMOTE_COMMAND=$2 ;; - -s | --shell ) ((_S+=1)); LOGIN_SHELL=$2 ;; + ( -t | --type ) ((_S+=1)); CONNECTION_TYPE=$2 ;; + ( -c | --command ) ((_S+=1)); REMOTE_COMMAND=$2 ;; + ( -s | --shell ) ((_S+=1)); LOGIN_SHELL=$2 ;; - --no-rc ) LOAD_RC=false ;; - --no-tty ) USE_TTY=false ;; + ( --no-rc ) LOAD_RC=false ;; + ( --no-tty ) USE_TTY=false ;; - --use-bastion ) + ( --use-bastion ) ((_S+=1)) USE_BASTION=$2 case $USE_BASTION in - true | false ) ;; - * ) ERROR "invalid setting for '--use-bastion' (must be 'true' or 'false')" ;; + ( true | false ) ;; + ( * ) echo.error "invalid setting for '--use-bastion' (must be 'true' or 'false')" ;; esac ;; - --force-local-login ) FORCE_LOCAL_LOGIN=true ;; + ( --force-local-login ) FORCE_LOCAL_LOGIN=true ;; - * ) ERROR "unknown argument '$1'" ;; + * ) echo.error "unknown argument '$1'" ;; esac [[ $_S -le $# ]] \ && shift $_S \ - || ERROR "missing argument for '$1'" \ + || echo.error "missing argument for '$1'" \ || shift $# done - CHECK_ERRORS + utils.check-errors --fail ########################################## GET_SSH_ARGS() { - REMOTE__GET_SSH_ARGS \ + remote.config.get-ssh-args \ --type $CONNECTION_TYPE \ --use-tty $USE_TTY \ $REMOTE_NAME \ @@ -89,14 +89,14 @@ MAIN() { ########################################## - local CONNECTION_STRING=$(REMOTE__GET_CONNECTION_STRING $REMOTE_NAME) + local CONNECTION_STRING=$(remote.config.get-connection-string $REMOTE_NAME) [ $CONNECTION_STRING ] \ || FAIL 1 'unable to determine connection string' ########################################## LOGIN_SHELL=$(\ - REMOTE__QUERY_CONNECTION_WITH_FALLBACK \ + remote.config.query-connection-with-fallback \ "$LOGIN_SHELL" \ ".$REMOTE_NAME.shell" \ ".default.shell" \ @@ -104,7 +104,7 @@ MAIN() { ) REMOTE_COMMAND=$(\ - REMOTE__QUERY_CONNECTION_WITH_FALLBACK \ + remote.config.query-connection-with-fallback \ "$REMOTE_COMMAND" \ ".sessions.$REMOTE_NAME.$CONNECTION_TYPE.command" \ ".sessions.$REMOTE_NAME.command" \ @@ -114,9 +114,9 @@ MAIN() { [ $REMOTE_COMMAND ] || { case $CONNECTION_TYPE in - tmux ) + ( tmux ) local TMUX_SESSION_NAME=$( - REMOTE__QUERY_CONNECTION_WITH_FALLBACK \ + remote.config.query-connection-with-fallback \ ".sessions.$REMOTE_NAME.tmux.session" \ ".default.tmux.session" \ "wryn" \ @@ -135,7 +135,7 @@ MAIN() { [ $USE_BASTION ] || { USE_BASTION=$(\ - REMOTE__QUERY_CONNECTION_WITH_FALLBACK \ + remote.config.query-connection-with-fallback \ ".sessions.$REMOTE_NAME.bastion.preferred" \ 'false' \ ; @@ -145,15 +145,15 @@ MAIN() { local BASTION_HOST [[ $USE_BASTION =~ true ]] && { BASTION_HOST=$(\ - REMOTE__QUERY_CONNECTION_WITH_FALLBACK \ + remote.config.query-connection-with-fallback \ ".sessions.$REMOTE_NAME.bastion.session" \ ) } [ $BASTION_HOST ] && { - DEBUG "REMOTE_COMMAND : $REMOTE_COMMAND" - PASSTHROUGH_COMMAND="$(GET_PASSTHROUGH_PREFIX) connect $REMOTE_NAME --" + echo.debug "REMOTE_COMMAND : $REMOTE_COMMAND" + PASSTHROUGH_COMMAND="$(remote.bastion.get-passthrough-prefix) connect $REMOTE_NAME --" [ "$REMOTE_COMMAND" ] && PASSTHROUGH_COMMAND+=" -c $(printf "%q " "$REMOTE_COMMAND")" BASTION_TARGET=$REMOTE_NAME REMOTE__TARGET=$BASTION_HOST MAIN --command "$PASSTHROUGH_COMMAND" @@ -165,7 +165,7 @@ MAIN() { [ $REMOTE_COMMAND ] && [[ $LOAD_RC =~ true ]] && { REMOTE_COMMAND="$LOGIN_SHELL -l -c 'source ~/.$(basename $LOGIN_SHELL)rc &>/dev/null; $REMOTE_COMMAND'" } - + [ ! $REMOTE_COMMAND ] && { [[ $LOAD_RC =~ true ]] \ && REMOTE_COMMAND="$LOGIN_SHELL -l" \ @@ -174,14 +174,14 @@ MAIN() { [ $BASTION_TARGET ] && CONNECTION_TYPE=bastion - DEBUG " + echo.debug " attempting execution: netpath : $(hostnamectl --static) -> $([ $BASTION_TARGET ] && echo "$BASTION_TARGET -> ")$REMOTE_NAME type : $CONNECTION_TYPE connection : $REMOTE_NAME command : \"$REMOTE_COMMAND\" " - + case $CONNECTION_STRING in localhost | $USER@localhost ) eval "cd; $REMOTE_COMMAND" @@ -189,6 +189,6 @@ MAIN() { ;; esac - DEBUG "ssh ${SSH_ARGS[@]} $CONNECTION_STRING \"$REMOTE_COMMAND\"" + echo.debug "ssh ${SSH_ARGS[@]} $CONNECTION_STRING \"$REMOTE_COMMAND\"" ssh ${SSH_ARGS[@]} $CONNECTION_STRING "$REMOTE_COMMAND" } diff --git a/scwrypts/remote/lib/bastion.module.zsh b/scwrypts/remote/lib/bastion.module.zsh index 2310725..b7513a2 100644 --- a/scwrypts/remote/lib/bastion.module.zsh +++ b/scwrypts/remote/lib/bastion.module.zsh @@ -1,3 +1,3 @@ -GET_PASSTHROUGH_PREFIX() { +${scwryptsmodule}.get-passthrough-prefix() { echo "source ~/.zshrc &>/dev/null; SUBSCWRYPT=$((SUBSCWRYPT+1)) SCWRYPTS_LOG_LEVEL=$SCWRYPTS_LOG_LEVEL scwrypts" } diff --git a/scwrypts/remote/lib/config.module.zsh b/scwrypts/remote/lib/config.module.zsh index a81eecd..eb527eb 100644 --- a/scwrypts/remote/lib/config.module.zsh +++ b/scwrypts/remote/lib/config.module.zsh @@ -4,19 +4,19 @@ DEPENDENCIES+=(yq) ##################################################################### -REMOTE__GET_CONNECTION_STRING() { +${scwryptsmodule}.get-connection-string() { local REMOTE_NAME="$1" - [ $(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.host) ] \ - || ERROR "no such connection $REMOTE_NAME exists" \ + [ $(remote.config.query-connection .sessions.$REMOTE_NAME.host) ] \ + || echo.error "no such connection $REMOTE_NAME exists" \ || return 1 - local CONNECTION_HOST=$(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.host) + local CONNECTION_HOST=$(remote.config.query-connection .sessions.$REMOTE_NAME.host) [ $CONNECTION_HOST ] \ - || ERROR "connection $REMOTE_NAME is misconfigured; missing 'host' field" \ + || echo.error "connection $REMOTE_NAME is misconfigured; missing 'host' field" \ || return 1 - local CONNECTION_USER=$(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.user) - [ $CONNECTION_USER ] || CONNECTION_USER=$(REMOTE__QUERY_CONNECTION .default.user) + local CONNECTION_USER=$(remote.config.query-connection .sessions.$REMOTE_NAME.user) + [ $CONNECTION_USER ] || CONNECTION_USER=$(remote.config.query-connection .default.user) [ $CONNECTION_USER ] \ && CONNECTION_STRING="${CONNECTION_USER}@${CONNECTION_HOST}" \ @@ -26,7 +26,7 @@ REMOTE__GET_CONNECTION_STRING() { echo $CONNECTION_STRING } -REMOTE__GET_SSH_ARGS() { +${scwryptsmodule}.get-ssh-args() { local REMOTE_NAME local TYPE=ssh local USE_TTY=true @@ -34,13 +34,13 @@ REMOTE__GET_SSH_ARGS() { while [[ $# -gt 0 ]] do case $1 in - -t | --type ) TYPE=$2; shift 1 ;; - --use-tty ) USE_TTY=$2; shift 1 ;; + ( -t | --type ) TYPE=$2; shift 1 ;; + ( --use-tty ) USE_TTY=$2; shift 1 ;; - --no-tty ) USE_TTY=false ;; + ( --no-tty ) USE_TTY=false ;; - * ) - [ $REMOTE_NAME ] && { ERROR "too many args :c"; return 1; } + ( * ) + [ $REMOTE_NAME ] && { echo.error "too many args :c"; return 1; } REMOTE_NAME=$1 ;; esac @@ -53,25 +53,25 @@ REMOTE__GET_SSH_ARGS() { return 0 } - local PORT=$(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.port) + local PORT=$(remote.config.query-connection .sessions.$REMOTE_NAME.port) [ $PORT ] && { case $TYPE in - ssh | xserver | tmux ) ARGS+=(-p $PORT) ;; - scp ) ARGS+=(-P $PORT) ;; # not really in use, just a sample - * ) - WARNING " + ( ssh | xserver | tmux ) ARGS+=(-p $PORT) ;; + ( scp ) ARGS+=(-P $PORT) ;; # not really in use, just a sample + ( * ) + WARNING " port is specified, but I'm not sure whether to use '-p' or '-P' if this command fails, try adding your --type to the appropriate - list in '$SCWRYPTS_ROOT__remote/lib/config.module.zsh' + list in '$(scwrypts.config.group remote root)/lib/config.module.zsh' " ARGS+=(-p $PORT) ;; esac } - ARGS+=($(REMOTE__QUERY_CONNECTION .session.$REMOTE_NAME.$TYPE.args)) + ARGS+=($(remote.config.query-connection .session.$REMOTE_NAME.$TYPE.args)) [[ $USE_TTY =~ true ]] && ARGS+=(-t) @@ -80,17 +80,17 @@ REMOTE__GET_SSH_ARGS() { ##################################################################### -REMOTE__QUERY_CONNECTION() { - YQ -oy -r $@ "$REMOTE_CONNECTIONS_FILE" \ +${scwryptsmodule}.query-connection() { + utils.yq -oy -r $@ "$REMOTE_CONNECTIONS_FILE" \ | grep -v ^null$ } -REMOTE__QUERY_CONNECTION_WITH_FALLBACK() { +${scwryptsmodule}.query-connection-with-fallback() { while [[ $# -gt 0 ]] && [ ! $QUERY_RESULT ] do case $1 in - .* ) QUERY_RESULT=$(REMOTE__QUERY_CONNECTION $1) ;; - * ) QUERY_RESULT="$1" ;; # allows raw default value + ( .* ) QUERY_RESULT=$(remote.config.query-connection $1) ;; + ( * ) QUERY_RESULT="$1" ;; # allows raw default value esac shift 1 done diff --git a/scwrypts/remote/lib/omni.module.zsh b/scwrypts/remote/lib/omni.module.zsh new file mode 100644 index 0000000..cd59c66 --- /dev/null +++ b/scwrypts/remote/lib/omni.module.zsh @@ -0,0 +1,13 @@ +##################################################################### + +OMNI_SOCKET="omni.socket" + +OMNI_LOGDIR="${XDG_STATE_HOME:-${HOME}/.local/state}/wryn/omni" +mkdir -p "${OMNI_LOGDIR}" + +${scwryptsmodule}.tmux() { + tmux -L ${OMNI_SOCKET} $@ +} + + +##################################################################### diff --git a/scwrypts/remote/lib/tmux.module.zsh b/scwrypts/remote/lib/tmux.module.zsh new file mode 100644 index 0000000..e69de29 diff --git a/scwrypts/remote/remote.scwrypts.zsh b/scwrypts/remote/remote.scwrypts.zsh index a76c4fa..2b43d10 100644 --- a/scwrypts/remote/remote.scwrypts.zsh +++ b/scwrypts/remote/remote.scwrypts.zsh @@ -1,14 +1,12 @@ -SCWRYPTS_GROUPS+=(remote) -[ $DOTWRYN ] || source "$HOME/.config/wryn/env.zsh" +readonly ${scwryptsgroup}__type=zsh +readonly ${scwryptsgroup}__color=$(utils.colors.blue) -export SCWRYPTS_TYPE__remote=zsh -export SCWRYPTS_ROOT__remote="$DOTWRYN/scwrypts/remote" -export SCWRYPTS_COLOR__remote='\033[0;34m' +##################################################################### DEPENDENCIES+=(yq) -REMOTE_CONNECTIONS_FILE="$HOME/.config/wryn/remote-connections.toml" +REMOTE_CONNECTIONS_FILE="${XDG_CONFIG_HOME:-${HOME}/.config}/wryn/remote-connections.toml" -SCWRYPTS__LIST_AVAILABLE_SCWRYPTS__remote() { +${scwryptsgroup}.list-available() { [ -f "$REMOTE_CONNECTIONS_FILE" ] || { mkdir -p "$(dirname -- "$REMOTE_CONNECTIONS_FILE")" &>/dev/null echo " @@ -27,20 +25,25 @@ SCWRYPTS__LIST_AVAILABLE_SCWRYPTS__remote() { echo "tmux/omni" echo "configure" echo "test" - } | sed "s|^|$SCWRYPTS_TYPE__remote/|" + } | sed "s|^|zsh/|" } -SCWRYPTS__GET_RUNSTRING__remote__zsh() { +${scwryptsgroup}.zsh.get-runstring() { local SCWRYPT_FILENAME case $SCWRYPT_NAME in - connect/* ) - SCWRYPT_FILENAME="$SCWRYPTS_ROOT__remote/connect" + connect/* ) + SCWRYPT_FILENAME="$(scwrypts.config.group remote root)/connect" echo "export REMOTE__TARGET=$(echo $SCWRYPT_NAME | sed 's|^.*connect/||')" ;; * ) - SCWRYPT_FILENAME="$SCWRYPTS_ROOT__remote/$SCWRYPT_NAME" + SCWRYPT_FILENAME="$(scwrypts.config.group remote root)/$SCWRYPT_NAME" ;; esac - SCWRYPTS__GET_RUNSTRING__zsh__generic "$SCWRYPT_FILENAME" + scwrypts.get-runstring.zsh.generic +} + +remote.config.yq() { + utils.yq -oy -r $@ "${REMOTE_CONNECTIONS_FILE}" \ + | grep -v ^null$ } diff --git a/scwrypts/remote/test b/scwrypts/remote/test index 12393ff..550ff05 100755 --- a/scwrypts/remote/test +++ b/scwrypts/remote/test @@ -34,9 +34,9 @@ MAIN() { -n | --name ) ((_S+=1)) REMOTE_NAME=$2 - CONNECTION_STRING=$(REMOTE__GET_CONNECTION_STRING $REMOTE_NAME) + CONNECTION_STRING=$(remote.config.get-connection-string $REMOTE_NAME) ;; - -s | --connection_string ) + -s | --connection_string ) ((_S+=1)) CONNECTION_STRING="$2" ;; @@ -44,7 +44,7 @@ MAIN() { ((_S+=1)) TIMEOUT_SECONDS=$2 [[ $TIMEOUT_SECONDS -gt 0 ]] \ - || ERROR "invalid timeout seconds '$TIMEOUT_SECONDS'" + || echo.error "invalid timeout seconds '$TIMEOUT_SECONDS'" ;; -c | --command ) ((_S+=1)) @@ -55,31 +55,31 @@ MAIN() { USE_BASTION=$2 case $USE_BASTION in true | false ) ;; - * ) ERROR "invalid setting for '--use-bastion' (must be 'true' or 'false')" ;; + * ) echo.error "invalid setting for '--use-bastion' (must be 'true' or 'false')" ;; esac ;; - * ) ERROR "unrecognized argument '$1'" ;; + * ) echo.error "unrecognized argument '$1'" ;; esac [[ $_S -le $# ]] \ && shift $_S \ - || ERROR "missing argument for '$1'" \ + || echo.error "missing argument for '$1'" \ || shift $# done [ $CONNECTION_STRING ] \ - || ERROR "unable to determine connection string" + || echo.error "unable to determine connection string" [ $USE_BASTION ] || { USE_BASTION=$(\ - REMOTE__QUERY_CONNECTION_WITH_FALLBACK \ + remote.config.query-connection-with-fallback \ ".sessions.$REMOTE_NAME.bastion.preferred" \ 'false' \ ; ) } - CHECK_ERRORS + utils.check-errors --fail ########################################## @@ -87,46 +87,46 @@ MAIN() { local BASTION_HOST [[ $USE_BASTION =~ true ]] && { BASTION_HOST=$(\ - REMOTE__QUERY_CONNECTION_WITH_FALLBACK \ + remote.config.query-connection-with-fallback \ ".sessions.$REMOTE_NAME.bastion.session" \ ) } [[ $USE_BASTION =~ true ]] && { [ $BASTION_HOST ] \ - || ERROR "cannot connect to $REMOTE_NAME; no configured bastion host" \ + || echo.error "cannot connect to $REMOTE_NAME; no configured bastion host" \ || return 1 } - + case $CONNECTION_STRING in localhost | $USER@localhost ) CONNECTION_TEST() { return 0; } # current user on local machine can always connect ;; - * ) + * ) [[ $USE_BASTION =~ true ]] && { - DEBUG "MAIN -n $BASTION_HOST -c \"$(GET_PASSTHROUGH_PREFIX) remote test -- -n $REMOTE_NAME -c \"$COMMAND\"\"" - BASTION_TARGET="$REMOTE_NAME" MAIN -n $BASTION_HOST -c "$(GET_PASSTHROUGH_PREFIX) remote test -- -n $REMOTE_NAME -c \"$COMMAND\"" + echo.debug "MAIN -n $BASTION_HOST -c \"$(remote.bastion.get-passthrough-prefix) remote test -- -n $REMOTE_NAME -c \"$COMMAND\"\"" + BASTION_TARGET="$REMOTE_NAME" MAIN -n $BASTION_HOST -c "$(remote.bastion.get-passthrough-prefix) remote test -- -n $REMOTE_NAME -c \"$COMMAND\"" return $? } CONNECTION_TEST() { [ $REMOTE_NAME ] && { - [[ $(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.enabled) =~ false ]] && { + [[ $(remote.config.query-connection .sessions.$REMOTE_NAME.enabled) =~ false ]] && { return 1 } } local REMOTE_ARGS=() - REMOTE_ARGS+=($(REMOTE__GET_SSH_ARGS --type ssh $REMOTE_NAME)) + REMOTE_ARGS+=($(remote.config.get-ssh-args --type ssh $REMOTE_NAME)) REMOTE_ARGS+=(-o BatchMode=yes) - DEBUG "attempting\ntimeout $TIMEOUT_SECONDS ssh $REMOTE_ARGS $CONNECTION_STRING "'\'"\"$COMMAND"'\'"\"" >&2 + echo.debug "attempting\ntimeout $TIMEOUT_SECONDS ssh $REMOTE_ARGS $CONNECTION_STRING "'\'"\"$COMMAND"'\'"\"" >&2 timeout --foreground $TIMEOUT_SECONDS ssh ${REMOTE_ARGS[@]} "$CONNECTION_STRING" "$COMMAND" >&2 } ;; esac [ $REMOTE_NAME ] || REMOTE_NAME=explicit - STATUS "testing connection $CONNECTION_STRING ($REMOTE_NAME$([ $BASTION_TARGET ] && echo " -> $BASTION_TARGET"))" \ + echo.status "testing connection $CONNECTION_STRING ($REMOTE_NAME$([ $BASTION_TARGET ] && echo " -> $BASTION_TARGET"))" \ && CONNECTION_TEST \ - && SUCCESS "successfully connected to '$CONNECTION_STRING' ($REMOTE_NAME)" \ - || ERROR "connection to '$CONNECTION_STRING ($REMOTE_NAME)' failed" \ + && echo.success "successfully connected to '$CONNECTION_STRING' ($REMOTE_NAME)" \ + || echo.error "connection to '$CONNECTION_STRING ($REMOTE_NAME)' failed" \ } diff --git a/scwrypts/remote/tmux/manager b/scwrypts/remote/tmux/manager index 553add8..fa2d574 100755 --- a/scwrypts/remote/tmux/manager +++ b/scwrypts/remote/tmux/manager @@ -1,66 +1,56 @@ #!/usr/bin/env zsh # -# works as a standalone zsh script +# must work as a standalone zsh script # +eval "$(scwrypts --config)" \ + && use omni --group remote \ + && utils.check-environment \ + || { echo 'scwrypts config error; aborting' >&2; sleep 5; return 1; } ##################################################################### -CONNECTIONS_FILE="$HOME/.config/wryn/remote-connections.toml" +OMNI_LOGFILE="${OMNI_LOGDIR}/omni.current.txt" -OMNI_LOGDIR="$HOME/.local/share/scwrypts/dotwryn" -OMNI_LOGFILE="$HOME/.local/share/scwrypts/dotwryn/omni.current.txt" - -[ ! -d "$OMNI_LOGDIR" ] && mkdir -p "$OMNI_LOGDIR" - -[ -f "$OMNI_LOGFILE" ] && { +[ -f "${OMNI_LOGFILE}" ] && { for x in {1..99} do - [ ! -f "$OMNI_LOGDIR/omni.$x.txt" ] && break + [ ! -f "${OMNI_LOGDIR}/omni.${x}.txt" ] && { + mv "${OMNI_LOGFILE}" "${OMNI_LOGDIR}/omni.${x}.txt" + break + } done - - [ ! -f "$OMNI_LOGDIR/omni.$x.txt" ] && mv "$OMNI_LOGFILE" "$OMNI_LOGDIR/omni.$x.txt" } -echo "OMNI MANAGER START : $(date)" > "$OMNI_LOGFILE" +echo "OMNI MANAGER START : $(date)" > "${OMNI_LOGFILE}" -OMNI_SOCKET="omni.socket" -OMNI_TMUX() { tmux -L $OMNI_SOCKET $@; } +##################################################################### -CONFIG_QUERY() { yq -oy -r $@ "$CONNECTIONS_FILE" | grep -v ^null$; } - -CONNECTED() { - [ $WINDOW_ID ] \ - && echo "\\033[1;32mconnected (window $WINDOW_ID)\\033[0m" >&2 \ - || echo "\\033[1;32mconnected\\033[0m" >&2 \ -} -DISCONNECTED() { echo "\\033[1;31mdisconnected\\033[0m" >&2; } - -GET_UNIQUE_WINDOW_ID() { +get-unique-window-id() { local MODE=use-default - local WINDOW_ID="$(eval "echo \$WINDOW_ID__$REMOTE_NAME")" + local WINDOW_ID="$(eval echo "\$WINDOW_ID__${REMOTE_NAME}")" : \ - && [[ $(eval "echo \$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \ - && [ $WINDOW_ID ] \ - && echo $WINDOW_ID \ + && [[ $(eval echo "\$CAN_CONNECT__${REMOTE_NAME}") =~ true ]] \ + && [ ${WINDOW_ID} ] \ + && echo ${WINDOW_ID} \ && return 0 \ ; - unset WINDOW_ID__$REMOTE_NAME &>>"$OMNI_LOGFILE" + unset WINDOW_ID__${REMOTE_NAME} &>>"${OMNI_LOGFILE}" WINDOW_ID="$1" - WINDOW_ID=$(CONFIG_QUERY .sessions.$REMOTE_NAME.id) - [ $WINDOW_ID ] && MODE=specify-preferred + WINDOW_ID=$(remote.config.yq .sessions.${REMOTE_NAME}.id) + [ ${WINDOW_ID} ] && MODE=specify-preferred WINDOW_ID_IS_TAKEN() { : \ - && [[ $(OMNI_TMUX list-windows -t=omni | grep "^$WINDOW_ID:" | wc -l) -gt 0 ]] \ - && [[ ! $(OMNI_TMUX list-windows -t=omni | grep "^$WINDOW_ID:" | awk '{print $2;}' =~ ^$REMOTE_NAME$) ]] \ + && [[ $(remote.omni.tmux list-windows -t=omni | grep "^${WINDOW_ID}:" | wc -l) -gt 0 ]] \ + && [[ ! $(remote.omni.tmux list-windows -t=omni | grep "^${WINDOW_ID}:" | awk '{print $2;}' =~ ^${REMOTE_NAME}$) ]] \ ; } local FALLBACK_STARTING_POINT - case $MODE in + case ${MODE} in use-default ) FALLBACK_STARTING_POINT=42 ;; specify-preferred ) FALLBACK_STARTING_POINT=69 ;; esac @@ -68,78 +58,78 @@ GET_UNIQUE_WINDOW_ID() { local I=0 while WINDOW_ID_IS_TAKEN do - echo "$REMOTE_NAME tried to acquire window id $WINDOW_ID, but it is already in-use (trying $(($FALLBACK_STARTING_POINT-$I)))" >&2 - WINDOW_ID=$(($FALLBACK_STARTING_POINT-$I)) + echo "${REMOTE_NAME} tried to acquire window id ${WINDOW_ID}, but it is already in-use (trying $((${FALLBACK_STARTING_POINT}-${I})))" >&2 + WINDOW_ID=$((${FALLBACK_STARTING_POINT}-${I})) ((I+=1)) - [[ $I -gt 10 ]] && WINDOW_ID= && break + [[ ${I} -gt 10 ]] && WINDOW_ID= && break done - echo $WINDOW_ID + echo ${WINDOW_ID} } ##################################################################### -sleep 1 -OMNI_TMUX new-window -t=omni-manager -dn 'harakiri' " +remote.omni.tmux new-window -t=omni-manager -dn 'harakiri' " +echo 'waiting for omni-manager to close' while true do - tmux -L $OMNI_SOCKET list-sessions | grep -v omni-manager | grep -qi omni || { - tmux -L $OMNI_SOCKET kill-session -t omni-manager - } sleep 5 + tmux -L ${OMNI_SOCKET} list-sessions 2>/dev/null | grep -v omni-manager | grep -qi omni || { + echo 'no active sessions detected; killing omni-manager session' + tmux -L ${OMNI_SOCKET} kill-session -t omni-manager + } done " while true do - clear - for REMOTE_NAME in $(CONFIG_QUERY '.sessions | keys | .[]') + #clear + for REMOTE_NAME in $(remote.config.yq '.sessions | keys | .[]') do - WINDOW_ID=$(GET_UNIQUE_WINDOW_ID) - export WINDOW_ID__$REMOTE_NAME=$WINDOW_ID + WINDOW_ID=$(get-unique-window-id) + export WINDOW_ID__${REMOTE_NAME}=${WINDOW_ID} - OMNI_TMUX list-windows -t=omni | awk '{print $2;}' | grep -q $REMOTE_NAME \ + remote.omni.tmux list-windows -t=omni | awk '{print $2;}' | grep -q ${REMOTE_NAME} \ && continue - printf "testing connection $REMOTE_NAME..." - echo "testing connection $REMOTE_NAME..." &>>"$OMNI_LOGFILE" + printf "testing connection ${REMOTE_NAME}..." + echo "testing connection ${REMOTE_NAME}..." &>>"${OMNI_LOGFILE}" scwrypts --name test --group remote --type zsh \ -- \ - --name $REMOTE_NAME \ + --name ${REMOTE_NAME} \ --command 'command -v tmux' \ - &>>"$OMNI_LOGFILE" \ - && export CAN_CONNECT__$REMOTE_NAME=true \ - || export CAN_CONNECT__$REMOTE_NAME=false \ + &>>"${OMNI_LOGFILE}" \ + && export CAN_CONNECT__${REMOTE_NAME}=true \ + || export CAN_CONNECT__${REMOTE_NAME}=false \ ; - [[ $(eval echo "\$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \ - && echo " \\033[1;32m✔\\033[0m" >&2 \ - || { echo " \\033[1;31m✖\\033[0m" >&2; continue; } + [[ $(eval echo "\$CAN_CONNECT__${REMOTE_NAME}") =~ true ]] \ + && utils.colors.print bright-green " ✔\n" >&2 \ + || { utils.colors.print bright-red " ✖\n" >&2; continue; } - OMNI_TMUX new-window \ - -t=omni:$WINDOW_ID \ - -dn $REMOTE_NAME " + remote.omni.tmux new-window \ + -t=omni:${WINDOW_ID} \ + -dn ${REMOTE_NAME} " source ~/.zshrc &>/dev/null - TMUX='' scwrypts -n connect/$REMOTE_NAME -- --type tmux + TMUX='' scwrypts -n connect/${REMOTE_NAME} -- --type tmux echo 'connection closed' sleep 2 " - OMNI_TMUX list-window -t=omni | awk '{print $1;}' | grep -q '99:' && OMNI_TMUX kill-window -t omni:99 + remote.omni.tmux list-window -t=omni | awk '{print $1;}' | grep -q '99:' && remote.omni.tmux kill-window -t omni:99 - OMNI_TMUX list-windows -t=omni | awk '{print $2;}' | grep -q $REMOTE_NAME \ - || export CAN_CONNECT__$REMOTE_NAME=false + remote.omni.tmux list-windows -t=omni | awk '{print $2;}' | grep -q ${REMOTE_NAME} \ + || export CAN_CONNECT__${REMOTE_NAME}=false done - clear + #clear echo "connections:\n" - local STATUS { - for REMOTE_NAME in $(CONFIG_QUERY '.sessions | keys | .[]') + for REMOTE_NAME in $(remote.config.yq '.sessions | keys | .[]') do - [[ $(eval "echo \$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \ - && echo "${REMOTE_NAME}^$(eval "echo \$WINDOW_ID__$REMOTE_NAME")^\\033[1;32mconnected\\033[0m" \ - || echo "${REMOTE_NAME}^-^\\033[1;31mdisconnected\\033[0m" \ + [[ $(eval echo "\$CAN_CONNECT__${REMOTE_NAME}") =~ true ]] \ + && echo "${REMOTE_NAME}^$(eval echo "\$WINDOW_ID__${REMOTE_NAME}")^$(utils.colors.print bright-green connected)" \ + || echo "${REMOTE_NAME}^-^$(utils.colors.print bright-red disconnected)" \ ; done } | column -ts '^' diff --git a/scwrypts/remote/tmux/omni b/scwrypts/remote/tmux/omni index e35ea2c..4bc40b5 100755 --- a/scwrypts/remote/tmux/omni +++ b/scwrypts/remote/tmux/omni @@ -1,5 +1,10 @@ #!/bin/zsh +##################################################################### + DEPENDENCIES+=(tmux scwrypts) + +use omni --group remote + ##################################################################### USAGE__description=" @@ -12,7 +17,7 @@ USAGE__description=" Switch between connections by using 'M-s' followed by the session ID number (or use any other default default tmux navigation command). Full configuration can be found here: - - $SCWRYPTS_ROOT__remote/omni/tmux.conf + - $(scwrypts.config.group remote root)/omni/tmux.conf Shut-down the omni-session by pressing 'M-Q' (Q not q! ALT + SHIFT + Q) @@ -20,7 +25,7 @@ USAGE__description=" A background process periodically attempts to refresh lost connections. Immediate retry can be forced with 'M-R'. - + Running this command in an existing tmux session will likely result in an infinite loop, so: - don't run the omni-session in a TMUX session @@ -30,7 +35,7 @@ USAGE__description=" ##################################################################### MAIN() { - [[ $TERM =~ tmux ]] && ERROR "\n Cannot run tmux-omni within a tmux session!\n " + [[ $TERM =~ tmux ]] && echo.error "\n Cannot run tmux-omni within a tmux session!\n " local BACKGROUND_LAUNCH=false @@ -43,33 +48,34 @@ MAIN() { esac [[ $_S -le $# ]] \ && shift $_S \ - || ERROR "missing argument for '$1'" \ + || echo.error "missing argument for '$1'" \ || shift $# \ ; done - CHECK_ERRORS + utils.check-errors --fail - local OMNI_SOCKET="omni.socket" - OMNI_TMUX() { tmux -L $OMNI_SOCKET $@; } + local SCWRYPTS_ROOT_REMOTE="$(scwrypts.config.group remote root)" - OMNI_TMUX list-sessions 2>/dev/null | grep -v omni-manager | grep -qi omni || { - STATUS "initializing omni server" - OMNI_TMUX kill-session -t=omni-manager >/dev/null 2>&1 - - OMNI_TMUX -f "$SCWRYPTS_ROOT__remote/tmux/tmux.conf" new -d -s omni \ - "echo searching for first connection...; sleep 30" \; \ - split-window "sleep 3; TMUX= tmux -L $OMNI_SOCKET a -t=omni-manager" \; \ + remote.omni.tmux list-sessions 2>/dev/null | grep -v omni-manager | grep -qi omni || { + echo.status "initializing omni server" + remote.omni.tmux kill-session -t=omni-manager >/dev/null 2>&1 + + echo.debug "${SCWRYPTS_ROOT_REMOTE}/tmux/tmux.conf" + remote.omni.tmux -f "${SCWRYPTS_ROOT_REMOTE}/tmux/tmux.conf" new -d -s omni \ + "echo searching for first connection...; sleep 10" \; \ + split-window "sleep 1; TMUX= tmux -L ${OMNI_SOCKET} a -t=omni-manager" \; \ move-window -t 99 \; - - OMNI_TMUX new -d -s omni-manager "$SCWRYPTS_ROOT__remote/tmux/manager" + + + remote.omni.tmux new -d -s omni-manager "${SCWRYPTS_ROOT_REMOTE}/tmux/manager" } [[ $BACKGROUND_LAUNCH =~ true ]] && { - SUCCESS "omni server activated" + echo.success "omni server activated" return 0 } - STATUS 'connecting to omni server' - OMNI_TMUX a -t=omni + echo.status 'connecting to omni server' + remote.omni.tmux a -t=omni } diff --git a/scwrypts/remote/tmux/tmux.conf b/scwrypts/remote/tmux/tmux.conf index eed1d22..59b1066 100644 --- a/scwrypts/remote/tmux/tmux.conf +++ b/scwrypts/remote/tmux/tmux.conf @@ -57,14 +57,12 @@ bind-key -n M-q send-keys M-q bind-key -n M-z send-keys M-z bind-key -n M-w send-keys C-M-w -bind-key -n M-Q kill-session - unbind-key C-b bind-key C-b send-prefix set-option -g prefix M-s bind-key M-s send-keys M-s -bind-key -n M-Q kill-session +bind-key -n M-Q kill-server # force reload now bind-key -n M-R send-keys -t omni-manager ENTER