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