]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/machinectl
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / shell-completion / bash / machinectl
1 # machinectl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of systemd.
5 #
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
27 __get_machines() {
28 local a b
29 (machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; echo ".host") | \
30 { while read a b; do echo " $a"; done; } | sort -u;
31 }
32
33 _machinectl() {
34 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
35 local i verb comps
36
37 local -A OPTS=(
38 [STANDALONE]='--all -a -l --full --help -h --no-ask-password --no-legend --no-pager --version --value
39 --mkdir --read-only --force -q --quiet'
40 [ARG]='--host -H --kill-who -M --machine --property -p --signal -s --uid -E --setenv -n --lines
41 -o --output --verify --format --max-addresses'
42 )
43
44 local -A VERBS=(
45 [STANDALONE]='list list-images clean pull-tar pull-raw list-transfers cancel-transfer import-fs'
46 [MACHINES]='status show start stop login shell enable disable poweroff reboot terminate kill bind copy-to copy-from
47 image-status show-image clone rename read-only remove set-limit export-tar export-raw'
48 [FILE]='import-tar import-raw'
49 )
50
51 _init_completion || return
52
53 for ((i=0; i <= COMP_CWORD; i++)); do
54 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
55 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
56 verb=${COMP_WORDS[i]}
57 break
58 fi
59 done
60
61 if __contains_word "$prev" ${OPTS[ARG]}; then
62 case $prev in
63 --signal|-s)
64 _signals
65 return
66 ;;
67 --kill-who)
68 comps='all leader'
69 ;;
70 --host|-H)
71 comps=$(compgen -A hostname)
72 ;;
73 --machine|-M)
74 comps=$( __get_machines )
75 ;;
76 --property|-p)
77 comps=''
78 ;;
79 --output|-o)
80 comps=$( machinectl --output=help 2>/dev/null )
81 ;;
82 --verify)
83 comps=$( machinectl --verify=help 2>/dev/null )
84 ;;
85 --format)
86 comps='uncompressed xz gzip bzip2'
87 ;;
88 esac
89 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
90 return 0
91 fi
92
93 if [[ "$cur" = -* ]]; then
94 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
95 return 0
96 fi
97
98 if [[ -z $verb ]]; then
99 comps=${VERBS[*]}
100
101 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
102 comps=''
103
104 elif __contains_word "$verb" ${VERBS[MACHINES]}; then
105 comps=$( __get_machines )
106
107 elif __contains_word "$verb" ${VERBS[FILE]}; then
108 comps=$(compgen -f -- "cur")
109 compopt -o filenames
110 fi
111
112 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
113 return 0
114 }
115
116 complete -F _machinectl machinectl