]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - shell-completion/bash/machinectl
Kill several SysV compat functionalities (v258) (#38178)
[thirdparty/systemd.git] / shell-completion / bash / machinectl
... / ...
CommitLineData
1# shellcheck shell=bash
2# machinectl(1) completion -*- shell-script -*-
3# SPDX-License-Identifier: LGPL-2.1-or-later
4#
5# This file is part of systemd.
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 <https://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 --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
32}
33
34_machinectl() {
35 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword
36 local i verb comps
37
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'
41 [ARG]='--host -H --kill-whom -M --machine --property -p --signal -s --uid -E --setenv -n --lines
42 -o --output --verify --format --max-addresses'
43 )
44
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 )
52
53 _init_completion || return
54
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
60 fi
61 done
62
63 if __contains_word "$prev" ${OPTS[ARG]}; then
64 case $prev in
65 --signal|-s)
66 _signals
67 return
68 ;;
69 --kill-whom|--kill-who)
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 zstd'
89 ;;
90 esac
91 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
92 return 0
93 fi
94
95 if [[ "$cur" = -* ]]; then
96 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
97 return 0
98 fi
99
100 if [[ -z ${verb-} ]]; then
101 comps=${VERBS[*]}
102
103 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
104 comps=''
105
106 elif __contains_word "$verb" ${VERBS[MACHINES]}; then
107 comps=$( __get_machines )
108
109 elif __contains_word "$verb" ${VERBS[FILE]}; then
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
116 fi
117
118 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
119 return 0
120}
121
122complete -F _machinectl machinectl