]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/machinectl
Merge pull request #14488 from yuwata/networkctl-show-logs
[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 # 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_machines() {
27 local a b
28 (machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; echo ".host") | \
29 { while read a b; do echo " $a"; done; } | sort -u;
30 }
31
32 _machinectl() {
33 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
34 local i verb comps
35
36 local -A OPTS=(
37 [STANDALONE]='--all -a -l --full --help -h --no-ask-password --no-legend --no-pager --version --value
38 --mkdir --read-only --force -q --quiet'
39 [ARG]='--host -H --kill-who -M --machine --property -p --signal -s --uid -E --setenv -n --lines
40 -o --output --verify --format --max-addresses'
41 )
42
43 local -A VERBS=(
44 [STANDALONE]='list list-images clean pull-tar pull-raw list-transfers cancel-transfer import-fs'
45 [MACHINES]='status show start stop login shell enable disable poweroff reboot terminate kill bind
46 copy-to copy-from image-status show-image clone rename read-only remove set-limit
47 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