-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcf_complete.sh
141 lines (128 loc) · 4.25 KB
/
cf_complete.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# completion for cf cli.
# source this file from your .bashrc
# be sure to remove the old cf completion line as that one doesn't work
# scrape the cli version to handle different help options
FULLVERSION=$(cf --version|cut -d " " -f 3|cut -d + -f 1)
MAJORV=$(echo $FULLVERSION|cut -d . -f 1)
MINORV=$(echo $FULLVERSION|cut -d . -f 2)
PATCHV=$(echo $FULLVERSION|cut -d . -f 3)
# produce a math compatible version number.
VERSION=$(printf "%02d%02d%02d" $MAJORV $MINORV $PATCHV 2>/dev/null)
# set the help command depending on the cli version
# 000000 indicates that the version is non-numeric and is probably a
# self-built binary which we assume is recent
if [ "$VERSION" -ge "062200" -o "$VERSION" -eq "000000" ]
then
HELPOPT="-a "
else
HELPOPT=""
fi
# gather the list of commands currently supported by cf cli.
COMMANDS=$(cf help $HELPOPT | sed -n -e '/GETTING STARTED:/,/ENVIRONMENT VARIABLES:/p' | grep '^ ' | awk '{print $1}')
# peel out all of the carriage returns ungracefully
COMMANDS=$(echo $COMMANDS)
_cf_complete()
{
# Collect the current word and previous word.
local cur=${COMP_WORDS[COMP_CWORD]}
local prev1=${COMP_WORDS[COMP_CWORD-1]}
# base case. After this case, we can assume we have a word before
# the previous word. That's why this base case is broken out.
case "$prev1" in
cf|help)
COMPREPLY=( $(compgen -W "$COMMANDS" -- $cur) )
return 0
;;
esac
# first arg after "cf"
case "$prev1" in
# org related commands or options
-o|delete-org|rename-org|set-quota|space-users|org)
COMPREPLY=( $(compgen -W "$(_cf_org)" -- $cur) )
return 0
;;
# space related commands
set-space-quota)
COMPREPLY=( $(compgen -W "$(_cf_space)" -- $cur) )
return 0
;;
# app related commands and options
app|a|bind-service|bs|unbind-service|us|copy-source|create-app-manifest|delete|env|e|events|files|logs|rename|restage|rg|restart|rs|restart-app-instance|scale|set-env|se|start|st|stop|sp|unset-env|get-health-check|set-health-check|enable-ssh|disable-ssh|ssh-enabled|ssh|--recent|enable-diego|disable-diego|tasks|run-task|terminate-task)
COMPREPLY=( $(compgen -W "$(_cf_app)" -- $cur) )
return 0
;;
# service instance related commands
delete-service|ds|purge-service-instance|rename-service|service|update-service|service-key|service-keys)
COMPREPLY=( $(compgen -W "$(_cf_service_instance)" -- $cur) )
return 0
;;
# service commands
create-service|enable-service-access|disable-service-access)
COMPREPLY=( $(compgen -W "$(_cf_service)" -- $cur) )
return 0
;;
# quota commands
update-quota)
COMPREPLY=( $(compgen -W "$(_cf_org_quota)" -- $cur) )
return 0
;;
esac
# we can now collect the word before the previous word. Woohoo!
local prev2=${COMP_WORDS[COMP_CWORD-2]}
# second argument after "cf"
case "$prev2" in
bind-service)
COMPREPLY=( $(compgen -W "$(_cf_service_instance)" -- $cur) )
return 0
;;
unbind-service)
COMPREPLY=( $(compgen -W "$(_cf_bound_service $prev1)" -- $cur) )
return 0
;;
create-service)
COMPREPLY=( $(compgen -W "$(_cf_plan $prev1)" -- $cur) )
return 0
;;
set-org-role|set-space-role)
COMPREPLY=( $(compgen -W "$(_cf_org)" -- $cur) )
return 0
;;
set-quota)
COMPREPLY=( $(compgen -W "$(_cf_org_quota)" -- $cur) )
return 0
;;
esac
}
_cf_app()
{
cf apps | awk 'NR>4 {print $1}'
}
_cf_service()
{
cf marketplace | awk 'NR>4 {print $1}'|sed -n -e '1,/^$/p'
}
_cf_plan()
{
cf marketplace -s "$1" | awk 'NR>4 {print $1}'
}
_cf_service_instance()
{
cf services | awk 'NR>3 {print $1}'
}
_cf_bound_service()
{
cf services | awk 'NR>3' | grep $1 | awk '{print $1}'
}
_cf_org()
{
cf orgs | awk 'NR>3 {print $1}'
}
_cf_space()
{
cf spaces | awk 'NR>3 {print $1}'
}
_cf_org_quota()
{
cf quotas | awk 'NR>4 {print $1}'
}
complete -F _cf_complete cf