| 1 |
# shellcheck shell=bash |
| 2 |
# importctl(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 |
{ |
| 30 |
machinectl list-images --full --no-legend --no-pager 2>/dev/null |
| 31 |
machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null |
| 32 |
echo ".host" |
| 33 |
} | |
| 34 |
{ while read -r a b; do echo " $a"; done; } | |
| 35 |
sort -u |
| 36 |
} |
| 37 |
|
| 38 |
_importctl() { |
| 39 |
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword |
| 40 |
local i verb comps |
| 41 |
|
| 42 |
local -A OPTS=( |
| 43 |
[STANDALONE]='--force -h --help -j -N --no-ask-password --no-legend --no-pager -q --quiet --read-only |
| 44 |
--version -m -P -S -C' |
| 45 |
[ARG]='--host -H -M --machine --json --verify --format --class --keep-download' |
| 46 |
) |
| 47 |
|
| 48 |
local -A VERBS=( |
| 49 |
[STANDALONE]='pull-tar pull-raw list-transfers cancel-transfer list-images' |
| 50 |
[FILE]='import-tar import-raw' |
| 51 |
[FILE2]='export-tar export-raw' |
| 52 |
[DIR]='import-fs' |
| 53 |
) |
| 54 |
|
| 55 |
_init_completion || return |
| 56 |
|
| 57 |
for ((i=0; i <= COMP_CWORD; i++)); do |
| 58 |
if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && |
| 59 |
! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then |
| 60 |
verb=${COMP_WORDS[i]} |
| 61 |
break |
| 62 |
fi |
| 63 |
done |
| 64 |
|
| 65 |
if __contains_word "$prev" ${OPTS[ARG]}; then |
| 66 |
case $prev in |
| 67 |
--host|-H) |
| 68 |
comps=$(compgen -A hostname) |
| 69 |
;; |
| 70 |
--machine|-M) |
| 71 |
comps=$( __get_machines ) |
| 72 |
;; |
| 73 |
--json) |
| 74 |
comps='pretty short off' |
| 75 |
;; |
| 76 |
--verify) |
| 77 |
comps='no checksum signature' |
| 78 |
;; |
| 79 |
--format) |
| 80 |
comps='uncompressed xz gzip bzip2 zstd' |
| 81 |
;; |
| 82 |
--class) |
| 83 |
comps='machine portable sysext confext' |
| 84 |
;; |
| 85 |
--keep-download) |
| 86 |
comps='yes no' |
| 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 |
elif __contains_word "$verb" ${VERBS[STANDALONE]}; then |
| 101 |
comps='' |
| 102 |
elif __contains_word "$verb" ${VERBS[DIR]}; then |
| 103 |
if (( COMP_CWORD == i + 1 )); then # first argument after verb |
| 104 |
comps=$(compgen -d -- "$cur") |
| 105 |
compopt -o dirnames |
| 106 |
else |
| 107 |
comps='' |
| 108 |
fi |
| 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 |
elif __contains_word "$verb" ${VERBS[FILE2]}; then |
| 117 |
if (( COMP_CWORD == i + 2 )); then # second argument after verb |
| 118 |
comps=$(compgen -f -- "$cur") |
| 119 |
compopt -o filenames |
| 120 |
else |
| 121 |
comps='' |
| 122 |
fi |
| 123 |
fi |
| 124 |
|
| 125 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 126 |
return 0 |
| 127 |
} |
| 128 |
|
| 129 |
complete -F _importctl importctl |