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