forked from coreos/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_completion
242 lines (210 loc) · 6.48 KB
/
bash_completion
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Add programmable completion to some Chromium OS build scripts
# Echo a list of -- flags that the current command accepts. The
# function assumes that the command supports shflags' --help flag.
_flags() {
echo $(command "${COMP_WORDS[0]}" --help 2>&1 \
| egrep -o -- --\[^\ \]+: \
| sed 's/://; s/--\[no\]\(.\+\)/--\1 --no\1/')
}
# Complete flags, i.e., current words starting with --. Return 1 if
# the current word doesn't start with --, 0 otherwise.
_flag_complete() {
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
if [[ "${cur}" == --* ]]; then
COMPREPLY=( $(compgen -W "$(_flags)" -- ${cur}) )
return 0
fi
return 1
}
# Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the
# word to be completed. If found, echo "--arg=foo".
_argeq() {
local arg=$1
local w0="${COMP_WORDS[COMP_CWORD]}"
local w1="${COMP_WORDS[COMP_CWORD-1]}"
# Check for completing "--arg="
if [ "${w1}" == ${arg} -a "${w0}" == "=" ]; then
echo "${w1}${w0}"
return 0
fi
# Check for completing "--arg foo"
if [ "${w1}" == ${arg} ]; then
echo "${w1}=${w0}"
return 0
fi
# Check for completing "--arg=foo"
if [ ${COMP_CWORD} -gt 2 ]; then
local w2="${COMP_WORDS[COMP_CWORD-2]}"
if [ "${w2}" == ${arg} -a "${w1}" == "=" ]; then
echo "${w2}${w1}${w0}"
return 0
fi
fi
}
# echo the existing target board sysroots
_board_sysroots() {
local builddir=/build
if [ -d ${builddir} ]; then
echo $(command ls "${builddir}")
fi
}
_complete_board_sysroot_flag() {
COMPREPLY=()
local arg=$(_argeq --board)
if [[ ${arg} == --board=* ]]; then
COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) )
return 0
fi
return 1
}
# Completion for --board= argument for existing board sysroots
_board_sysroot() {
_flag_complete && return 0
_complete_board_sysroot_flag && return 0
}
# Completion for -c and -s argument for autotest script
_ls_autotest() {
local autotest_dir=../third_party/autotest/files
ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \
| sed s/"..\/third_party\/autotest\/files\/"//g
}
_autotest_complete() {
_flag_complete && return 0
local arg=$(_argeq -c)
if [[ ${arg} == -c=* ]]; then
COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-c=})"))
return 0
fi
arg=$(_argeq -s)
if [[ ${arg} == -s=* ]]; then
COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-s=})"))
return 0
fi
_complete_board_sysroot_flag && return 0
}
# Complete cros_workon's <command> argument.
#
# TODO(petkov): We should probably extract the list of commands from
# cros_workon --help, just like we do for flags (see _flag_complete).
#
# TODO(petkov): Currently, this assumes that the command is the first
# argument. In practice, the command is the first non-flag
# argument. I.e., this should be fixed to support something like
# "cros_workon --all list".
_complete_cros_workon_command() {
[ ${COMP_CWORD} -eq 1 ] || return 1
local command="${COMP_WORDS[1]}"
COMPREPLY=($(compgen -W "start stop list iterate" -- "$command"))
return 0
}
# Prints the full path to the cros_workon executable, handling tilde
# expansion for the current user.
_cros_workon_executable() {
local cros_workon="${COMP_WORDS[0]}"
if [[ "$cros_workon" == '~/'* ]]; then
cros_workon="$HOME/${cros_workon#'~/'}"
fi
echo "$cros_workon"
}
# Lists the workon (or live, if --all is passed in) ebuilds. Lists
# both the full names (e.g., chromeos-base/metrics) as well as just
# the ebuild names (e.g., metrics).
_cros_workon_list() {
local cros_workon=$(_cros_workon_executable)
${cros_workon} list $1 | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
}
# Completes the current cros_workon argument assuming it's a
# package/ebuild name.
_complete_cros_workon_package() {
[ ${COMP_CWORD} -gt 1 ] || return 1
local package="${COMP_WORDS[COMP_CWORD]}"
local command="${COMP_WORDS[1]}"
# If "start", complete based on all workon packages.
if [[ ${command} == "start" ]]; then
COMPREPLY=($(compgen -W "$(_cros_workon_list --all)" -- "$package"))
return 0
fi
# If "stop" or "iterate", complete based on all live packages.
if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
return 0
fi
return 1
}
# Complete cros_workon arguments.
_cros_workon() {
COMPREPLY=()
_flag_complete && return 0
_complete_board_sysroot_flag && return 0
_complete_cros_workon_command && return 0
_complete_cros_workon_package && return 0
return 0
}
_list_repo_commands() {
local repo=${COMP_WORDS[0]}
"$repo" help --all | grep -E '^ ' | sed 's/ \([^ ]\+\) .\+/\1/'
}
_list_repo_branches() {
local repo=${COMP_WORDS[0]}
"$repo" branches 2>&1 | grep \| | sed 's/[ *][Pp ] *\([^ ]\+\) .*/\1/'
}
_list_repo_projects() {
local repo=${COMP_WORDS[0]}
"$repo" manifest -o /dev/stdout 2> /dev/null \
| grep 'project name=' \
| sed 's/.\+name="\([^"]\+\)".\+/\1/'
}
# Complete repo's <command> argument.
_complete_repo_command() {
[ ${COMP_CWORD} -eq 1 ] || return 1
local command=${COMP_WORDS[1]}
COMPREPLY=($(compgen -W "$(_list_repo_commands)" -- "$command"))
return 0
}
_complete_repo_arg() {
[ ${COMP_CWORD} -gt 1 ] || return 1
local command=${COMP_WORDS[1]}
local current=${COMP_WORDS[COMP_CWORD]}
if [[ ${command} == "abandon" ]]; then
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=($(compgen -W "$(_list_repo_branches)" -- "$current"))
else
COMPREPLY=($(compgen -W "$(_list_repo_projects)" -- "$current"))
fi
return 0
fi
if [[ ${command} == "help" ]]; then
[ ${COMP_CWORD} -eq 2 ] && \
COMPREPLY=($(compgen -W "$(_list_repo_commands)" -- "$current"))
return 0
fi
if [[ ${command} == "start" ]]; then
[ ${COMP_CWORD} -gt 2 ] && \
COMPREPLY=($(compgen -W "$(_list_repo_projects)" -- "$current"))
return 0
fi
return 1
}
# Complete repo arguments.
_complete_repo() {
COMPREPLY=()
_complete_repo_command && return 0
_complete_repo_arg && return 0
return 0
}
complete -o bashdefault -o default -F _board_sysroot \
build_autotest.sh \
build_image \
build_packages \
image_to_usb.sh \
mod_image_for_test.sh
complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
complete -F _cros_workon cros_workon
complete -F _complete_repo repo
### Local Variables:
### mode: shell-script
### End: