]>
Commit | Line | Data |
---|---|---|
95fe27d9 | 1 | # bootctl(1) completion -*- shell-script -*- |
7059062c | 2 | # SPDX-License-Identifier: LGPL-2.1+ |
95fe27d9 TA |
3 | # |
4 | # This file is part of systemd. | |
5 | # | |
95fe27d9 TA |
6 | # systemd is free software; you can redistribute it and/or modify it |
7 | # under the terms of the GNU Lesser General Public License as published by | |
8 | # the Free Software Foundation; either version 2.1 of the License, or | |
9 | # (at your option) any later version. | |
10 | # | |
11 | # systemd is distributed in the hope that it will be useful, but | |
12 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | # General Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU Lesser General Public License | |
17 | # along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | __contains_word () { | |
843cfcb1 ZJS |
20 | local w word=$1; shift |
21 | for w in "$@"; do | |
22 | [[ $w = "$word" ]] && return | |
23 | done | |
95fe27d9 TA |
24 | } |
25 | ||
a73b2f4a | 26 | __get_entry_ids() { |
843cfcb1 | 27 | bootctl --no-pager list 2>/dev/null | { while read -r a b; do [[ $a == 'id:' ]] && echo " $b"; done } |
a73b2f4a YW |
28 | } |
29 | ||
95fe27d9 | 30 | _bootctl() { |
843cfcb1 ZJS |
31 | local i verb comps |
32 | local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} | |
33 | local -A OPTS=( | |
8f0a346a | 34 | [STANDALONE]='-h --help -p --print-esp-path -x --print-boot-path --version --no-variables --no-pager --graceful' |
2789437b | 35 | [ARG]='--esp-path --boot-path' |
843cfcb1 | 36 | ) |
95fe27d9 | 37 | |
843cfcb1 ZJS |
38 | if __contains_word "$prev" ${OPTS[ARG]}; then |
39 | case $prev in | |
2789437b | 40 | --esp-path|--boot-path) |
843cfcb1 ZJS |
41 | if [[ -z $cur ]]; then |
42 | comps=$(compgen -A directory -- "/" ) | |
43 | else | |
44 | comps=$(compgen -A directory -- "$cur" ) | |
45 | fi | |
46 | compopt -o filenames | |
47 | ;; | |
48 | esac | |
49 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
50 | return 0 | |
51 | fi | |
ed0c5a6f | 52 | |
843cfcb1 ZJS |
53 | if [[ "$cur" = -* ]]; then |
54 | COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) | |
55 | return 0 | |
56 | fi | |
95fe27d9 | 57 | |
843cfcb1 | 58 | local -A VERBS=( |
8f0a346a ZJS |
59 | # systemd-efi-options takes an argument, but it is free-form, so we cannot complete it |
60 | [STANDALONE]='help status install update remove is-installed random-seed systemd-efi-options list' | |
843cfcb1 | 61 | [BOOTENTRY]='set-default set-oneshot' |
8f0a346a | 62 | [BOOLEAN]='reboot-to-firmware' |
843cfcb1 | 63 | ) |
95fe27d9 | 64 | |
843cfcb1 ZJS |
65 | for ((i=0; i < COMP_CWORD; i++)); do |
66 | if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then | |
67 | verb=${COMP_WORDS[i]} | |
68 | break | |
69 | fi | |
70 | done | |
95fe27d9 | 71 | |
843cfcb1 ZJS |
72 | if [[ -z $verb ]]; then |
73 | comps=${VERBS[*]} | |
74 | elif __contains_word "$verb" ${VERBS[STANDALONE]}; then | |
75 | comps='' | |
76 | elif __contains_word "$verb" ${VERBS[BOOTENTRY]}; then | |
77 | name= | |
78 | for ((i++; i < COMP_CWORD; i++)); do | |
79 | if ! __contains_word "${COMP_WORDS[i]}" ${OPTS[*]} ${VERBS[*]} && | |
80 | ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then | |
81 | name=${COMP_WORDS[i]} | |
82 | break; | |
83 | fi | |
84 | done | |
a73b2f4a | 85 | |
843cfcb1 ZJS |
86 | if [[ -z $name ]]; then |
87 | comps=$( __get_entry_ids ) | |
88 | else | |
89 | comps='' | |
95fe27d9 | 90 | fi |
8f0a346a ZJS |
91 | elif __contains_word "$verb" ${VERBS[BOOLEAN]}; then |
92 | comps="yes no" | |
843cfcb1 | 93 | fi |
95fe27d9 | 94 | |
843cfcb1 ZJS |
95 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
96 | return 0 | |
95fe27d9 TA |
97 | } |
98 | ||
99 | complete -F _bootctl bootctl |