Chris Sosa | c1bd111 | 2011-08-26 15:22:10 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 2 | # 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 Hesling | 8c3e5f1 | 2019-10-31 10:34:08 -0700 | [diff] [blame] | 7 | # Declare one cache for all bash complete operations to |
| 8 | # allow it to be easiley cleared if need be. |
| 9 | # > unset _comp_cache |
| 10 | declare -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 Shih | cefecb6 | 2020-07-30 14:54:53 +0800 | [diff] [blame] | 20 | | egrep -o '\{[[:alnum:]_,-]+\}' | sort -u \ |
Craig Hesling | 8c3e5f1 | 2019-10-31 10:34:08 -0700 | [diff] [blame] | 21 | | tr -d '{}' | tr ',' ' ' |
| 22 | return ${PIPESTATUS[0]} |
| 23 | } |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 24 | |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 25 | # Echo a list of -- flags that the current command accepts. The |
| 26 | # function assumes that the command supports shflags' --help flag. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 27 | _flags() { |
Craig Hesling | e6bbc55 | 2019-11-13 09:46:46 -0800 | [diff] [blame] | 28 | command "$@" --help 2>&1 \ |
| 29 | | egrep -o -- '--(\[no\])?[[:alnum:]=_-]+' \ |
| 30 | | sed -E -e 's|--\[no\](.+)|--\1 --no\1|' |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 33 | # Complete flags, i.e., current words starting with --. Return 1 if |
| 34 | # the current word doesn't start with --, 0 otherwise. |
Craig Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 35 | _complete_flag_help() { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 36 | COMPREPLY=() |
| 37 | local cur="${COMP_WORDS[COMP_CWORD]}" |
| 38 | if [[ "${cur}" == --* ]]; then |
Craig Hesling | 3b06f21 | 2019-11-01 14:18:10 -0700 | [diff] [blame] | 39 | local key="flags/${COMP_WORDS[0]}" |
| 40 | if [[ -z "${_comp_cache[${key}]}" ]]; then |
Craig Hesling | e6bbc55 | 2019-11-13 09:46:46 -0800 | [diff] [blame] | 41 | _comp_cache[${key}]="$(_flags "${COMP_WORDS[0]}")" |
Craig Hesling | 3b06f21 | 2019-11-01 14:18:10 -0700 | [diff] [blame] | 42 | fi |
| 43 | COMPREPLY=( $(compgen -W "${_comp_cache[${key}]}" -- "${cur}") ) |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 44 | return 0 |
| 45 | fi |
| 46 | return 1 |
| 47 | } |
| 48 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 49 | # 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 51 | _argeq() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 52 | 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 80 | _board_sysroots() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 81 | local builddir=/build |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 82 | if [ -d ${builddir} ]; then |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 83 | echo $(command ls "${builddir}") |
| 84 | fi |
| 85 | } |
| 86 | |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 87 | _complete_board_sysroot_flag() { |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 88 | 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 Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 97 | # Completion for --board= argument for existing board sysroots |
Craig Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 98 | _complete_basic() { |
| 99 | _complete_flag_help && return 0 |
Eric Li | cad2b49 | 2010-04-01 13:07:10 -0700 | [diff] [blame] | 100 | _complete_board_sysroot_flag && return 0 |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 103 | # echo the existing target board overlays |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 104 | _board_overlays() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 105 | local overlaydir=../overlays |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 106 | if [ -d ${overlaydir} ]; then |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 107 | echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,) |
| 108 | fi |
| 109 | } |
| 110 | |
| 111 | # Completion for --board= argument for existing board overlays |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 112 | _board_overlay() { |
Craig Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 113 | _complete_flag_help && return 0 |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 114 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 115 | COMPREPLY=() |
| 116 | local arg=$(_argeq --board) |
| 117 | if [[ ${arg} == --board=* ]]; then |
| 118 | COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) ) |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 119 | fi |
| 120 | } |
| 121 | |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 122 | # Completion for -c and -s argument for autotest script |
| 123 | _ls_autotest() { |
| 124 | local autotest_dir=../third_party/autotest/files |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 125 | ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \ |
| 126 | | sed s/"..\/third_party\/autotest\/files\/"//g |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 129 | _autotest_complete() { |
Craig Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 130 | _complete_flag_help && return 0 |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 131 | |
| 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 Hesling | d89d598 | 2019-11-01 16:10:03 -0700 | [diff] [blame] | 147 | _test_that_complete() { |
Craig Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 148 | _complete_flag_help && return 0 |
Craig Hesling | d89d598 | 2019-11-01 16:10:03 -0700 | [diff] [blame] | 149 | return 0 |
| 150 | } |
| 151 | |
Darin Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 152 | # Complete cros_workon's <command> argument. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 153 | # |
| 154 | # TODO(petkov): We should probably extract the list of commands from |
Craig Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 155 | # cros_workon --help, just like we do for flags (see _complete_flag_help). |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 156 | # |
| 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 Baines | 93da99b | 2011-02-05 11:42:19 -0800 | [diff] [blame] | 160 | # "cros_workon --all list". |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 161 | _complete_cros_workon_command() { |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 162 | [ ${COMP_CWORD} -eq 1 ] || return 1 |
| 163 | local command="${COMP_WORDS[1]}" |
Craig Hesling | 1f5f5a3 | 2019-10-31 13:19:45 -0700 | [diff] [blame] | 164 | |
| 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 176 | return 0 |
| 177 | } |
| 178 | |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 179 | # 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 189 | # 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 Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 193 | local cros_workon=$(_cros_workon_executable) |
Craig Hesling | 33b6021 | 2019-11-01 14:16:49 -0700 | [diff] [blame] | 194 | ${cros_workon} $1 list | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,' |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | # Completes the current cros_workon argument assuming it's a |
| 198 | # package/ebuild name. |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 199 | _complete_cros_workon_package() { |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 200 | [ ${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 Hesling | 6db16b5 | 2019-11-01 14:17:43 -0700 | [diff] [blame] | 205 | 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 210 | 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 Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 220 | # Complete cros_workon arguments. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 221 | _cros_workon() { |
| 222 | COMPREPLY=() |
Craig Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 223 | _complete_flag_help && return 0 |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 224 | _complete_board_sysroot_flag && return 0 |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 225 | _complete_cros_workon_command && return 0 |
| 226 | _complete_cros_workon_package && return 0 |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 227 | return 0 |
| 228 | } |
| 229 | |
Craig Hesling | 8c3e5f1 | 2019-10-31 10:34:08 -0700 | [diff] [blame] | 230 | _complete_cros_command() { |
| 231 | local command="${COMP_WORDS[COMP_CWORD]}" |
| 232 | if [ ${COMP_CWORD} -ne 1 ]; then |
Pi-Hsun Shih | 9ba30ba | 2019-11-28 14:16:47 +0800 | [diff] [blame] | 233 | return 1 |
Craig Hesling | 8c3e5f1 | 2019-10-31 10:34:08 -0700 | [diff] [blame] | 234 | 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 Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 247 | _complete_flag_help && return 0 |
Craig Hesling | 8c3e5f1 | 2019-10-31 10:34:08 -0700 | [diff] [blame] | 248 | _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 Keshet | b08a89a | 2013-03-12 12:27:49 -0700 | [diff] [blame] | 254 | # 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 Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 271 | complete -o bashdefault -o default -F _complete_basic \ |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 272 | build_autotest.sh \ |
| 273 | build_image \ |
| 274 | build_packages \ |
Craig Hesling | 01d91d4 | 2019-11-13 09:50:12 -0800 | [diff] [blame] | 275 | mod_image_for_test.sh \ |
| 276 | cros_portage_upgrade |
| 277 | |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 278 | complete -o bashdefault -o default -F _board_overlay setup_board |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 279 | complete -o bashdefault -o default -o nospace -F _autotest_complete autotest |
Craig Hesling | d89d598 | 2019-11-01 16:10:03 -0700 | [diff] [blame] | 280 | complete -o bashdefault -o default -o nospace -F _test_that_complete test_that |
Pi-Hsun Shih | 9ba30ba | 2019-11-28 14:16:47 +0800 | [diff] [blame] | 281 | complete -o bashdefault -o default -F _cros cros |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 282 | complete -F _cros_workon cros_workon |
Aviv Keshet | 7af8fe3 | 2013-03-14 16:03:31 -0700 | [diff] [blame] | 283 | complete -o bashdefault -o default -F _complete_equery equery |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 284 | |
Aviv Keshet | 7e743ed | 2013-03-14 18:37:54 -0700 | [diff] [blame] | 285 | # Use equery completion for equery-$board for known boards |
| 286 | _boardlist=$(cros_list_overlays | egrep "^.*/overlays/overlay-.*$" | |
| 287 | sed -n "s/.*overlay-//p") |
| 288 | for board in $_boardlist; do |
| 289 | complete -o bashdefault -o default -F _complete_equery equery-$board |
| 290 | done |
| 291 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 292 | ### Local Variables: |
| 293 | ### mode: shell-script |
| 294 | ### End: |