feat(zsh): add ssh-menu and get-dump

This commit is contained in:
Matej Janezic 2024-12-29 17:45:41 +01:00
parent 71fa00e26b
commit 6120bd900f
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
3 changed files with 128 additions and 0 deletions

2
.gitignore vendored
View File

@ -22,6 +22,8 @@ zsh/*
!zsh/aliases.zsh !zsh/aliases.zsh
!zsh/completions/.keep !zsh/completions/.keep
!zsh/.p10k.zsh !zsh/.p10k.zsh
!zsh/ssh-menu
!zsh/get-dump
# tmux # tmux
!tmux !tmux

51
zsh/get-dump Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
function get-dump {
if [[ -z $1 ]]; then
host="$(rg -N --no-heading 'Host .*' ~/.ssh \
| sed 's/Host \(.*\)/\1/' \
| fzf --cycle \
--bind 'tab:toggle-up,btab:toggle-down' \
--delimiter ':' \
--with-nth 2 \
--header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \
--border "double" \
--border-label "get-dump" \
--ansi \
--highlight-line \
| cut -d ":" -f2
)"
if [[ -z $host ]]; then
return
fi
else
host=$1
fi
selected=$(ssh "$host" docker ps --format "{{.Names}}" \
| fzf --cycle \
--bind 'tab:toggle-up,btab:toggle-down' \
--header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \
--border "double" \
--border-label "get-dump" \
--ansi \
--highlight-line \
)
if [[ -z $selected ]]; then
echo "not provided"
return
fi
if ! [[ -d "./dumps" ]]; then
mkdir "./dumps"
fi
location="./dumps/${host}_${selected}_$(date +'%s')"
echo "dumping to $location"
ssh "$host" docker exec "$selected" pg_dump -U db --format=c db >"$location"
}
get-dump "$@"

75
zsh/ssh-menu Executable file
View File

@ -0,0 +1,75 @@
#!/usr/bin/env bash
function _ssh_menu_preview {
file=$1; shift
host=$1; shift
j=$(cat $file \
| jc --ssh-conf \
| jq -r ".[] \
| select(.host == \"$host\") \
| .jumps = (\
if (.proxyjump | type == \"array\" and length > 0) then \
(.proxyjump | join(\",\")) \
else \
\"null\" \
end \
)" \
)
user=$(jq -r '.user' <<<$j)
port=$(jq -r '.port' <<<$j)
hostname=$(jq -r '.hostname' <<<$j)
jumps=$(jq -r '.jumps' <<<$j)
echo "$(cat <<EOF
User $user
HostName $hostname\
$([[ $port != "null" ]] && echo -e "\nPort $port")\
$([[ $jumps != "null" ]] && echo -e "\nProxyJump $jumps")
command:
ssh $user@$hostname$([[ $port != "null" ]] && echo -n " -p $port")$([[ $jumps != "null" ]] && echo -n " -J $jumps")
EOF
)"
echo ""
if command -v host 2>&1 >/dev/null; then
host $hostname
echo ""
fi
ping -c 1 -W 1 $hostname &>/dev/null \
&& echo "Host is reachable!" \
|| echo "Host is not reachable!"
}
export -f _ssh_menu_preview
function ssh-menu {
selected="$(rg -N --no-heading 'Host .*' ~/.ssh \
| sed 's/Host \(.*\)/\1/' \
| SHELL=$(which bash) fzf --cycle \
--bind 'tab:toggle-up,btab:toggle-down' \
--delimiter ':' \
--with-nth 2 \
--preview "_ssh_menu_preview {1} {2}" \
--preview-label "ssh config info" \
--header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \
--border "double" \
--border-label "ssh-menu" \
--ansi \
--highlight-line \
| cut -d ":" -f2
)"
if [[ -z $selected ]]; then
return
fi
ssh "$selected"
}
ssh-menu