From fdaf0f7144935aa7b1f1293cc370535e9e85f9fc Mon Sep 17 00:00:00 2001 From: Andrei Mikhailov Date: Tue, 2 Jun 2020 01:21:14 -0300 Subject: [PATCH] bash completion for yq read; requires jq --- utils/yq-completion.bash | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 utils/yq-completion.bash diff --git a/utils/yq-completion.bash b/utils/yq-completion.bash new file mode 100644 index 00000000..51ef5aae --- /dev/null +++ b/utils/yq-completion.bash @@ -0,0 +1,41 @@ + +_yq() { + local cur prev coms opts car cdr x + coms="compare delete help merge new prefix read validate write" + cur="${COMP_WORDS[COMP_CWORD]}" + if [[ ${cur} == -* ]] ; then + COMPREPLY=() + else + case "$COMP_CWORD" in + "1") + COMPREPLY=( $(compgen -W "$coms" -- $cur) ) + ;; + "2") + if [[ "${COMP_WORDS[1]}" == read ]] ; then + [ "$cur" ] && { p="" ; s="*" ; } || { p="./" ; s="" ; } + cur1=$(echo "$cur" | sed -e 's!^~!'$HOME'!g') + COMPREPLY=( $(x="$p$cur1$s" ; echo "$x") ) + else + COMPREPLY=() + fi + ;; + "3") + if [[ "${COMP_WORDS[1]}" == read ]] ; then + partkey="$(echo $cur| sed -e 's/[^.]*$//g' -e 's/\.$//g')" + [ "$partkey" ] && { x="\"${partkey}.\" + keys[]" ; } || { x="keys[]" ; } + COMPREPLY=( $(yq read ${COMP_WORDS[2]} ${partkey} -j | + jq "$x" -r 2>&- | + grep ${cur}. + ) ) + else + COMPREPLY=() + fi + ;; + *) + COMPREPLY=() + ;; + esac + fi + return 0 +} +complete -o nospace -F _yq yq