blob: 63929bbacb69fbc0e5c248057f1ef1173471018a [file] [log] [blame]
Chris Sosac1bd1112011-08-26 15:22:10 -07001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Darin Petkov049dbab2010-02-23 18:16:37 -08002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Add programmable completion to some Chromium OS build scripts
6
Craig Hesling8c3e5f12019-10-31 10:34:08 -07007# Declare one cache for all bash complete operations to
8# allow it to be easiley cleared if need be.
9# > unset _comp_cache
10declare -A _comp_cache
11
12# Usage: cros --help | _subcmds_from_help
13# Parse subcommands from a commands's help message.
14# Trims the help outputs after it see any token with "command"
15# in it. Then, it searches for a no-space bracketed token,
16# similar to the following:
17# {subcmd1,subcmd2,subcmd3}
18_subcmds_from_help() {
19 sed -n -e '/commands:/,$p' \
Pi-Hsun Shihcefecb62020-07-30 14:54:53 +080020 | egrep -o '\{[[:alnum:]_,-]+\}' | sort -u \
Craig Hesling8c3e5f12019-10-31 10:34:08 -070021 | tr -d '{}' | tr ',' ' '
22 return ${PIPESTATUS[0]}
23}
Darin Petkov049dbab2010-02-23 18:16:37 -080024
Darin Petkovd6bf1012010-02-26 13:10:11 -080025# Echo a list of -- flags that the current command accepts. The
26# function assumes that the command supports shflags' --help flag.
Darin Petkov593e7962010-08-13 14:45:02 -070027_flags() {
Craig Heslinge6bbc552019-11-13 09:46:46 -080028 command "$@" --help 2>&1 \
29 | egrep -o -- '--(\[no\])?[[:alnum:]=_-]+' \
30 | sed -E -e 's|--\[no\](.+)|--\1 --no\1|'
Darin Petkovd6bf1012010-02-26 13:10:11 -080031}
32
Darin Petkovd6bf1012010-02-26 13:10:11 -080033# Complete flags, i.e., current words starting with --. Return 1 if
34# the current word doesn't start with --, 0 otherwise.
Craig Hesling01d91d42019-11-13 09:50:12 -080035_complete_flag_help() {
Darin Petkovd6bf1012010-02-26 13:10:11 -080036 COMPREPLY=()
37 local cur="${COMP_WORDS[COMP_CWORD]}"
38 if [[ "${cur}" == --* ]]; then
Craig Hesling3b06f212019-11-01 14:18:10 -070039 local key="flags/${COMP_WORDS[0]}"
40 if [[ -z "${_comp_cache[${key}]}" ]]; then
Craig Heslinge6bbc552019-11-13 09:46:46 -080041 _comp_cache[${key}]="$(_flags "${COMP_WORDS[0]}")"
Craig Hesling3b06f212019-11-01 14:18:10 -070042 fi
43 COMPREPLY=( $(compgen -W "${_comp_cache[${key}]}" -- "${cur}") )
Darin Petkovd6bf1012010-02-26 13:10:11 -080044 return 0
45 fi
46 return 1
47}
48
Darin Petkov049dbab2010-02-23 18:16:37 -080049# Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the
50# word to be completed. If found, echo "--arg=foo".
Darin Petkov593e7962010-08-13 14:45:02 -070051_argeq() {
Darin Petkov049dbab2010-02-23 18:16:37 -080052 local arg=$1
53 local w0="${COMP_WORDS[COMP_CWORD]}"
54 local w1="${COMP_WORDS[COMP_CWORD-1]}"
55
56 # Check for completing "--arg="
57 if [ "${w1}" == ${arg} -a "${w0}" == "=" ]; then
58 echo "${w1}${w0}"
59 return 0
60 fi
61
62 # Check for completing "--arg foo"
63 if [ "${w1}" == ${arg} ]; then
64 echo "${w1}=${w0}"
65 return 0
66 fi
67
68 # Check for completing "--arg=foo"
69 if [ ${COMP_CWORD} -gt 2 ]; then
70 local w2="${COMP_WORDS[COMP_CWORD-2]}"
71 if [ "${w2}" == ${arg} -a "${w1}" == "=" ]; then
72 echo "${w2}${w1}${w0}"
73 return 0
74 fi
75 fi
76}
77
78
79# echo the existing target board sysroots
Darin Petkov593e7962010-08-13 14:45:02 -070080_board_sysroots() {
Darin Petkov049dbab2010-02-23 18:16:37 -080081 local builddir=/build
Darin Petkovd6bf1012010-02-26 13:10:11 -080082 if [ -d ${builddir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -080083 echo $(command ls "${builddir}")
84 fi
85}
86
Darin Petkov593e7962010-08-13 14:45:02 -070087_complete_board_sysroot_flag() {
Eric Li3d13cd12010-03-31 11:47:22 -070088 COMPREPLY=()
89 local arg=$(_argeq --board)
90 if [[ ${arg} == --board=* ]]; then
91 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) )
92 return 0
93 fi
94 return 1
95}
96
Darin Petkov049dbab2010-02-23 18:16:37 -080097# Completion for --board= argument for existing board sysroots
Craig Hesling01d91d42019-11-13 09:50:12 -080098_complete_basic() {
99 _complete_flag_help && return 0
Eric Licad2b492010-04-01 13:07:10 -0700100 _complete_board_sysroot_flag && return 0
Darin Petkov049dbab2010-02-23 18:16:37 -0800101}
102
Darin Petkov049dbab2010-02-23 18:16:37 -0800103# echo the existing target board overlays
Darin Petkov593e7962010-08-13 14:45:02 -0700104_board_overlays() {
Darin Petkov049dbab2010-02-23 18:16:37 -0800105 local overlaydir=../overlays
Darin Petkovd6bf1012010-02-26 13:10:11 -0800106 if [ -d ${overlaydir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -0800107 echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,)
108 fi
109}
110
111# Completion for --board= argument for existing board overlays
Darin Petkov593e7962010-08-13 14:45:02 -0700112_board_overlay() {
Craig Hesling01d91d42019-11-13 09:50:12 -0800113 _complete_flag_help && return 0
Darin Petkovd6bf1012010-02-26 13:10:11 -0800114
Darin Petkov049dbab2010-02-23 18:16:37 -0800115 COMPREPLY=()
116 local arg=$(_argeq --board)
117 if [[ ${arg} == --board=* ]]; then
118 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) )
Darin Petkov049dbab2010-02-23 18:16:37 -0800119 fi
120}
121
Eric Li3d13cd12010-03-31 11:47:22 -0700122# Completion for -c and -s argument for autotest script
123_ls_autotest() {
124 local autotest_dir=../third_party/autotest/files
Darin Petkov593e7962010-08-13 14:45:02 -0700125 ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \
126 | sed s/"..\/third_party\/autotest\/files\/"//g
Eric Li3d13cd12010-03-31 11:47:22 -0700127}
128
Darin Petkov593e7962010-08-13 14:45:02 -0700129_autotest_complete() {
Craig Hesling01d91d42019-11-13 09:50:12 -0800130 _complete_flag_help && return 0
Eric Li3d13cd12010-03-31 11:47:22 -0700131
132 local arg=$(_argeq -c)
133 if [[ ${arg} == -c=* ]]; then
134 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-c=})"))
135 return 0
136 fi
137
138 arg=$(_argeq -s)
139 if [[ ${arg} == -s=* ]]; then
140 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-s=})"))
141 return 0
142 fi
143
144 _complete_board_sysroot_flag && return 0
145}
146
Craig Heslingd89d5982019-11-01 16:10:03 -0700147_test_that_complete() {
Craig Hesling01d91d42019-11-13 09:50:12 -0800148 _complete_flag_help && return 0
Craig Heslingd89d5982019-11-01 16:10:03 -0700149 return 0
150}
151
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700152# Complete cros_workon's <command> argument.
Darin Petkov593e7962010-08-13 14:45:02 -0700153#
154# TODO(petkov): We should probably extract the list of commands from
Craig Hesling01d91d42019-11-13 09:50:12 -0800155# cros_workon --help, just like we do for flags (see _complete_flag_help).
Darin Petkov593e7962010-08-13 14:45:02 -0700156#
157# TODO(petkov): Currently, this assumes that the command is the first
158# argument. In practice, the command is the first non-flag
159# argument. I.e., this should be fixed to support something like
Mandeep Singh Baines93da99b2011-02-05 11:42:19 -0800160# "cros_workon --all list".
Darin Petkov6944c5d2010-08-18 09:57:34 -0700161_complete_cros_workon_command() {
Darin Petkov593e7962010-08-13 14:45:02 -0700162 [ ${COMP_CWORD} -eq 1 ] || return 1
163 local command="${COMP_WORDS[1]}"
Craig Hesling1f5f5a32019-10-31 13:19:45 -0700164
165 # TODO(hesling): Local scoped references to associative arrays
166 # seems to be broken in bash version 4.3.48, but working in version 5.
167 # We can beautify this by using the following command, when cros bash
168 # is updated.
169 # local -n cache="_comp_cache[subcmds/cros_workon]"
170
171 local key="subcmds/cros_workon"
172 if [[ -z "${_comp_cache[${key}]}" ]]; then
173 _comp_cache[${key}]="$(command cros_workon --help | _subcmds_from_help)"
174 fi
175 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
Darin Petkov593e7962010-08-13 14:45:02 -0700176 return 0
177}
178
Darin Petkov6944c5d2010-08-18 09:57:34 -0700179# Prints the full path to the cros_workon executable, handling tilde
180# expansion for the current user.
181_cros_workon_executable() {
182 local cros_workon="${COMP_WORDS[0]}"
183 if [[ "$cros_workon" == '~/'* ]]; then
184 cros_workon="$HOME/${cros_workon#'~/'}"
185 fi
186 echo "$cros_workon"
187}
188
Darin Petkov593e7962010-08-13 14:45:02 -0700189# Lists the workon (or live, if --all is passed in) ebuilds. Lists
190# both the full names (e.g., chromeos-base/metrics) as well as just
191# the ebuild names (e.g., metrics).
192_cros_workon_list() {
Darin Petkov6944c5d2010-08-18 09:57:34 -0700193 local cros_workon=$(_cros_workon_executable)
Craig Hesling33b60212019-11-01 14:16:49 -0700194 ${cros_workon} $1 list | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
Darin Petkov593e7962010-08-13 14:45:02 -0700195}
196
197# Completes the current cros_workon argument assuming it's a
198# package/ebuild name.
Darin Petkov6944c5d2010-08-18 09:57:34 -0700199_complete_cros_workon_package() {
Darin Petkov593e7962010-08-13 14:45:02 -0700200 [ ${COMP_CWORD} -gt 1 ] || return 1
201 local package="${COMP_WORDS[COMP_CWORD]}"
202 local command="${COMP_WORDS[1]}"
203 # If "start", complete based on all workon packages.
204 if [[ ${command} == "start" ]]; then
Craig Hesling6db16b52019-11-01 14:17:43 -0700205 local key="pkgs/all"
206 if [[ -z "${_comp_cache[${key}]}" ]]; then
207 _comp_cache[${key}]="$(_cros_workon_list --all)"
208 fi
209 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${package}"))
Darin Petkov593e7962010-08-13 14:45:02 -0700210 return 0
211 fi
212 # If "stop" or "iterate", complete based on all live packages.
213 if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
214 COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
215 return 0
216 fi
217 return 1
218}
219
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700220# Complete cros_workon arguments.
Darin Petkov593e7962010-08-13 14:45:02 -0700221_cros_workon() {
222 COMPREPLY=()
Craig Hesling01d91d42019-11-13 09:50:12 -0800223 _complete_flag_help && return 0
Darin Petkov593e7962010-08-13 14:45:02 -0700224 _complete_board_sysroot_flag && return 0
Darin Petkov6944c5d2010-08-18 09:57:34 -0700225 _complete_cros_workon_command && return 0
226 _complete_cros_workon_package && return 0
Darin Petkov593e7962010-08-13 14:45:02 -0700227 return 0
228}
229
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700230_complete_cros_command() {
231 local command="${COMP_WORDS[COMP_CWORD]}"
232 if [ ${COMP_CWORD} -ne 1 ]; then
Pi-Hsun Shih9ba30ba2019-11-28 14:16:47 +0800233 return 1
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700234 fi
235
236 local key="subcmds/cros"
237 if [[ -z "${_comp_cache[${key}]}" ]]; then
238 _comp_cache[${key}]="$(command cros --help | _subcmds_from_help)"
239 fi
240 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
241 return 0
242}
243
244# Complete cros arguments.
245_cros() {
246 COMPREPLY=()
Craig Hesling01d91d42019-11-13 09:50:12 -0800247 _complete_flag_help && return 0
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700248 _complete_board_sysroot_flag && return 0
249 _complete_cros_command && return 0
250 # TODO(hesling): Add package completion like cros_workon.
251 return 0
252}
253
Aviv Keshetb08a89a2013-03-12 12:27:49 -0700254# Complete equery's <module-name> argument.
255_complete_equery_module_name() {
256 [ ${COMP_CWORD} -eq 1 ] || return 1
257 local command="${COMP_WORDS[1]}"
258 COMPREPLY=($(compgen -W "belongs changes check depends depgraph files has \
259 hasuse keywords list meta size uses which" \
260 -- "$command"))
261 return 0
262}
263
264# Complete equery arguments.
265_complete_equery() {
266 COMPREPLY=()
267 _complete_equery_module_name && return 0
268 return 0
269}
270
Craig Hesling01d91d42019-11-13 09:50:12 -0800271complete -o bashdefault -o default -F _complete_basic \
Darin Petkov593e7962010-08-13 14:45:02 -0700272 build_autotest.sh \
273 build_image \
274 build_packages \
Craig Hesling01d91d42019-11-13 09:50:12 -0800275 mod_image_for_test.sh \
276 cros_portage_upgrade
277
Darin Petkov593e7962010-08-13 14:45:02 -0700278complete -o bashdefault -o default -F _board_overlay setup_board
Eric Li3d13cd12010-03-31 11:47:22 -0700279complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
Craig Heslingd89d5982019-11-01 16:10:03 -0700280complete -o bashdefault -o default -o nospace -F _test_that_complete test_that
Pi-Hsun Shih9ba30ba2019-11-28 14:16:47 +0800281complete -o bashdefault -o default -F _cros cros
Darin Petkov6944c5d2010-08-18 09:57:34 -0700282complete -F _cros_workon cros_workon
Aviv Keshet7af8fe32013-03-14 16:03:31 -0700283complete -o bashdefault -o default -F _complete_equery equery
Darin Petkov049dbab2010-02-23 18:16:37 -0800284
Aviv Keshet7e743ed2013-03-14 18:37:54 -0700285# Use equery completion for equery-$board for known boards
286_boardlist=$(cros_list_overlays | egrep "^.*/overlays/overlay-.*$" |
287 sed -n "s/.*overlay-//p")
288for board in $_boardlist; do
289 complete -o bashdefault -o default -F _complete_equery equery-$board
290done
291
Darin Petkov049dbab2010-02-23 18:16:37 -0800292### Local Variables:
293### mode: shell-script
294### End: