]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/bootctl
Merge pull request #14488 from yuwata/networkctl-show-logs
[thirdparty/systemd.git] / shell-completion / bash / bootctl
1 # bootctl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of systemd.
5 #
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 () {
20 local w word=$1; shift
21 for w in "$@"; do
22 [[ $w = "$word" ]] && return
23 done
24 }
25
26 __get_entry_ids() {
27 bootctl --no-pager list 2>/dev/null | { while read -r a b; do [[ $a == 'id:' ]] && echo " $b"; done }
28 }
29
30 _bootctl() {
31 local i verb comps
32 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
33 local -A OPTS=(
34 [STANDALONE]='-h --help --no-variables -p --print-esp-path -x --print-boot-path --version --no-pager'
35 [ARG]='--esp-path --boot-path'
36 )
37
38 if __contains_word "$prev" ${OPTS[ARG]}; then
39 case $prev in
40 --esp-path|--boot-path)
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
52
53 if [[ "$cur" = -* ]]; then
54 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
55 return 0
56 fi
57
58 local -A VERBS=(
59 [STANDALONE]='help install list remove status update'
60 [BOOTENTRY]='set-default set-oneshot'
61 )
62
63 for ((i=0; i < COMP_CWORD; i++)); do
64 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
65 verb=${COMP_WORDS[i]}
66 break
67 fi
68 done
69
70 if [[ -z $verb ]]; then
71 comps=${VERBS[*]}
72 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
73 comps=''
74 elif __contains_word "$verb" ${VERBS[BOOTENTRY]}; then
75 name=
76 for ((i++; i < COMP_CWORD; i++)); do
77 if ! __contains_word "${COMP_WORDS[i]}" ${OPTS[*]} ${VERBS[*]} &&
78 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
79 name=${COMP_WORDS[i]}
80 break;
81 fi
82 done
83
84 if [[ -z $name ]]; then
85 comps=$( __get_entry_ids )
86 else
87 comps=''
88 fi
89 fi
90
91 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
92 return 0
93 }
94
95 complete -F _bootctl bootctl