Last active
August 29, 2015 14:23
-
-
Save motemen/2b0656be9e2acf32b35d to your computer and use it in GitHub Desktop.
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
autoload -U modify-current-argument | |
autoload -U split-shell-arguments | |
complete-mackerel-host-ip () { | |
local apikey=$1 | |
local apikey_name=$2 | |
local mode_append_only=0 | |
local REPLY | |
local reply | |
local filter='fzf --nth=2,3 --delimiter="\t"' # or 'peco' | |
split-shell-arguments | |
if [ $(($REPLY % 2)) -eq 0 ]; then | |
# query by word under cursor | |
query_arg="--query=$reply[$REPLY]" | |
elif [ -n "${LBUFFER##* }" ]; then | |
# query by word jsut before cursor | |
query_arg="--query=${LBUFFER##* }" | |
else | |
# no word detected | |
query_arg='--query=' | |
mode_append_only=1 | |
fi | |
res=$(MACKEREL_APIKEY=${apikey:-$MACKEREL_APIKEY} MACKEREL_APIKEY_NAME=${apikey_name:-$MACKEREL_APIKEY_NAME} mkr-hosts-tsv | eval $filter "$query_arg") | |
if [ -z "$res" ]; then | |
zle reset-prompt | |
return 1 | |
fi | |
ip=$(echo "$res" | cut -f1) | |
host=$(echo "$res" | cut -f2) | |
if [ $mode_append_only = 1 ]; then | |
LBUFFER+="$ip" | |
else | |
modify-current-argument "$ip" | |
fi | |
BUFFER+=" # $host" | |
zle reset-prompt | |
} | |
zle -N complete-mackerel-host-ip |
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
#!/bin/sh | |
cache_file=~/.cache/mkr-hosts-tsv/cache | |
cache_minutes=1440 | |
if [ -n "$MACKEREL_APIKEY_NAME" ]; then | |
cache_file="$cache_file.$MACKEREL_APIKEY_NAME" | |
fi | |
if [ ! -r "$cache_file" -o -n "$(find \"$cache_file\" -mmin +$cache_minutes 2>/dev/null)" ]; then | |
mkdir -p $(dirname $cache_file) | |
echo 'cache file is too old, updating...' >&2 | |
mkr hosts -f '{{range .}}{{if (len .Interfaces)}}{{range .Interfaces}}{{if (eq .Name "eth0")}}{{.IPAddress}}{{end}}{{end}}{{"\t"}}{{.Name}}{{"\t"}}{{range $s,$rr := .Roles}}{{range $r := $rr}}{{$s}}:{{$r}} {{end}}{{end}}{{"\n"}}{{end}}{{end}}' | tee $cache_file | |
chmod go-r $cache_file | |
else | |
cat $cache_file | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment