]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/bootctl
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / shell-completion / bash / bootctl
CommitLineData
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#
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 <http://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
a73b2f4a
YW
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
95fe27d9
TA
31_bootctl() {
32 local i verb comps
33 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
34 local -A OPTS=(
8ed22786 35 [STANDALONE]='-h --help --no-variables -p --print-path --version --no-pager'
92360836 36 [ARG]='--path'
95fe27d9
TA
37 )
38
ed0c5a6f
YW
39 if __contains_word "$prev" ${OPTS[ARG]}; then
40 case $prev in
41 --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 esac
50 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
51 return 0
52 fi
53
95fe27d9
TA
54 if [[ "$cur" = -* ]]; then
55 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
56 return 0
57 fi
58
59 local -A VERBS=(
92360836 60 [STANDALONE]='help install list remove status update'
a73b2f4a 61 [BOOTENTRY]='set-default set-oneshot'
95fe27d9
TA
62 )
63
64 for ((i=0; i < COMP_CWORD; i++)); do
65 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
66 verb=${COMP_WORDS[i]}
67 break
68 fi
69 done
70
71 if [[ -z $verb ]]; then
72 comps=${VERBS[*]}
73 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
74 comps=''
a73b2f4a
YW
75 elif __contains_word "$verb" ${VERBS[BOOTENTRY]}; then
76 name=
77 for ((i++; i < COMP_CWORD; i++)); do
78 if ! __contains_word "${COMP_WORDS[i]}" ${OPTS[*]} ${VERBS[*]} &&
79 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
80 name=${COMP_WORDS[i]}
81 break;
82 fi
83 done
84
85 if [[ -z $name ]]; then
86 comps=$( __get_entry_ids )
87 else
88 comps=''
89 fi
95fe27d9
TA
90 fi
91
92 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
93 return 0
94}
95
96complete -F _bootctl bootctl