]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/systemd-analyze
update TODO
[thirdparty/systemd.git] / shell-completion / bash / systemd-analyze
1 # shellcheck shell=bash
2 # systemd-analyze(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_machines() {
30 local a b
31 { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \
32 { while read a b; do echo " $a"; done; } | \
33 sort -u
34 }
35
36 __get_units_all() {
37 systemctl list-units --no-legend --no-pager --plain --all $1 | \
38 { while read -r a b c; do echo " $a"; done }
39 }
40
41 __get_services() {
42 systemctl list-units --no-legend --no-pager --plain -t service --all $1 | \
43 { while read -r a b c; do [[ $b == "loaded" ]]; echo " $a"; done }
44 }
45
46 __get_syscall_sets() {
47 local line
48 systemd-analyze syscall-filter --no-pager | while IFS= read -r line; do
49 if [[ $line == @* ]]; then
50 printf '%s\n' "$line"
51 fi
52 done
53 }
54
55 __get_architectures() {
56 systemd-analyze --no-legend --no-pager architectures | { while read -r a b; do echo " $a"; done }
57 }
58
59 _systemd_analyze() {
60 local i verb comps mode
61 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword
62
63 local -A OPTS=(
64 [STANDALONE]='-h --help --version --system --user --global --order --require --no-pager
65 --man=no --generators=yes -q --quiet'
66 [ARG]='-H --host -M --machine --fuzz --from-pattern --to-pattern --root'
67 )
68
69 local -A VERBS=(
70 [STANDALONE]='time blame unit-files unit-paths exit-status capability compare-versions calendar timestamp timespan pcrs srk'
71 [CRITICAL_CHAIN]='critical-chain'
72 [DOT]='dot'
73 [DUMP]='dump'
74 [VERIFY]='verify'
75 [SECCOMP_FILTER]='syscall-filter'
76 [CAT_CONFIG]='cat-config'
77 [SECURITY]='security'
78 [CONDITION]='condition'
79 [INSPECT_ELF]='inspect-elf'
80 [PLOT]='plot'
81 [ARCHITECTURES]='architectures'
82 [FDSTORE]='fdstore'
83 )
84
85 local CONFIGS='locale.conf systemd/bootchart.conf systemd/coredump.conf systemd/journald.conf
86 systemd/journal-remote.conf systemd/journal-upload.conf systemd/logind.conf
87 systemd/resolved.conf systemd/networkd.conf systemd/pstore.conf systemd/resolved.conf
88 systemd/sleep.conf systemd/system.conf systemd/timedated.conf
89 systemd/timesyncd.conf systemd/user.conf udev/udev.conf'
90
91 _init_completion || return
92
93 for ((i=0; i < COMP_CWORD; i++)); do
94 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
95 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
96 verb=${COMP_WORDS[i]}
97 break
98 fi
99 done
100
101 if __contains_word "--user" ${COMP_WORDS[*]}; then
102 mode=--user
103 else
104 mode=--system
105 fi
106
107 if __contains_word "$prev" ${OPTS[ARG]}; then
108 case $prev in
109 --host|-H)
110 comps=$(compgen -A hostname)
111 ;;
112 --machine|-M)
113 comps=$( __get_machines )
114 ;;
115 esac
116 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
117 return 0
118 fi
119
120 if [[ -z ${verb-} && $cur = -* ]]; then
121 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
122 return 0
123 fi
124
125 if [[ -z ${verb-} ]]; then
126 comps=${VERBS[*]}
127
128 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
129 if [[ $cur = -* ]]; then
130 comps='--help --version --system --user --global --no-pager'
131 fi
132
133 elif __contains_word "$verb" ${VERBS[CRITICAL_CHAIN]}; then
134 if [[ $cur = -* ]]; then
135 comps='--help --version --system --user --fuzz --no-pager'
136 else
137 comps=$( __get_units_all $mode )
138 fi
139
140 elif __contains_word "$verb" ${VERBS[DOT]}; then
141 if [[ $cur = -* ]]; then
142 comps='--help --version --system --user --global --from-pattern --to-pattern --order --require'
143 fi
144
145 elif __contains_word "$verb" ${VERBS[DUMP]}; then
146 if [[ $cur = -* ]]; then
147 comps='--help --version --system --user --no-pager'
148 else
149 comps=$( __get_units_all $mode )
150 fi
151
152 elif __contains_word "$verb" ${VERBS[SECCOMP_FILTER]}; then
153 if [[ $cur = -* ]]; then
154 comps='--help --version --no-pager'
155 else
156 comps=$( __get_syscall_sets )
157 fi
158
159 elif __contains_word "$verb" ${VERBS[VERIFY]}; then
160 if [[ $cur = -* ]]; then
161 comps='--help --version --system --user --global --man=no --generators=yes --root --image --recursive-errors=no --recursive-errors=yes --recursive-errors=one'
162 else
163 comps=$( compgen -A file -- "$cur";
164 __get_units_all $mode )
165 compopt -o filenames
166 fi
167
168 elif __contains_word "$verb" ${VERBS[CAT_CONFIG]}; then
169 if [[ $cur = -* ]]; then
170 comps='--help --version --root --no-pager'
171 elif [[ -z $cur ]]; then
172 comps="$CONFIGS"
173 compopt -o filenames
174 else
175 comps="$CONFIGS $( compgen -A file -- "$cur" )"
176 compopt -o filenames
177 fi
178
179 elif __contains_word "$verb" ${VERBS[SECURITY]}; then
180 if [[ $cur = -* ]]; then
181 comps='--help --version --no-pager --system --user -H --host -M --machine --offline --threshold --security-policy --json=off --json=pretty --json=short --root --image --profile=default --profile=nonetwork --profile=strict --profile=trusted'
182 elif ! __contains_word "--offline" ${COMP_WORDS[*]}; then
183 comps=$( __get_services $mode )
184 else
185 comps="$CONFIGS $( compgen -A file -- "$cur" )"
186 compopt -o filenames
187 fi
188
189 elif __contains_word "$verb" ${VERBS[CONDITION]}; then
190 if [[ $cur = -* ]]; then
191 comps='--help --version --system --user --global --no-pager --root --image'
192 elif [[ $prev = "-u" ]] || [[ $prev = "--unit" ]]; then
193 comps=$( __get_services $mode )
194 fi
195
196 elif __contains_word "$verb" ${VERBS[INSPECT_ELF]}; then
197 if [[ $cur = -* ]]; then
198 comps='--help --version --json=off --json=pretty --json=short'
199 else
200 comps=$( compgen -A file -- "$cur" )
201 compopt -o filenames
202 fi
203
204 elif __contains_word "$verb" ${VERBS[PLOT]}; then
205 if [[ $cur = -* ]]; then
206 comps='--help --version --system --user --global --no-pager --json=off --json=pretty --json=short --table --no-legend'
207 fi
208
209 elif __contains_word "$verb" ${VERBS[ARCHITECTURES]}; then
210 if [[ $cur = -* ]]; then
211 comps='--help --version --no-pager --json=off --json=pretty --json=short --no-legend'
212 else
213 comps=$( __get_architectures )
214 fi
215
216 elif __contains_word "$verb" ${VERBS[FDSTORE]}; then
217 if [[ $cur = -* ]]; then
218 comps='--help --version --system --user --global -H --host -M --machine --no-pager --json=off --json=pretty --json=short --root --image'
219 else
220 comps=$( __get_services $mode )
221 fi
222 fi
223
224 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
225 return 0
226 }
227
228 complete -F _systemd_analyze systemd-analyze