Last active
June 6, 2023 04:26
-
-
Save crpb/adb9b7b92711d494a5be998d8cfb07c2 to your computer and use it in GitHub Desktop.
helper to lookup some truenas midclt calls on the fly / also works w/o tmux / fzf required!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# v=0.5 2023-06-06 | |
# ~cb | |
# | |
# # TMUX | |
# cat << EOTMUX >> ~/.tmux.conf | |
# # TrueNAS MIDCLT-QUERYs | |
# bind-key C-t popup -E tmux-midclt | |
# EOTMUX | |
# TODO: Add auto-paste in last active window | |
NAS="10.10.2.2" | |
CLIPBOARD=True | |
# Copy text to the clipboard | |
# disable/enable to match your environment.. | |
cp_to_clipboard() { | |
if [[ "$(uname)" == "Linux" ]] && binary_exist "xsel"; then | |
echo -n "$1" | xsel -b | |
elif [[ "$(uname)" == "Linux" ]] && binary_exist "xclip"; then | |
echo -n "$1" | xclip -i | |
elif [[ "$(uname)" == "Linux" ]] && binary_exist "wl-copy"; then | |
echo -n "$1" | wl-copy | |
elif [[ "$(uname)" == "Darwin" ]] && binary_exist "pbcopy"; then | |
echo -n "$1" | pbcopy | |
else | |
return 1 | |
fi | |
} | |
# Check if binary exist | |
binary_exist() { | |
local binary=$1 | |
command -v "$binary" &> /dev/null | |
return $? | |
} | |
main(){ | |
QUERYS="${QUERYS:-"$(curl -sk -L "https://$NAS/api/docs/websocket" | \ | |
grep -Po '(\w+\.)+(general|summary|config|choices|query|check_available|(g|s)et(\w+))' |sort -u)"}" | |
#export QUERYS | |
FILTER="${*}" | |
if ! { [[ "$TERM" =~ "screen"* ]] && [ -n "$TMUX" ]; } then | |
FZF="fzf-tmux";FZFOPTS="-i"; | |
else | |
FZF="fzf-tmux";FZFOPTS="-p 20%,20%"; | |
fi | |
OUT="midclt call $(echo "$QUERYS" | \ | |
${FZF} "${FZFOPTS}" --no-multi --query="${FILTER}"| \ | |
awk '{print $(NF -0)}' | sed 's/\"//g') |jq '.[]'" | |
if [ -n "$CLIPBOARD" ]; then | |
cp_to_clipboard "$OUT" | |
else | |
printf "%s" "$OUT" | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment