]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/systemd-analyze
mkosi: Add zsh to Arch packages
[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 <http://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 --no-legend --no-pager | { while read a b; do echo " $a"; done; };
31 }
32
33 __get_services() {
34 systemctl list-units --no-legend --no-pager --plain -t service --all $1 | \
35 { while read -r a b c; do [[ $b == "loaded" ]]; echo " $a"; done }
36 }
37
38 __get_syscall_sets() {
39 local line
40 systemd-analyze syscall-filter --no-pager | while IFS= read -r line; do
41 if [[ $line == @* ]]; then
42 printf '%s\n' "$line"
43 fi
44 done
45 }
46
47 _systemd_analyze() {
48 local i verb comps mode
49 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword
50
51 local -A OPTS=(
52 [STANDALONE]='-h --help --version --system --user --global --order --require --no-pager
53 --man=no --generators=yes'
54 [ARG]='-H --host -M --machine --fuzz --from-pattern --to-pattern --root'
55 )
56
57 local -A VERBS=(
58 [STANDALONE]='time blame plot dump unit-paths exit-status calendar timestamp timespan'
59 [CRITICAL_CHAIN]='critical-chain'
60 [DOT]='dot'
61 [VERIFY]='verify'
62 [SECCOMP_FILTER]='syscall-filter'
63 [CAT_CONFIG]='cat-config'
64 [SECURITY]='security'
65 [CONDITION]='condition'
66 )
67
68 local CONFIGS='systemd/bootchart.conf systemd/coredump.conf systemd/journald.conf
69 systemd/journal-remote.conf systemd/journal-upload.conf systemd/logind.conf
70 systemd/resolved.conf systemd/networkd.conf systemd/resolved.conf
71 systemd/sleep.conf systemd/system.conf systemd/timedated.conf
72 systemd/timesyncd.conf systemd/user.conf udev/udev.conf'
73
74 _init_completion || return
75
76 for ((i=0; i < COMP_CWORD; i++)); do
77 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
78 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
79 verb=${COMP_WORDS[i]}
80 break
81 fi
82 done
83
84 if __contains_word "$prev" ${OPTS[ARG]}; then
85 case $prev in
86 --host|-H)
87 comps=$(compgen -A hostname)
88 ;;
89 --machine|-M)
90 comps=$( __get_machines )
91 ;;
92 esac
93 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
94 return 0
95 fi
96
97 if [[ -z ${verb-} && $cur = -* ]]; then
98 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
99 return 0
100 fi
101
102 if [[ -z ${verb-} ]]; then
103 comps=${VERBS[*]}
104
105 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
106 if [[ $cur = -* ]]; then
107 comps='--help --version --system --user --global --no-pager'
108 fi
109
110 elif __contains_word "$verb" ${VERBS[CRITICAL_CHAIN]}; then
111 if [[ $cur = -* ]]; then
112 comps='--help --version --system --user --fuzz --no-pager'
113 fi
114
115 elif __contains_word "$verb" ${VERBS[DOT]}; then
116 if [[ $cur = -* ]]; then
117 comps='--help --version --system --user --global --from-pattern --to-pattern --order --require'
118 fi
119
120 elif __contains_word "$verb" ${VERBS[SECCOMP_FILTER]}; then
121 if [[ $cur = -* ]]; then
122 comps='--help --version --no-pager'
123 else
124 comps=$( __get_syscall_sets )
125 fi
126
127 elif __contains_word "$verb" ${VERBS[VERIFY]}; then
128 if [[ $cur = -* ]]; then
129 comps='--help --version --system --user --global --man=no --generators=yes --root --image --recursive-errors=no --recursive-errors=yes --recursive-errors=one'
130 else
131 comps=$( compgen -A file -- "$cur" )
132 compopt -o filenames
133 fi
134
135 elif __contains_word "$verb" ${VERBS[CAT_CONFIG]}; then
136 if [[ $cur = -* ]]; then
137 comps='--help --version --root --no-pager'
138 elif [[ -z $cur ]]; then
139 comps="$CONFIGS"
140 compopt -o filenames
141 else
142 comps="$CONFIGS $( compgen -A file -- "$cur" )"
143 compopt -o filenames
144 fi
145
146 elif __contains_word "$verb" ${VERBS[SECURITY]}; then
147 if [[ $cur = -* ]]; then
148 comps='--help --version --no-pager --system --user -H --host -M --machine --offline --threshold --security-policy'
149 else
150 if __contains_word "--user" ${COMP_WORDS[*]}; then
151 mode=--user
152 else
153 mode=--system
154 fi
155 comps=$( __get_services $mode )
156 fi
157
158 elif __contains_word "$verb" ${VERBS[CONDITION]}; then
159 if [[ $cur = -* ]]; then
160 comps='--help --version --system --user --global --no-pager --root --image'
161 elif [[ $prev = "-u" ]] || [[ $prev = "--unit" ]]; then
162 if __contains_word "--user" ${COMP_WORDS[*]}; then
163 mode=--user
164 else
165 mode=--system
166 fi
167 comps=$( __get_services $mode )
168 fi
169 fi
170
171 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
172 return 0
173 }
174
175 complete -F _systemd_analyze systemd-analyze