| 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 mode=$1; shift 1 |
| 29 |
local a b |
| 30 |
{ machinectl $mode list-images --full --no-legend --no-pager 2>/dev/null; machinectl $mode list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | |
| 31 |
{ while read -r a b; do echo " $a"; done; } | |
| 32 |
sort -u |
| 33 |
} |
| 34 |
|
| 35 |
_machinectl() { |
| 36 |
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword |
| 37 |
local i verb comps mode |
| 38 |
|
| 39 |
local -A OPTS=( |
| 40 |
[STANDALONE]='--all -a -l --full --help -h --no-ask-password --no-legend --no-pager --version --value |
| 41 |
--mkdir --read-only --force -q --quiet --system --user --now -V' |
| 42 |
[ARG]='--host -H --kill-whom -M --machine --property -p -P --signal -s --uid -E --setenv -n --lines |
| 43 |
-o --output --verify --format --max-addresses --runner' |
| 44 |
) |
| 45 |
|
| 46 |
local -A VERBS=( |
| 47 |
[STANDALONE]='list list-images clean pull-tar pull-raw list-transfers cancel-transfer import-fs' |
| 48 |
[MACHINES]='status show start stop login shell enable disable poweroff reboot pause resume terminate kill |
| 49 |
image-status show-image remove export-tar export-raw' |
| 50 |
[MACHINES_OR_FILES]='edit cat' |
| 51 |
[MACHINE_ONLY]='clone rename set-limit bind-volume unbind-volume' |
| 52 |
[READONLY]='read-only' |
| 53 |
[FILE]='import-tar import-raw' |
| 54 |
[MACHINES_AND_FILES]='copy-to copy-from bind' |
| 55 |
) |
| 56 |
|
| 57 |
_init_completion || return |
| 58 |
|
| 59 |
if __contains_word "--user" ${COMP_WORDS[*]}; then |
| 60 |
mode=--user |
| 61 |
else |
| 62 |
mode=--system |
| 63 |
fi |
| 64 |
|
| 65 |
for ((i=0; i <= COMP_CWORD; i++)); do |
| 66 |
if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && |
| 67 |
! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then |
| 68 |
verb=${COMP_WORDS[i]} |
| 69 |
break |
| 70 |
fi |
| 71 |
done |
| 72 |
|
| 73 |
if __contains_word "$prev" ${OPTS[ARG]}; then |
| 74 |
case $prev in |
| 75 |
--signal|-s) |
| 76 |
_signals |
| 77 |
return |
| 78 |
;; |
| 79 |
--kill-whom|--kill-who) |
| 80 |
comps='all leader' |
| 81 |
;; |
| 82 |
--host|-H) |
| 83 |
comps=$(compgen -A hostname) |
| 84 |
;; |
| 85 |
--machine|-M) |
| 86 |
comps=$( __get_machines $mode ) |
| 87 |
;; |
| 88 |
--property|-p|-P) |
| 89 |
comps='' |
| 90 |
;; |
| 91 |
--output|-o) |
| 92 |
comps=$( machinectl --output=help 2>/dev/null ) |
| 93 |
;; |
| 94 |
--verify) |
| 95 |
comps=$( machinectl --verify=help 2>/dev/null ) |
| 96 |
;; |
| 97 |
--format) |
| 98 |
comps='uncompressed xz gzip bzip2 zstd' |
| 99 |
;; |
| 100 |
--runner) |
| 101 |
comps='nspawn vmspawn' |
| 102 |
;; |
| 103 |
esac |
| 104 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 105 |
return 0 |
| 106 |
fi |
| 107 |
|
| 108 |
if [[ "$cur" = -* ]]; then |
| 109 |
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) |
| 110 |
return 0 |
| 111 |
fi |
| 112 |
|
| 113 |
if [[ -z ${verb-} ]]; then |
| 114 |
comps=${VERBS[*]} |
| 115 |
|
| 116 |
elif __contains_word "$verb" ${VERBS[STANDALONE]}; then |
| 117 |
comps='' |
| 118 |
|
| 119 |
elif __contains_word "$verb" ${VERBS[MACHINES]}; then |
| 120 |
comps=$( __get_machines $mode ) |
| 121 |
|
| 122 |
elif __contains_word "$verb" ${VERBS[MACHINES_OR_FILES]}; then |
| 123 |
comps=$( __get_machines $mode ) |
| 124 |
compopt -o filenames |
| 125 |
comps+=" $(compgen -f -- "$cur")" |
| 126 |
|
| 127 |
elif __contains_word "$verb" ${VERBS[MACHINE_ONLY]}; then |
| 128 |
local k args=0 |
| 129 |
for ((k=i+1; k < COMP_CWORD; k++)); do |
| 130 |
if __contains_word "${COMP_WORDS[k]}" ${OPTS[ARG]}; then |
| 131 |
((k++)) |
| 132 |
elif [[ ${COMP_WORDS[k]} = -* ]]; then |
| 133 |
continue |
| 134 |
else |
| 135 |
((args++)) |
| 136 |
fi |
| 137 |
done |
| 138 |
if (( args == 0 )); then |
| 139 |
comps=$( __get_machines $mode ) |
| 140 |
else |
| 141 |
comps='' |
| 142 |
fi |
| 143 |
|
| 144 |
elif __contains_word "$verb" ${VERBS[READONLY]}; then |
| 145 |
local k args=0 |
| 146 |
for ((k=i+1; k < COMP_CWORD; k++)); do |
| 147 |
if __contains_word "${COMP_WORDS[k]}" ${OPTS[ARG]}; then |
| 148 |
((k++)) |
| 149 |
elif [[ ${COMP_WORDS[k]} = -* ]]; then |
| 150 |
continue |
| 151 |
else |
| 152 |
((args++)) |
| 153 |
fi |
| 154 |
done |
| 155 |
if (( args == 0 )); then |
| 156 |
comps=$( __get_machines $mode ) |
| 157 |
elif (( args == 1 )); then |
| 158 |
comps='true false' |
| 159 |
else |
| 160 |
comps='' |
| 161 |
fi |
| 162 |
|
| 163 |
elif __contains_word "$verb" ${VERBS[MACHINES_AND_FILES]}; then |
| 164 |
local k args=0 |
| 165 |
for ((k=i+1; k < COMP_CWORD; k++)); do |
| 166 |
if __contains_word "${COMP_WORDS[k]}" ${OPTS[ARG]}; then |
| 167 |
((k++)) |
| 168 |
elif [[ ${COMP_WORDS[k]} = -* ]]; then |
| 169 |
continue |
| 170 |
else |
| 171 |
((args++)) |
| 172 |
fi |
| 173 |
done |
| 174 |
if (( args == 0 )); then |
| 175 |
comps=$( __get_machines $mode ) |
| 176 |
elif (( args <= 2 )); then |
| 177 |
comps=$(compgen -f -- "$cur") |
| 178 |
compopt -o filenames |
| 179 |
else |
| 180 |
comps='' |
| 181 |
fi |
| 182 |
|
| 183 |
elif __contains_word "$verb" ${VERBS[FILE]}; then |
| 184 |
if (( COMP_CWORD == i + 1 )); then # first argument after verb |
| 185 |
comps=$(compgen -f -- "$cur") |
| 186 |
compopt -o filenames |
| 187 |
else |
| 188 |
comps='' |
| 189 |
fi |
| 190 |
fi |
| 191 |
|
| 192 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 193 |
return 0 |
| 194 |
} |
| 195 |
|
| 196 |
complete -F _machinectl machinectl |