]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/bootctl
NEWS: fix typo
[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 --random-seed'
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 --random-seed)
60 comps="yes no"
61 ;;
62 esac
63 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
64 return 0
65 fi
66
67 if [[ "$cur" = -* ]]; then
68 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
69 return 0
70 fi
71
72 local -A VERBS=(
73 # systemd-efi-options takes an argument, but it is free-form, so we cannot complete it
74 [STANDALONE]='help status install update remove is-installed random-seed systemd-efi-options list set-timeout set-timeout-oneshot cleanup'
75 [BOOTENTRY]='set-default set-oneshot unlink'
76 [BOOLEAN]='reboot-to-firmware'
77 [FILE]='kernel-identify kernel-inspect'
78 )
79
80 for ((i=0; i < COMP_CWORD; i++)); do
81 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
82 verb=${COMP_WORDS[i]}
83 break
84 fi
85 done
86
87 if [[ -z ${verb-} ]]; then
88 comps=${VERBS[*]}
89 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
90 comps=''
91 elif __contains_word "$verb" ${VERBS[BOOTENTRY]}; then
92 name=
93 for ((i++; i < COMP_CWORD; i++)); do
94 if ! __contains_word "${COMP_WORDS[i]}" ${OPTS[*]} ${VERBS[*]} &&
95 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
96 name=${COMP_WORDS[i]}
97 break
98 fi
99 done
100
101 if [[ -z $name ]]; then
102 comps=$( __get_entry_ids )
103 else
104 comps=''
105 fi
106 elif __contains_word "$verb" ${VERBS[BOOLEAN]}; then
107 comps="yes no"
108 elif __contains_word "$verb" ${VERBS[FILE]}; then
109 comps=$( compgen -A file -- "$cur" )
110 compopt -o filenames
111 fi
112
113 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
114 return 0
115 }
116
117 complete -F _bootctl bootctl