diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2018-03-09 23:48:24 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2018-03-09 23:48:24 +0000 |
commit | 85e809aa266d8da80d5fd5ac3c7d283202b61405 (patch) | |
tree | 9c5fcad9816253e144d1c71d3545c339a5c76492 | |
parent | 7555c8dedf02a4dc946626f9dbe24de60c9ba441 (diff) | |
parent | c26c2aa159ae50e948344f358864edfd046142cb (diff) | |
download | sdk-85e809aa266d8da80d5fd5ac3c7d283202b61405.tar.gz |
Merge "bash_completion: Add completion for fastboot."
-rw-r--r-- | bash_completion/fastboot.bash | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/bash_completion/fastboot.bash b/bash_completion/fastboot.bash new file mode 100644 index 000000000..b91ec723e --- /dev/null +++ b/bash_completion/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 |