]> git.rmz.io Git - dotfiles.git/blob - zsh/plugins/knife/_knife
add xbindkeysrc
[dotfiles.git] / zsh / plugins / knife / _knife
1 #compdef knife
2
3 # These flags should be available everywhere according to man knife
4 knife_general_flags=( --help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes )
5
6 # knife has a very special syntax, some example calls are:
7 # knife status
8 # knife cookbook list
9 # knife role show ROLENAME
10 # knife data bag show DATABAGNAME
11 # knife role show ROLENAME --attribute ATTRIBUTENAME
12 # knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes
13
14 # The -Q switch in compadd allow for completions of things like "data bag" without having to go through two rounds of completion and avoids zsh inserting a \ for escaping spaces
15 _knife() {
16 local curcontext="$curcontext" state line
17 typeset -A opt_args
18 cloudproviders=(bluebox ec2 rackspace slicehost terremark)
19 _arguments \
20 '1: :->knifecmd'\
21 '2: :->knifesubcmd'\
22 '3: :->knifesubcmd2' \
23 '4: :->knifesubcmd3' \
24 '5: :->knifesubcmd4' \
25 '6: :->knifesubcmd5'
26
27 case $state in
28 knifecmd)
29 compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" exec environment index node recipe role search ssh status windows $cloudproviders
30 ;;
31 knifesubcmd)
32 case $words[2] in
33 (bluebox|ec2|rackspace|slicehost|terremark)
34 compadd "$@" server images
35 ;;
36 client)
37 compadd -Q "$@" "bulk delete" list create show delete edit reregister
38 ;;
39 configure)
40 compadd "$@" client
41 ;;
42 cookbook)
43 compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
44 ;;
45 environment)
46 compadd -Q "$@" list create delete edit show "from file"
47 ;;
48 node)
49 compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
50 ;;
51 recipe)
52 compadd "$@" list
53 ;;
54 role)
55 compadd -Q "$@" "bulk delete" create delete edit "from file" list show
56 ;;
57 windows)
58 compadd "$@" bootstrap
59 ;;
60 *)
61 _arguments '2:Subsubcommands:($(_knife_options1))'
62 esac
63 ;;
64 knifesubcmd2)
65 case $words[3] in
66 server)
67 compadd "$@" list create delete
68 ;;
69 images)
70 compadd "$@" list
71 ;;
72 site)
73 compadd "$@" vendor show share search download list unshare
74 ;;
75 (show|delete|edit)
76 _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
77 ;;
78 (upload|test)
79 _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
80 ;;
81 list)
82 compadd -a "$@" knife_general_flags
83 ;;
84 bag)
85 compadd -Q "$@" show edit list "from file" create delete
86 ;;
87 *)
88 _arguments '3:Subsubcommands:($(_knife_options2))'
89 esac
90 ;;
91 knifesubcmd3)
92 case $words[3] in
93 show)
94 case $words[2] in
95 cookbook)
96 versioncomp=1
97 _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
98 ;;
99 (node|client|role)
100 compadd "$@" --attribute
101 esac
102 esac
103 case $words[4] in
104 (show|edit)
105 _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
106 ;;
107 file)
108 _arguments '*:file or directory:_files -g "*.(rb|json)"'
109 ;;
110 list)
111 compadd -a "$@" knife_general_flags
112 ;;
113 *)
114 _arguments '*:Subsubcommands:($(_knife_options3))'
115 esac
116 ;;
117 knifesubcmd4)
118 if (( versioncomp > 0 )); then
119 compadd "$@" attributes definitions files libraries providers recipes resources templates
120 else
121 _arguments '*:Subsubcommands:($(_knife_options2))'
122 fi
123 ;;
124 knifesubcmd5)
125 _arguments '*:Subsubcommands:($(_knife_options3))'
126 esac
127 }
128
129 # Helper functions to provide the argument completion for several depths of commands
130 _knife_options1() {
131 ( for line in $( knife $words[2] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
132 }
133
134 _knife_options2() {
135 ( for line in $( knife $words[2] $words[3] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
136 }
137
138 _knife_options3() {
139 ( for line in $( knife $words[2] $words[3] $words[4] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
140 }
141
142 # The chef_x_remote functions use knife to get a list of objects of type x on the server
143 _chef_roles_remote() {
144 (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
145 }
146
147 _chef_clients_remote() {
148 (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
149 }
150
151 _chef_nodes_remote() {
152 (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
153 }
154
155 _chef_cookbooks_remote() {
156 (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
157 }
158
159 _chef_sitecookbooks_remote() {
160 (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
161 }
162
163 _chef_data_bags_remote() {
164 (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
165 }
166
167 _chef_environments_remote() {
168 (knife environment list | awk '{print $1}')
169 }
170
171 # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
172 _chef_cookbooks_local() {
173 (for i in $( grep cookbook_path $HOME/.chef/knife.rb | awk 'BEGIN {FS = "[" }; {print $2}' | sed 's/\,//g' | sed "s/'//g" | sed 's/\(.*\)]/\1/' ); do ls $i; done)
174 }
175
176 # This function extracts the available cookbook versions on the chef server
177 _cookbook_versions() {
178 (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
179 }
180
181 _knife "$@"