Skip to content

Instantly share code, notes, and snippets.

@yang-l
Created October 2, 2022 21:18
Show Gist options
  • Save yang-l/f2a61d951f02d5f75d20c3c7f1f900ea to your computer and use it in GitHub Desktop.
Save yang-l/f2a61d951f02d5f75d20c3c7f1f900ea to your computer and use it in GitHub Desktop.
# based on - https://gist.github.com/albertogalan/957a11d15c385c07d0be219a3fbf984c
# background - https://gist.github.com/jbriales/ea475a155f1f7bbb6731e39e8764f1d1
function jq() {
if [ -f $1 ]; then
FILE=$1; shift
# Move FILE at the end as expected by native jq
command jq "$@" "$FILE"
else
command jq "$@"
fi
}
function _jq() {
COMPREPLY=()
local curr prev
curr=$2
prev=$3
#set -x
case $COMP_CWORD in
1)
COMPREPLY=( $(compgen -f -- $curr) )
;;
2)
keys=$(command jq -c 'paths | if (. | length) == 2 then [(.[0] + "[" + (.[1] | tostring) + "]")] else . end | map(. | tostring) | join(".")' $prev | tr -d '"' | sed 's=^=\.=')
COMPREPLY=( $(compgen -W "$keys" -- $curr ) )
;;
esac
}
complete -F _jq jq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment