Skip to content

Instantly share code, notes, and snippets.

@mberglof
Created September 24, 2019 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mberglof/2efd1c82376e08533875e7e2a9f0d472 to your computer and use it in GitHub Desktop.
Save mberglof/2efd1c82376e08533875e7e2a9f0d472 to your computer and use it in GitHub Desktop.
Bash completion for scaleft mainly to enable ssh completion
#!/usr/bin/env bash
# vim: ts=2 sw=2 et
# requirements: pcregrep and sft
# For each of the words, get options with pcregrep -o -- '--\w+-?(\w+)?'
WORDS=$(sft -h | pcregrep -o '^ \w+(-?)(\w+)?' | sed -e 's/^ //' | tr "\n" " ")
SERVERS=()
# Get servers, minus header
_sft_list_servers() {
while IFS='' read -r line; do SERVERS+=("$line"); done < <(sft list-servers --columns hostname 2> /dev/null | tail -n +2)
}
_sft_completions() {
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
case ${COMP_CWORD} in
1)
COMPREPLY=($(compgen -W "$WORDS" -- "${cur}"))
;;
2)
case ${prev} in
ssh)
if [ ${#SERVERS} -lt 1 ]; then
_sft_list_servers
fi
COMPREPLY=($(compgen -W "${SERVERS[*]}" -- "${cur}"))
;;
esac
esac
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi
}
complete -F _sft_completions sft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment