From 6120bd900f0eae2f9be7b8e86e5c643dc65f124f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Sun, 29 Dec 2024 17:45:41 +0100 Subject: [PATCH] feat(zsh): add ssh-menu and get-dump --- .gitignore | 2 ++ zsh/get-dump | 51 +++++++++++++++++++++++++++++++++++ zsh/ssh-menu | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100755 zsh/get-dump create mode 100755 zsh/ssh-menu diff --git a/.gitignore b/.gitignore index 3f6dd84..926ea03 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,8 @@ zsh/* !zsh/aliases.zsh !zsh/completions/.keep !zsh/.p10k.zsh +!zsh/ssh-menu +!zsh/get-dump # tmux !tmux diff --git a/zsh/get-dump b/zsh/get-dump new file mode 100755 index 0000000..6dd2a63 --- /dev/null +++ b/zsh/get-dump @@ -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 "$@" diff --git a/zsh/ssh-menu b/zsh/ssh-menu new file mode 100755 index 0000000..3d52e50 --- /dev/null +++ b/zsh/ssh-menu @@ -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 <&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