Created
July 18, 2020 08:34
-
-
Save albertogalan/957a11d15c385c07d0be219a3fbf984c 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
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 | 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
Wow Thanks! This is inspiring! I make some adjustment to fit my need along with bug fixes:
It' also ported to ZSH