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