-
Notifications
You must be signed in to change notification settings - Fork 1
/
knife.sh
executable file
·87 lines (77 loc) · 2.99 KB
/
knife.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# chef bash_completion functions
# Not hard-coded to any particular version of knife, they instead query knife for available options
# Author: Dmitriy Vi <vindimy@gmail.com>
# knife function definitions
_knife ()
{
local c=1 word
local numwords=0 cmd="knife"
while [ $c -le $COMP_CWORD ]; do
word="${COMP_WORDS[c]}"
case "$word" in
--*)
;;
*)
cmd=$cmd" "$word
numwords=$((++numwords))
;;
esac
c=$((++c))
done
local opts subcmd
word="${COMP_WORDS[COMP_CWORD]}"
case "$word" in
--*)
if [ $COMP_CWORD -ge 2 ]; then
subcmd=${COMP_WORDS[1]}
fi
opts=$(_knife_opts "$subcmd")
if [ "$opts" == "" ]; then
opts="--help"
fi
;;
*)
opts=$(_knife_cmds "$cmd" $((++numwords)))
case "$opts" in
BAG)
opts=$(knife data bag list)
;;
CLIENT)
opts=$(knife client list)
;;
COOKBOOK)
opts=$(knife cookbook list | awk '{ print $1 }')
;;
ENVIRONMENT)
opts=$(knife environment list)
;;
FILE)
opts=$(ls)
;;
NODE)
opts=$(knife node list)
;;
ROLE)
opts=$(knife role list)
;;
SERVER_NAME)
opts=$(knife cs server list | sed '1d' | awk '{ print $1 }')
;;
"(options)")
opts=""
;;
esac
;;
esac
COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$word"))
}
_knife_cmds ()
{
knife --help | grep ^knife | grep "$1" | awk -v col=$2 '{ print $col }' | sort | uniq
}
_knife_opts ()
{
knife $1 --help | grep -oh -e "--\w* " -e "--\w*-\w*" | sort | uniq
}
# complete statements
complete -o default -F _knife knife