blob: 5a8d0340d309c70e0bc7058283e346ee7e11c7c1 [file] [log] [blame]
Travis Swicegood39f8ef92012-02-18 21:52:10 -06001# defaults
2# Bash command line completion for defaults
3#
4# Created by Jonathon Mah on 2006-11-08.
5# Copyright 2006 Playhaus. All rights reserved.
6#
7# Version 1.0 (2006-11-08)
8
9
10_defaults_domains()
11{
12 local cur
13 COMPREPLY=()
14 cur=${COMP_WORDS[COMP_CWORD]}
15
16 local domains=$( defaults domains | sed -e 's/, /:/g' | tr : '\n' | sed -e 's/ /\\ /g' | grep -i "^$cur" )
17 local IFS=$'\n'
18 COMPREPLY=( $domains )
19 if [[ $( echo '-app' | grep "^$cur" ) ]]; then
20 COMPREPLY[${#COMPREPLY[@]}]="-app"
21 fi
22
23 return 0
24}
25
26
27_defaults()
28{
29 local cur prev host_opts cmds cmd domain keys key_index
30 cur=${COMP_WORDS[COMP_CWORD]}
31 prev=${COMP_WORDS[COMP_CWORD-1]}
32
33 host_opts='-currentHost -host'
34 cmds='read read-type write rename delete domains find help'
35
36 if [[ $COMP_CWORD -eq 1 ]]; then
37 COMPREPLY=( $( compgen -W "$host_opts $cmds" -- $cur ) )
38 return 0
39 elif [[ $COMP_CWORD -eq 2 ]]; then
40 if [[ "$prev" == "-currentHost" ]]; then
41 COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
42 return 0
43 elif [[ "$prev" == "-host" ]]; then
44 return 0
45 _known_hosts -a
46 else
47 _defaults_domains
48 return 0
49 fi
50 elif [[ $COMP_CWORD -eq 3 ]]; then
51 if [[ ${COMP_WORDS[1]} == "-host" ]]; then
52 _defaults_domains
53 return 0
54 fi
55 fi
56
57 # Both a domain and command have been specified
58
59 if [[ ${COMP_WORDS[1]} == [${cmds// /|}] ]]; then
60 cmd=${COMP_WORDS[1]}
61 domain=${COMP_WORDS[2]}
62 key_index=3
63 if [[ "$domain" == "-app" ]]; then
64 if [[ $COMP_CWORD -eq 3 ]]; then
65 # Completing application name. Can't help here, sorry
66 return 0
67 fi
68 domain="-app ${COMP_WORDS[3]}"
69 key_index=4
70 fi
71 elif [[ ${COMP_WORDS[2]} == "-currentHost" ]] && [[ ${COMP_WORDS[2]} == [${cmds// /|}] ]]; then
72 cmd=${COMP_WORDS[2]}
73 domain=${COMP_WORDS[3]}
74 key_index=4
75 if [[ "$domain" == "-app" ]]; then
76 if [[ $COMP_CWORD -eq 4 ]]; then
77 # Completing application name. Can't help here, sorry
78 return 0
79 fi
80 domain="-app ${COMP_WORDS[4]}"
81 key_index=5
82 fi
83 elif [[ ${COMP_WORDS[3]} == "-host" ]] && [[ ${COMP_WORDS[3]} == [${cmds// /|}] ]]; then
84 cmd=${COMP_WORDS[3]}
85 domain=${COMP_WORDS[4]}
86 key_index=5
87 if [[ "$domain" == "-app" ]]; then
88 if [[ $COMP_CWORD -eq 5 ]]; then
89 # Completing application name. Can't help here, sorry
90 return 0
91 fi
92 domain="-app ${COMP_WORDS[5]}"
93 key_index=6
94 fi
95 fi
96
97 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' )
98
99 case $cmd in
100 read|read-type)
101 # Complete key
102 local IFS=$'\n'
103 COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) )
104 ;;
105 write)
106 if [[ $key_index -eq $COMP_CWORD ]]; then
107 # Complete key
108 local IFS=$'\n'
109 COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) )
110 elif [[ $((key_index+1)) -eq $COMP_CWORD ]]; then
111 # Complete value type
112 # Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces
113 local value_types='-string -data -integer -float -boolean -date -array -array-add -dict -dict-add'
114 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" )
115 if [[ $cur_type ]]; then
116 COMPREPLY=( $cur_type )
117 else
118 COMPREPLY=( $( compgen -W "$value_types" -- $cur ) )
119 fi
120 elif [[ $((key_index+2)) -eq $COMP_CWORD ]]; then
121 # Complete value
122 # Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces
123 COMPREPLY=( $( defaults read $domain ${COMP_WORDS[key_index]} 2>/dev/null | grep -i "^${cur//\\/\\\\}" ) )
124 fi
125 ;;
126 rename)
127 if [[ $key_index -eq $COMP_CWORD ]] ||
128 [[ $((key_index+1)) -eq $COMP_CWORD ]]; then
129 # Complete source and destination keys
130 local IFS=$'\n'
131 COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) )
132 fi
133 ;;
134 delete)
135 if [[ $key_index -eq $COMP_CWORD ]]; then
136 # Complete key
137 local IFS=$'\n'
138 COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) )
139 fi
140 ;;
141 esac
142
143 return 0
144}
145
146complete -F _defaults -o default defaults
147
148
149# This file is licensed under the BSD license, as follows:
150#
151# Copyright (c) 2006, Playhaus
152# All rights reserved.
153#
154# Redistribution and use in source and binary forms, with or without
155# modification, are permitted provided that the following conditions are met:
156#
157# * Redistributions of source code must retain the above copyright notice, this
158# list of conditions and the following disclaimer.
159# * Redistributions in binary form must reproduce the above copyright notice,
160# this list of conditions and the following disclaimer in the documentation
161# and/or other materials provided with the distribution.
162# * Neither the name of the Playhaus nor the names of its contributors may be
163# used to endorse or promote products derived from this software without
164# specific prior written permission.
165#
166# This software is provided by the copyright holders and contributors "as is"
167# and any express or implied warranties, including, but not limited to, the
168# implied warranties of merchantability and fitness for a particular purpose are
169# disclaimed. In no event shall the copyright owner or contributors be liable
170# for any direct, indirect, incidental, special, exemplary, or consequential
171# damages (including, but not limited to, procurement of substitute goods or
172# services; loss of use, data, or profits; or business interruption) however
173# caused and on any theory of liability, whether in contract, strict liability,
174# or tort (including negligence or otherwise) arising in any way out of the use
175# of this software, even if advised of the possibility of such damage.