Skip to content

Commit

Permalink
WIP Add displaying attached container host and user
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-sol committed May 10, 2024
1 parent 4bd013e commit 3ec5472
Showing 1 changed file with 54 additions and 10 deletions.
64 changes: 54 additions & 10 deletions scripts/shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,52 @@ set_tmux_option() {
}

get_remote_info() {
local command=$1
local pane_pid=$(tmux display-message -p "#{pane_pid}")

# First get the current pane command pid to get the full command with arguments
local cmd=$({ pgrep -flaP $pane_pid ; ps -o command -p $pane_pid ; } | xargs -I{} echo {} | grep ssh | sed -E 's/^[0-9]*[[:blank:]]*ssh //')
local cmd=$(_current_pane_command | grep -E 'ssh' | sed -E 's/^[0-9]*[[:blank:]]*ssh //')
# Fetch configuration with given cmd
ssh -G $cmd 2>/dev/null | grep -E -e '^host\s' -e '^port\s' -e '^user\s' | sort | cut -f 2 -d ' ' | xargs
}

get_container_info() {
local cmd=$(_current_pane_command | grep -E 'docker|podman' | sed -E 's/^[0-9]*[[:blank:]]* //')

local runner=${cmd%% *}
if [[ $cmd =~ ' --name' ]]; then
local container=$(echo ${cmd##* --name} | cut -f 1 -d ' ')
container=${container##*=}
else
# @TODO get dynamic named container
# local all_running_containers=$($runner ps -q | xargs $runner inspect)
return
fi

local info=$($runner inspect --format '{{ .Config.Hostname }}/{{ .Config.Domainname }}/{{ .Config.User }}' $container)
local host=$(echo $info | cut -f 1 -d '/')
local domain=$(echo $info | cut -f 2 -d '/')
local user=$(echo $info | cut -f 3 -d '/')
# @TODO `port` is not applicable with container for now
echo "${host}${domain:+.$domain} 0 ${user}"
}

get_info() {
local host port user
# If command is ssh, fetch connection info
if ssh_connected; then
read -r host port user <<<$(get_remote_info)
fi
if containered; then
read -r host port user <<<$(get_container_info)
fi
# Return requested info
case "$1" in
"user") # user from ssh info or `whoami`
[[ -z "$user" ]] && whoami || echo "$user"
[[ -z "$user" ]] && _get_username || echo "$user"
;;
"host") # host from ssh info or `hostname`
[[ -z "$host" ]] && hostname || echo "$host"
[[ -z "$host" ]] && _get_hostname || echo "$host"
;;
"host_short") # host from ssh info or `hostname -s`
[[ -z "$host" ]] && hostname -s || echo "$host"
[[ -z "$host" ]] && _get_hostname_short || echo "$host"
;;
"port") # port from ssh info or empty
echo "$port"
Expand All @@ -49,10 +71,32 @@ get_info() {
}

ssh_connected() {
local cmd=$(_current_pane_command)

[[ $cmd =~ " ssh " ]] || [[ $cmd =~ " sshpass " ]]
}

containered() {
local cmd=$(_current_pane_command)

[[ $cmd =~ " docker " ]] || [[ $cmd =~ " podman " ]]
}

_current_pane_command() {
local pane_pid=$(tmux display-message -p "#{pane_pid}")

# Get current pane command
local cmd=$(pgrep -flaP $pane_pid)
# @TODO research, maybe `pgrep -flaP $pane_pid` is enough
{ pgrep -flaP $pane_pid; ps -o command -p $pane_pid; } | xargs -I{} echo {}
}

_get_username() {
command -v whoami && whoami
}

[[ $cmd =~ " ssh " ]] || [[ $cmd =~ " sshpass " ]]
_get_hostname() {
command -v hostname && hostname || echo "${HOSTNAME}"
}

_get_hostname_short() {
command -v hostname && hostname --short || echo "${HOSTNAME%%.*}"
}

0 comments on commit 3ec5472

Please sign in to comment.