]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/portablectl
Merge pull request #31511 from jamacku/prepare-for-diff-shellcheck
[thirdparty/systemd.git] / shell-completion / bash / portablectl
CommitLineData
f8457290 1# shellcheck shell=bash
37e21077 2# portablectl(1) completion -*- shell-script -*-
db9ecf05 3# SPDX-License-Identifier: LGPL-2.1-or-later
37e21077
YW
4#
5# This file is part of systemd.
6#
37e21077
YW
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/>.
37e21077
YW
19
20__contains_word () {
843cfcb1
ZJS
21 local w word=$1; shift
22 for w in "$@"; do
23 [[ $w = "$word" ]] && return
24 done
37e21077
YW
25}
26
27__get_machines() {
843cfcb1 28 local a b
4e918305
ZJS
29 { 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
37e21077
YW
32}
33
34_portablectl() {
843cfcb1
ZJS
35 local i n comps verb
36 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
37 local -A OPTS=(
38 [STANDALONE]='-q --quiet --runtime --no-reload --cat --no-pager --no-legend
e2c1ddcc 39 --no-ask-password --enable --now -h --help --version'
fa84c1ce 40 [ARG]='-p --profile --copy -H --host -M --machine --extension'
843cfcb1 41 )
37e21077 42
843cfcb1
ZJS
43 local -A VERBS=(
44 [STANDALONE]='list'
e26fe5f9 45 [IMAGE]='attach detach reattach inspect is-attached set-limit'
843cfcb1
ZJS
46 [IMAGES]='remove'
47 [IMAGE_WITH_BOOL]='read-only'
48 )
37e21077 49
843cfcb1
ZJS
50 if __contains_word "$prev" ${OPTS[ARG]}; then
51 case $prev in
52 --profile|-p)
53 comps="default nonetwork strict trusted"
54 ;;
55 --copy)
56 comps="copy symlink auto"
57 ;;
58 --host|-H)
59 comps=$(compgen -A hostname)
60 ;;
61 --machine|-M)
62 comps=$( __get_machines )
63 ;;
fa84c1ce
LB
64 --extension)
65 comps=$( compgen -A file -- "$cur" )
66 compopt -o filenames
67 ;;
843cfcb1
ZJS
68 esac
69 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
70 return 0
71 fi
37e21077 72
843cfcb1
ZJS
73 if [[ "$cur" = -* ]]; then
74 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
75 return 0
76 fi
37e21077 77
843cfcb1
ZJS
78 for ((i=0; i < COMP_CWORD; i++)); do
79 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
80 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
81 verb=${COMP_WORDS[i]}
82 break
83 fi
84 done
37e21077 85
e873a9f1 86 n=$((COMP_CWORD - i))
37e21077 87
36ec0268 88 if [[ -z ${verb-} ]]; then
843cfcb1
ZJS
89 comps=${VERBS[*]}
90 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
91 comps=''
92 elif __contains_word "$verb" ${VERBS[IMAGE]}; then
93 if [[ $n == 1 ]]; then
94 comps=$( compgen -A file -- "$cur" )
95 compopt -o filenames
96 else
97 comps=''
37e21077 98 fi
843cfcb1
ZJS
99 elif __contains_word "$verb" ${VERBS[IMAGES]}; then
100 comps=$( compgen -A file -- "$cur" )
101 compopt -o filenames
102 elif __contains_word "$verb" ${VERBS[IMAGE_WITH_BOOL]}; then
103 if [[ $n == 1 ]]; then
104 comps=$( compgen -A file -- "$cur" )
105 compopt -o filenames
106 elif [[ $n == 2 ]]; then
107 comps='yes no'
108 else
109 comps=''
110 fi
111 fi
37e21077 112
843cfcb1
ZJS
113 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
114 return 0
37e21077
YW
115}
116
117complete -F _portablectl portablectl