diff options
author | Elliott Hughes <enh@google.com> | 2018-04-03 13:42:18 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2018-04-04 08:33:33 -0700 |
commit | fbfa0e878174f971836dfe77e12ac9c82430c4bb (patch) | |
tree | b8728b11917f5429a5b351ef588f02f91559f59d /fastboot | |
parent | a10d40e2b2a6132b7e06397f62ac07a4d9bfb5e1 (diff) | |
download | system_core-fbfa0e878174f971836dfe77e12ac9c82430c4bb.tar.gz system_core-fbfa0e878174f971836dfe77e12ac9c82430c4bb.tar.bz2 system_core-fbfa0e878174f971836dfe77e12ac9c82430c4bb.zip |
Move adb/fastboot bash completion into adb/fastboot.
Bug: http://b/74443706
Test: bash completion still works.
Change-Id: I9da3a0ae748a202407652f513c990e9d05f8b01a
Diffstat (limited to 'fastboot')
-rw-r--r-- | fastboot/fastboot.bash | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/fastboot/fastboot.bash b/fastboot/fastboot.bash new file mode 100644 index 000000000..b91ec723e --- /dev/null +++ b/fastboot/fastboot.bash @@ -0,0 +1,182 @@ +# /* vim: set ai ts=4 ft=sh: */ +# +# Copyright 2017, The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +_fastboot() { + if ! type -t "$1" >/dev/null; then + return + fi + + if type -t _init_completion >/dev/null; then + _init_completion || return + fi + + local where i cur serial + COMPREPLY=() + + serial="${ANDROID_SERIAL:-none}" + where=OPTIONS + for ((i=1; i <= COMP_CWORD; i++)); do + cur="${COMP_WORDS[i]}" + case "${cur}" in + -s) + where=OPT_SERIAL + ;; + --slot) + where=OPT_SLOT + ;; + -*) + where=OPTIONS + ;; + *) + if [[ $where == OPT_SERIAL ]]; then + where=OPT_SERIAL_ARG + serial=${cur} + elif [[ $where == OPT_SLOT ]]; then + where=OPT_SLOT_ARG + else + where=COMMAND + break + fi + ;; + esac + done + + if [[ $where == COMMAND && $i -ge $COMP_CWORD ]]; then + where=OPTIONS + fi + + OPTIONS="-a -c --disable-verification --disable-verity -h --help -s --set_active --skip-secondary --skip-reboot --slot -u --version -w" + COMMAND="continue devices erase flash flashall flashing format getvar get_staged help oem reboot stage update" + + case $where in + OPTIONS|OPT_SERIAL) + COMPREPLY=( $(compgen -W "$OPTIONS $COMMAND" -- "$cur") ) + ;; + OPT_SERIAL_ARG) + local devices=$(command fastboot devices 2> /dev/null | awk '{ print $1 }') + COMPREPLY=( $(compgen -W "${devices}" -- ${cur}) ) + ;; + OPT_SLOT_ARG) + local slots="a all b other" + COMPREPLY=( $(compgen -W "${slots}" -- ${cur}) ) + ;; + COMMAND) + if [[ $i -eq $COMP_CWORD ]]; then + COMPREPLY=( $(compgen -W "$COMMAND" -- "$cur") ) + else + i=$((i+1)) + case "${cur}" in + flash) + _fastboot_cmd_flash "$serial" $i + ;; + reboot) + if [[ $COMP_CWORD == $i ]]; then + args="bootloader emergency" + COMPREPLY=( $(compgen -W "${args}" -- "${COMP_WORDS[i]}") ) + fi + ;; + update) + _fastboot_cmd_update "$serial" $i + ;; + esac + fi + ;; + esac + + return 0 +} + +_fastboot_cmd_flash() { + local serial i cur + local partitions + + serial=$1 + i=$2 + + cur="${COMP_WORDS[COMP_CWORD]}" + if [[ $i -eq $COMP_CWORD ]]; then + partitions="boot bootloader dtbo modem radio recovery system vbmeta vendor" + COMPREPLY=( $(compgen -W "$partitions" -- $cur) ) + else + _fastboot_util_complete_local_file "${cur}" '!*.img' + fi +} + +_fastboot_cmd_update() { + local serial i cur + + serial=$1 + i=$2 + + cur="${COMP_WORDS[COMP_CWORD]}" + + _fastboot_util_complete_local_file "${cur}" '!*.zip' +} + +_fastboot_util_complete_local_file() { + local file xspec i j IFS=$'\n' + local -a dirs files + + file=$1 + xspec=$2 + + # Since we're probably doing file completion here, don't add a space after. + if [[ $(type -t compopt) = "builtin" ]]; then + compopt -o plusdirs + if [[ "${xspec}" == "" ]]; then + COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") ) + else + compopt +o filenames + COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") ) + fi + else + # Work-around for shells with no compopt + + dirs=( $(compgen -d -- "${cur}" ) ) + + if [[ "${xspec}" == "" ]]; then + files=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") ) + else + files=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") ) + fi + + COMPREPLY=( $( + for i in "${files[@]}"; do + local skip= + for j in "${dirs[@]}"; do + if [[ $i == $j ]]; then + skip=1 + break + fi + done + [[ -n $skip ]] || printf "%s\n" "$i" + done + )) + + COMPREPLY=( ${COMPREPLY[@]:-} $( + for i in "${dirs[@]}"; do + printf "%s/\n" "$i" + done + )) + fi +} + +if [[ $(type -t compopt) = "builtin" ]]; then + complete -F _fastboot fastboot +else + complete -o nospace -F _fastboot fastboot +fi |