Browse code

Merge pull request #452 from franklouwers/master

chef "knife" plugin

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