Debian Codegraph @

shell-completion/bash/userdbctl

raw
1 # shellcheck shell=bash
2 # userdbctl(1) completion -*- shell-script -*-
3 # SPDX-License-Identifier: LGPL-2.1-or-later
4 #
5 # This file is part of systemd.
6 #
7 # Copyright © 2010 Ran Benita
8 #
9 # systemd is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation; either version 2.1 of the License, or
12 # (at your option) any later version.
13 #
14 # systemd is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with systemd; If not, see <https://www.gnu.org/licenses/>.
21
22 __contains_word () {
23 local w word=$1; shift
24 for w in "$@"; do
25 [[ $w = "$word" ]] && return
26 done
27 }
28
29 __get_all_users () {
30 local a b
31 userdbctl user --no-legend -B 2>/dev/null | { while read -r a b; do printf "%s\n" "$a"; done; }
32 }
33
34 __get_all_groups () {
35 local a b
36 userdbctl group --no-legend -B 2>/dev/null | { while read -r a b; do printf "%s\n" "$a"; done; }
37 }
38
39 _userdbctl () {
40 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
41 local i verb comps
42
43 local -A OPTS=(
44 [STANDALONE]='-h --help --version --no-pager --no-legend
45 -j -N --chain -z --fuzzy -I -S -R -B'
46 [ARG]='--output -s --service --with-nss --synthesize --with-dropin --with-varlink
47 --multiplexer --json --uid-min --uid-max --disposition --boundaries --from-file'
48 )
49
50 if __contains_word "$prev" ${OPTS[ARG]}; then
51 case $prev in
52 --output)
53 comps=$( userdbctl --output=help 2>/dev/null )
54 ;;
55 -s|--service|--uid-min|--uid-max)
56 comps=''
57 ;;
58 --with-nss|--synthesize|--with-dropin|--with-varlink|--multiplexer|--boundaries)
59 comps='yes no'
60 ;;
61 --json)
62 comps=$( userdbctl --json=help 2>/dev/null )
63 ;;
64 --disposition)
65 comps='intrinsic system dynamic regular container reserved'
66 ;;
67 esac
68 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
69 return 0
70 fi
71
72 if [[ "$cur" = -* ]]; then
73 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
74 return 0
75 fi
76
77 local -A VERBS=(
78 [USERS]='ssh-authorized-keys user users-in-group'
79 [GROUPS]='group groups-of-user'
80 [STANDALONE]='services'
81 )
82
83 for ((i=0; i < COMP_CWORD; i++)); do
84 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
85 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
86 verb=${COMP_WORDS[i]}
87 break
88 fi
89 done
90
91 if [[ -z ${verb-} ]]; then
92 comps="${VERBS[*]}"
93 elif __contains_word "$verb" ${VERBS[USERS]}; then
94 comps=$( __get_all_users )
95 elif __contains_word "$verb" ${VERBS[GROUPS]}; then
96 comps=$( __get_all_groups )
97 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
98 comps=''
99 fi
100
101 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
102 return 0
103 }
104
105 complete -F _userdbctl userdbctl