chiark / gitweb /
afcaadf7d809404bbe6a3e2a911f9e7116b84c4b
[elogind.git] / shell-completion / bash / portablectl
1 # portablectl(1) completion                             -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of elogind.
5 #
6 # Copyright © 2018 Yu Watanabe
7 #
8 # elogind is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # elogind is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with elogind; If not, see <http://www.gnu.org/licenses/>.
20
21 __contains_word () {
22         local w word=$1; shift
23         for w in "$@"; do
24                 [[ $w = "$word" ]] && return
25         done
26 }
27
28 __get_machines() {
29         local a b
30         machinectl list --no-legend --no-pager 2>/dev/null |
31                 { while read a b; do echo " $a"; done; };
32 }
33
34 _portablectl() {
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
39                               --no-ask-password -h --help --version'
40                 [ARG]='-p --profile --copy -H --host -M --machine'
41         )
42
43         local -A VERBS=(
44                 [STANDALONE]='list'
45                 [IMAGE]='attach detach inspect is-attached set-limit'
46                 [IMAGES]='remove'
47                 [IMAGE_WITH_BOOL]='read-only'
48         )
49
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                                 ;;
64                 esac
65                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
66                 return 0
67         fi
68
69         if [[ "$cur" = -* ]]; then
70                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
71                 return 0
72         fi
73
74         for ((i=0; i < COMP_CWORD; i++)); do
75                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
76                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
77                         verb=${COMP_WORDS[i]}
78                         break
79                 fi
80         done
81
82         n=$(($COMP_CWORD - $i))
83
84         if [[ -z $verb ]]; then
85                 comps=${VERBS[*]}
86         elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
87                 comps=''
88         elif __contains_word "$verb" ${VERBS[IMAGE]}; then
89                 if [[ $n == 1 ]]; then
90                         comps=$( compgen -A file -- "$cur" )
91                         compopt -o filenames
92                 else
93                         comps=''
94                 fi
95         elif __contains_word "$verb" ${VERBS[IMAGES]}; then
96                 comps=$( compgen -A file -- "$cur" )
97                 compopt -o filenames
98         elif __contains_word "$verb" ${VERBS[IMAGE_WITH_BOOL]}; then
99                 if [[ $n == 1 ]]; then
100                         comps=$( compgen -A file -- "$cur" )
101                         compopt -o filenames
102                 elif [[ $n == 2 ]]; then
103                         comps='yes no'
104                 else
105                         comps=''
106                 fi
107         fi
108
109         COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
110         return 0
111 }
112
113 complete -F _portablectl portablectl