formatted defaults.completion.bash

pull/1782/head
Gurkirat Singh 2021-01-10 18:16:25 +05:30
parent 32d76a6145
commit 87edb2d65b
No known key found for this signature in database
GPG Key ID: CFD64E1DCB3DA835
1 changed files with 110 additions and 110 deletions

View File

@ -13,12 +13,12 @@ _defaults_domains()
COMPREPLY=() COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
local domains=$( defaults domains | sed -e 's/, /:/g' | tr : '\n' | sed -e 's/ /\\ /g' | grep -i "^$cur" ) local domains=$( defaults domains | sed -e 's/, /:/g' | tr : '\n' | sed -e 's/ /\\ /g' | grep -i "^$cur" )
local IFS=$'\n' local IFS=$'\n'
COMPREPLY=( $domains ) COMPREPLY=( $domains )
if [[ $( echo '-app' | grep "^$cur" ) ]]; then if [[ $( echo '-app' | grep "^$cur" ) ]]; then
COMPREPLY[${#COMPREPLY[@]}]="-app" COMPREPLY[${#COMPREPLY[@]}]="-app"
fi fi
return 0 return 0
} }
@ -26,119 +26,119 @@ _defaults_domains()
_defaults() _defaults()
{ {
local cur prev host_opts cmds cmd domain keys key_index local cur prev host_opts cmds cmd domain keys key_index
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
host_opts='-currentHost -host' host_opts='-currentHost -host'
cmds='read read-type write rename delete domains find help' cmds='read read-type write rename delete domains find help'
if [[ $COMP_CWORD -eq 1 ]]; then if [[ $COMP_CWORD -eq 1 ]]; then
COMPREPLY=( $( compgen -W "$host_opts $cmds" -- $cur ) ) COMPREPLY=( $( compgen -W "$host_opts $cmds" -- $cur ) )
return 0 return 0
elif [[ $COMP_CWORD -eq 2 ]]; then elif [[ $COMP_CWORD -eq 2 ]]; then
if [[ "$prev" == "-currentHost" ]]; then if [[ "$prev" == "-currentHost" ]]; then
COMPREPLY=( $( compgen -W "$cmds" -- $cur ) ) COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
return 0 return 0
elif [[ "$prev" == "-host" ]]; then elif [[ "$prev" == "-host" ]]; then
return 0 return 0
_known_hosts -a _known_hosts -a
else else
_defaults_domains _defaults_domains
return 0 return 0
fi fi
elif [[ $COMP_CWORD -eq 3 ]]; then elif [[ $COMP_CWORD -eq 3 ]]; then
if [[ ${COMP_WORDS[1]} == "-host" ]]; then if [[ ${COMP_WORDS[1]} == "-host" ]]; then
_defaults_domains _defaults_domains
return 0 return 0
fi fi
fi fi
# Both a domain and command have been specified # Both a domain and command have been specified
if [[ ${COMP_WORDS[1]} == [${cmds// /|}] ]]; then if [[ ${COMP_WORDS[1]} == [${cmds// /|}] ]]; then
cmd=${COMP_WORDS[1]} cmd=${COMP_WORDS[1]}
domain=${COMP_WORDS[2]} domain=${COMP_WORDS[2]}
key_index=3 key_index=3
if [[ "$domain" == "-app" ]]; then if [[ "$domain" == "-app" ]]; then
if [[ $COMP_CWORD -eq 3 ]]; then if [[ $COMP_CWORD -eq 3 ]]; then
# Completing application name. Can't help here, sorry # Completing application name. Can't help here, sorry
return 0 return 0
fi fi
domain="-app ${COMP_WORDS[3]}" domain="-app ${COMP_WORDS[3]}"
key_index=4 key_index=4
fi fi
elif [[ ${COMP_WORDS[2]} == "-currentHost" ]] && [[ ${COMP_WORDS[2]} == [${cmds// /|}] ]]; then elif [[ ${COMP_WORDS[2]} == "-currentHost" ]] && [[ ${COMP_WORDS[2]} == [${cmds// /|}] ]]; then
cmd=${COMP_WORDS[2]} cmd=${COMP_WORDS[2]}
domain=${COMP_WORDS[3]} domain=${COMP_WORDS[3]}
key_index=4 key_index=4
if [[ "$domain" == "-app" ]]; then if [[ "$domain" == "-app" ]]; then
if [[ $COMP_CWORD -eq 4 ]]; then if [[ $COMP_CWORD -eq 4 ]]; then
# Completing application name. Can't help here, sorry # Completing application name. Can't help here, sorry
return 0 return 0
fi fi
domain="-app ${COMP_WORDS[4]}" domain="-app ${COMP_WORDS[4]}"
key_index=5 key_index=5
fi fi
elif [[ ${COMP_WORDS[3]} == "-host" ]] && [[ ${COMP_WORDS[3]} == [${cmds// /|}] ]]; then elif [[ ${COMP_WORDS[3]} == "-host" ]] && [[ ${COMP_WORDS[3]} == [${cmds// /|}] ]]; then
cmd=${COMP_WORDS[3]} cmd=${COMP_WORDS[3]}
domain=${COMP_WORDS[4]} domain=${COMP_WORDS[4]}
key_index=5 key_index=5
if [[ "$domain" == "-app" ]]; then if [[ "$domain" == "-app" ]]; then
if [[ $COMP_CWORD -eq 5 ]]; then if [[ $COMP_CWORD -eq 5 ]]; then
# Completing application name. Can't help here, sorry # Completing application name. Can't help here, sorry
return 0 return 0
fi fi
domain="-app ${COMP_WORDS[5]}" domain="-app ${COMP_WORDS[5]}"
key_index=6 key_index=6
fi fi
fi fi
keys=$( defaults read $domain 2>/dev/null | sed -n -e '/^ [^}) ]/p' | sed -e 's/^ \([^" ]\{1,\}\) = .*$/\1/g' -e 's/^ "\([^"]\{1,\}\)" = .*$/\1/g' | sed -e 's/ /\\ /g' ) keys=$( defaults read $domain 2>/dev/null | sed -n -e '/^ [^}) ]/p' | sed -e 's/^ \([^" ]\{1,\}\) = .*$/\1/g' -e 's/^ "\([^"]\{1,\}\)" = .*$/\1/g' | sed -e 's/ /\\ /g' )
case $cmd in case $cmd in
read|read-type) read|read-type)
# Complete key # Complete key
local IFS=$'\n' local IFS=$'\n'
COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) )
;; ;;
write) write)
if [[ $key_index -eq $COMP_CWORD ]]; then if [[ $key_index -eq $COMP_CWORD ]]; then
# Complete key # Complete key
local IFS=$'\n' local IFS=$'\n'
COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) )
elif [[ $((key_index+1)) -eq $COMP_CWORD ]]; then elif [[ $((key_index+1)) -eq $COMP_CWORD ]]; then
# Complete value type # Complete value type
# Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces # Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces
local value_types='-string -data -integer -float -boolean -date -array -array-add -dict -dict-add' local value_types='-string -data -integer -float -boolean -date -array -array-add -dict -dict-add'
local cur_type=$( defaults read-type $domain ${COMP_WORDS[key_index]} 2>/dev/null | sed -e 's/^Type is \(.*\)/-\1/' -e's/dictionary/dict/' | grep "^$cur" ) local cur_type=$( defaults read-type $domain ${COMP_WORDS[key_index]} 2>/dev/null | sed -e 's/^Type is \(.*\)/-\1/' -e's/dictionary/dict/' | grep "^$cur" )
if [[ $cur_type ]]; then if [[ $cur_type ]]; then
COMPREPLY=( $cur_type ) COMPREPLY=( $cur_type )
else else
COMPREPLY=( $( compgen -W "$value_types" -- $cur ) ) COMPREPLY=( $( compgen -W "$value_types" -- $cur ) )
fi fi
elif [[ $((key_index+2)) -eq $COMP_CWORD ]]; then elif [[ $((key_index+2)) -eq $COMP_CWORD ]]; then
# Complete value # Complete value
# Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces # Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces
COMPREPLY=( $( defaults read $domain ${COMP_WORDS[key_index]} 2>/dev/null | grep -i "^${cur//\\/\\\\}" ) ) COMPREPLY=( $( defaults read $domain ${COMP_WORDS[key_index]} 2>/dev/null | grep -i "^${cur//\\/\\\\}" ) )
fi fi
;; ;;
rename) rename)
if [[ $key_index -eq $COMP_CWORD ]] || if [[ $key_index -eq $COMP_CWORD ]] ||
[[ $((key_index+1)) -eq $COMP_CWORD ]]; then [[ $((key_index+1)) -eq $COMP_CWORD ]]; then
# Complete source and destination keys # Complete source and destination keys
local IFS=$'\n' local IFS=$'\n'
COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) )
fi fi
;; ;;
delete) delete)
if [[ $key_index -eq $COMP_CWORD ]]; then if [[ $key_index -eq $COMP_CWORD ]]; then
# Complete key # Complete key
local IFS=$'\n' local IFS=$'\n'
COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) )
fi fi
;; ;;
esac esac
return 0 return 0
} }