Debian Codegraph @

shell-completion/bash/systemd-creds

raw
1 # shellcheck shell=bash
2 # systemd-creds(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_tpm2_devices() {
28 local a b c
29 systemd-creds --no-legend --quiet --tpm2-device=list 2>/dev/null |
30 { while read -r a b c; do echo " $a"; done; }
31 }
32
33 __get_creds() {
34 local a b c
35 local mode=$1; shift 1
36 systemd-creds list $mode --no-legend --no-pager $1 2>/dev/null |
37 { while read -r a b c; do echo " $a"; done; }
38 }
39
40 _systemd_creds() {
41 local verb comps mode i
42 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword
43 local -A OPTS=(
44 [STANDALONE]='-h --help --version --no-pager --no-legend --no-ask-password
45 -H -T --system --user -p --pretty --allow-null'
46 [ARG]='--tpm2-device
47 --tpm2-device-key
48 --tpm2-pcrs
49 --tpm2-public-key
50 --tpm2-public-key-pcrs
51 --tpm2-signature
52 --json
53 --transcode
54 --newline
55 --name
56 --timestamp
57 --not-after
58 --with-key
59 --uid'
60 )
61
62 local -A VERBS=(
63 [STANDALONE]='list setup'
64 [CAT]='cat'
65 [ENCRYPT]='encrypt'
66 [DECRYPT]='decrypt'
67 )
68
69 local -A OPTS_FOR_VERB=(
70 [CAT]='--help
71 --version
72 --no-pager
73 --no-legend
74 --user
75 --system'
76 [ENCRYPT]='--help
77 --version
78 --no-pager
79 --no-legend
80 --no-ask-password
81 --uid
82 --with-key
83 -H
84 -T
85 --tpm2-device
86 --tpm2-pcrs
87 --tpm2-public-key
88 --tpm2-public-key-pcrs
89 --tpm2-signature
90 --not-after
91 --name
92 --timestamp
93 --pretty'
94 [DECRYPT]='--help
95 --version
96 --no-pager
97 --no-legend
98 --no-ask-password
99 --uid
100 --tpm2-device
101 --tpm2-pcrs
102 --tpm2-public-key
103 --tpm2-public-key-pcrs
104 --tpm2-signature
105 --name
106 --timestamp
107 --allow-null'
108 )
109
110 _init_completion || return
111
112 if __contains_word "--user" ${COMP_WORDS[*]}; then
113 mode=--user
114 elif __contains_word "--system" ${COMP_WORDS[*]}; then
115 mode=--system
116 else
117 mode=''
118 fi
119
120 if __contains_word "$prev" ${OPTS[ARG]}; then
121 case $prev in
122 --tpm2-public-key|--tpm2-signature)
123 comps=$(compgen -A file -- "$cur")
124 compopt -o filenames
125 ;;
126 --tpm2-device)
127 comps="auto list $(__get_tpm2_devices)"
128 ;;
129 --json)
130 comps=$( systemd-creds --json=help 2>/dev/null )
131 ;;
132 --transcode)
133 comps=$( systemd-creds --no-legend --transcode=help 2>/dev/null )
134 ;;
135 --with-key)
136 comps=$( systemd-creds --no-legend --with-key=help 2>/dev/null )
137 ;;
138 esac
139 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
140 return 0
141 fi
142
143 if [[ -z ${verb-} && "$cur" = -* ]]; then
144 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
145 return 0
146 fi
147
148 for ((i=0; i < COMP_CWORD; i++)); do
149 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
150 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
151 verb=${COMP_WORDS[i]}
152 break
153 fi
154 done
155
156 if [[ -z ${verb-} ]]; then
157 comps=${VERBS[*]}
158 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
159 comps=''
160 elif __contains_word "$verb" ${VERBS[CAT]}; then
161 if [[ $cur = -* ]]; then
162 comps=${OPTS_FOR_VERB[CAT]}
163 else
164 comps=$( __get_creds $mode )
165 fi
166 elif __contains_word "$verb" ${VERBS[ENCRYPT]}; then
167 if [[ $cur = -* ]]; then
168 comps=${OPTS_FOR_VERB[ENCRYPT]}
169 else
170 comps=$( compgen -A file -- "$cur" )
171 compopt -o filenames
172 fi
173 elif __contains_word "$verb" ${VERBS[DECRYPT]}; then
174 if [[ $cur = -* ]]; then
175 comps=${OPTS_FOR_VERB[DECRYPT]}
176 else
177 comps=$( compgen -A file -- "$cur" )
178 compopt -o filenames
179 fi
180 fi
181
182 COMPREPLY=( $(compgen -W '${comps-}' -- "$cur") )
183 return 0
184 }
185
186 complete -F _systemd_creds systemd-creds