]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/homectl
0a7bd0d13c88685de0c886de7fad7dbca54eab8f
[thirdparty/systemd.git] / shell-completion / bash / homectl
1 # homectl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 #
4 # This file is part of systemd.
5 #
6 # systemd is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation; either version 2.1 of the License, or
9 # (at your option) any later version.
10 #
11 # systemd is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with systemd; If not, see <https://www.gnu.org/licenses/>.
18
19 __contains_word () {
20 local w word=$1; shift
21 for w in "$@"; do
22 [[ $w = "$word" ]] && return
23 done
24 }
25
26 __get_machines() {
27 local a b
28 machinectl list --full --no-legend --no-pager 2>/dev/null |
29 { while read a b; do echo " $a"; done; };
30 }
31
32 __get_homes() {
33 homectl --no-pager --no-legend list 2>/dev/null
34 }
35
36 _homectl() {
37 local i verb comps
38 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
39
40 local -A OPTS=(
41 [STANDALONE]='-h --help --version
42 --no-pager --no-legend --no-ask-password
43 -j -E -P'
44 [ARG]=' -H --host
45 -M --machine
46 --identity
47 --json
48 --export-format
49 -c --real-name
50 --realm
51 --email-address
52 --location
53 --icon-name
54 -d --home-dir
55 --uid
56 -G --member-of
57 --skel
58 --shell
59 --setenv
60 --timezone
61 --language
62 --ssh-authorized-keys
63 --pkcs11-token-uri
64 --locked
65 --not-before
66 --not-after
67 --rate-limit-interval
68 --rate-limit-burst
69 --password-hint
70 --enforce-password-policy
71 --password-change-now
72 --password-change-min
73 --password-change-max
74 --password-change-warn
75 --password-change-inactive
76 --disk-size
77 --access-mode
78 --umask
79 --nice
80 --rlimit
81 --tasks-max
82 --memory-high
83 --memory-max
84 --cpu-weight
85 --io-weight
86 --storage
87 --image-path
88 --fs-type
89 --luks-discard
90 --luks-offline-discard
91 --luks-cipher
92 --luks-cipher-mode
93 --luks-volume-key-size
94 --luks-pbkdf-type
95 --luks-pbkdf-hash-algorithm
96 --luks-pbkdf-force-iterations
97 --luks-pbkdf-time-cost
98 --luks-pbkdf-memory-cost
99 --luks-pbkdf-parallel-threads
100 --luks-sector-size
101 --nosuid
102 --nodev
103 --noexec
104 --cifs-domain
105 --cifs-user-name
106 --cifs-service
107 --stop-delay
108 --kill-processes
109 --auto-login'
110 )
111
112 if __contains_word "$prev" ${OPTS[ARG]}; then
113 case $prev in
114 --host|-H)
115 comps=$(compgen -A hostname)
116 ;;
117 --machine|-M)
118 comps=$( __get_machines )
119 ;;
120 --identity|--image-path)
121 comps=$(compgen -A file -- "$cur" )
122 compopt -o filenames
123 ;;
124 --json)
125 comps='pretty short off'
126 ;;
127 --export-format)
128 comps='full stripped minimal'
129 ;;
130 --locked|--enforce-password-policy|--password-change-now|--luks-discard|--luks-offline-discard|--nosuid|--nodev|--noexec|--kill-processes|--auto-login)
131 comps='yes no'
132 ;;
133 -d|--home-dir|--skel)
134 comps=$(compgen -A directory -- "$cur" )
135 compopt -o dirnames
136 ;;
137 -G|--member-of)
138 comps=$(compgen -A group -- "$cur" )
139 ;;
140 --shell)
141 comps=$(cat /etc/shells)
142 ;;
143 --fs-type)
144 comps='btrfs ext4 xfs'
145 ;;
146 --cifs-user-name)
147 comps=$(compgen -A user -- "$cur" )
148 ;;
149 esac
150 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
151 return 0
152 fi
153
154 if [[ "$cur" = -* ]]; then
155 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
156 return 0
157 fi
158
159 local -A VERBS=(
160 [STANDALONE]='list lock-all'
161 [CREATE]='create'
162 [NAMES]='activate deactivate inspect authenticate remove lock unlock'
163 [NAME]='update passwd'
164 [RESIZE]='resize'
165 [WITH]='with'
166 )
167
168 for ((i=0; i < COMP_CWORD; i++)); do
169 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
170 verb=${COMP_WORDS[i]}
171 break
172 fi
173 done
174
175 if [[ -z ${verb-} ]]; then
176 comps=${VERBS[*]}
177 elif __contains_word "$verb" ${VERBS[NAME]}; then
178 comps=$(__get_homes)
179 elif __contains_word "$verb" ${VERBS[NAMES]}; then
180 comps=$(__get_homes)
181 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[CREATE]} ${VERBS[RESIZE]}; then
182 comps=$(__get_homes)
183 elif __contains_word "$verb" ${VERBS[WITH]}; then
184 comps=$(__get_homes)
185 fi
186
187 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
188 return 0
189 }
190
191 complete -F _homectl homectl