]>
Commit | Line | Data |
---|---|---|
f8457290 | 1 | # shellcheck shell=bash |
83cb95b5 | 2 | # systemd-analyze(1) completion -*- shell-script -*- |
db9ecf05 | 3 | # SPDX-License-Identifier: LGPL-2.1-or-later |
83cb95b5 HH |
4 | # |
5 | # This file is part of systemd. | |
6 | # | |
96b2fb93 | 7 | # Copyright © 2010 Ran Benita |
83cb95b5 HH |
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 | |
85fce6f4 | 20 | # along with systemd; If not, see <https://www.gnu.org/licenses/>. |
83cb95b5 HH |
21 | |
22 | __contains_word () { | |
843cfcb1 ZJS |
23 | local w word=$1; shift |
24 | for w in "$@"; do | |
25 | [[ $w = "$word" ]] && return | |
26 | done | |
83cb95b5 HH |
27 | } |
28 | ||
64ae7f18 | 29 | __get_machines() { |
843cfcb1 | 30 | local a b |
4e918305 ZJS |
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 | |
64ae7f18 TA |
34 | } |
35 | ||
b4bb96f3 | 36 | __get_units_all() { |
283f3bd5 | 37 | systemctl list-units --no-legend --no-pager --plain --all $1 | \ |
b4bb96f3 NK |
38 | { while read -r a b c; do echo " $a"; done } |
39 | } | |
40 | ||
83da42c3 | 41 | __get_services() { |
1cabd2d0 | 42 | systemctl list-units --no-legend --no-pager --plain -t service --all $1 | \ |
843cfcb1 | 43 | { while read -r a b c; do [[ $b == "loaded" ]]; echo " $a"; done } |
83da42c3 YW |
44 | } |
45 | ||
2431ca22 | 46 | __get_syscall_sets() { |
843cfcb1 ZJS |
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 | |
2431ca22 LW |
53 | } |
54 | ||
cfb912ab LB |
55 | __get_architectures() { |
56 | systemd-analyze --no-legend --no-pager architectures | { while read -r a b; do echo " $a"; done } | |
57 | } | |
58 | ||
83cb95b5 | 59 | _systemd_analyze() { |
843cfcb1 | 60 | local i verb comps mode |
f34173a0 | 61 | local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword |
64ae7f18 | 62 | |
843cfcb1 ZJS |
63 | local -A OPTS=( |
64 | [STANDALONE]='-h --help --version --system --user --global --order --require --no-pager | |
7c0e0bbb | 65 | --man=no --generators=yes -q --quiet' |
843cfcb1 ZJS |
66 | [ARG]='-H --host -M --machine --fuzz --from-pattern --to-pattern --root' |
67 | ) | |
68 | ||
69 | local -A VERBS=( | |
f21a6502 | 70 | [STANDALONE]='time blame unit-paths exit-status calendar timestamp timespan' |
843cfcb1 ZJS |
71 | [CRITICAL_CHAIN]='critical-chain' |
72 | [DOT]='dot' | |
d1d8786c | 73 | [DUMP]='dump' |
843cfcb1 ZJS |
74 | [VERIFY]='verify' |
75 | [SECCOMP_FILTER]='syscall-filter' | |
843cfcb1 ZJS |
76 | [CAT_CONFIG]='cat-config' |
77 | [SECURITY]='security' | |
8de7929d | 78 | [CONDITION]='condition' |
917e6554 | 79 | [INSPECT_ELF]='inspect-elf' |
f21a6502 | 80 | [PLOT]='plot' |
cfb912ab | 81 | [ARCHITECTURES]='architectures' |
843cfcb1 ZJS |
82 | ) |
83 | ||
84 | local CONFIGS='systemd/bootchart.conf systemd/coredump.conf systemd/journald.conf | |
581ab537 YW |
85 | systemd/journal-remote.conf systemd/journal-upload.conf systemd/logind.conf |
86 | systemd/resolved.conf systemd/networkd.conf systemd/resolved.conf | |
87 | systemd/sleep.conf systemd/system.conf systemd/timedated.conf | |
88 | systemd/timesyncd.conf systemd/user.conf udev/udev.conf' | |
89 | ||
843cfcb1 ZJS |
90 | _init_completion || return |
91 | ||
92 | for ((i=0; i < COMP_CWORD; i++)); do | |
93 | if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && | |
94 | ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then | |
95 | verb=${COMP_WORDS[i]} | |
96 | break | |
64ae7f18 | 97 | fi |
843cfcb1 ZJS |
98 | done |
99 | ||
283f3bd5 FS |
100 | if __contains_word "--user" ${COMP_WORDS[*]}; then |
101 | mode=--user | |
102 | else | |
103 | mode=--system | |
104 | fi | |
105 | ||
843cfcb1 ZJS |
106 | if __contains_word "$prev" ${OPTS[ARG]}; then |
107 | case $prev in | |
108 | --host|-H) | |
109 | comps=$(compgen -A hostname) | |
110 | ;; | |
111 | --machine|-M) | |
112 | comps=$( __get_machines ) | |
113 | ;; | |
114 | esac | |
115 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
116 | return 0 | |
117 | fi | |
118 | ||
36ec0268 | 119 | if [[ -z ${verb-} && $cur = -* ]]; then |
843cfcb1 ZJS |
120 | COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) |
121 | return 0 | |
122 | fi | |
123 | ||
36ec0268 | 124 | if [[ -z ${verb-} ]]; then |
843cfcb1 | 125 | comps=${VERBS[*]} |
64ae7f18 | 126 | |
843cfcb1 ZJS |
127 | elif __contains_word "$verb" ${VERBS[STANDALONE]}; then |
128 | if [[ $cur = -* ]]; then | |
129 | comps='--help --version --system --user --global --no-pager' | |
83cb95b5 HH |
130 | fi |
131 | ||
843cfcb1 ZJS |
132 | elif __contains_word "$verb" ${VERBS[CRITICAL_CHAIN]}; then |
133 | if [[ $cur = -* ]]; then | |
134 | comps='--help --version --system --user --fuzz --no-pager' | |
b4bb96f3 | 135 | else |
283f3bd5 | 136 | comps=$( __get_units_all $mode ) |
83cb95b5 HH |
137 | fi |
138 | ||
843cfcb1 ZJS |
139 | elif __contains_word "$verb" ${VERBS[DOT]}; then |
140 | if [[ $cur = -* ]]; then | |
141 | comps='--help --version --system --user --global --from-pattern --to-pattern --order --require' | |
142 | fi | |
143 | ||
d1d8786c FB |
144 | elif __contains_word "$verb" ${VERBS[DUMP]}; then |
145 | if [[ $cur = -* ]]; then | |
146 | comps='--help --version --system --user --no-pager' | |
147 | else | |
283f3bd5 | 148 | comps=$( __get_units_all $mode ) |
d1d8786c FB |
149 | fi |
150 | ||
843cfcb1 ZJS |
151 | elif __contains_word "$verb" ${VERBS[SECCOMP_FILTER]}; then |
152 | if [[ $cur = -* ]]; then | |
153 | comps='--help --version --no-pager' | |
154 | else | |
155 | comps=$( __get_syscall_sets ) | |
156 | fi | |
157 | ||
158 | elif __contains_word "$verb" ${VERBS[VERIFY]}; then | |
159 | if [[ $cur = -* ]]; then | |
3cc3dc77 | 160 | comps='--help --version --system --user --global --man=no --generators=yes --root --image --recursive-errors=no --recursive-errors=yes --recursive-errors=one' |
843cfcb1 | 161 | else |
283f3bd5 FS |
162 | comps=$( compgen -A file -- "$cur"; |
163 | __get_units_all $mode ) | |
843cfcb1 ZJS |
164 | compopt -o filenames |
165 | fi | |
166 | ||
843cfcb1 ZJS |
167 | elif __contains_word "$verb" ${VERBS[CAT_CONFIG]}; then |
168 | if [[ $cur = -* ]]; then | |
169 | comps='--help --version --root --no-pager' | |
170 | elif [[ -z $cur ]]; then | |
171 | comps="$CONFIGS" | |
172 | compopt -o filenames | |
173 | else | |
174 | comps="$CONFIGS $( compgen -A file -- "$cur" )" | |
175 | compopt -o filenames | |
176 | fi | |
177 | ||
178 | elif __contains_word "$verb" ${VERBS[SECURITY]}; then | |
179 | if [[ $cur = -* ]]; then | |
04469211 | 180 | 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' |
83de7427 | 181 | elif ! __contains_word "--offline" ${COMP_WORDS[*]}; then |
843cfcb1 | 182 | comps=$( __get_services $mode ) |
83de7427 LB |
183 | else |
184 | comps="$CONFIGS $( compgen -A file -- "$cur" )" | |
185 | compopt -o filenames | |
843cfcb1 | 186 | fi |
8de7929d DDM |
187 | |
188 | elif __contains_word "$verb" ${VERBS[CONDITION]}; then | |
189 | if [[ $cur = -* ]]; then | |
190 | comps='--help --version --system --user --global --no-pager --root --image' | |
191 | elif [[ $prev = "-u" ]] || [[ $prev = "--unit" ]]; then | |
8de7929d DDM |
192 | comps=$( __get_services $mode ) |
193 | fi | |
917e6554 LB |
194 | |
195 | elif __contains_word "$verb" ${VERBS[INSPECT_ELF]}; then | |
196 | if [[ $cur = -* ]]; then | |
197 | comps='--help --version --json=off --json=pretty --json=short' | |
198 | else | |
199 | comps=$( compgen -A file -- "$cur" ) | |
200 | compopt -o filenames | |
201 | fi | |
f21a6502 | 202 | |
203 | elif __contains_word "$verb" ${VERBS[PLOT]}; then | |
204 | if [[ $cur = -* ]]; then | |
205 | comps='--help --version --system --user --global --no-pager --json=off --json=pretty --json=short --table --no-legend' | |
206 | fi | |
cfb912ab LB |
207 | |
208 | elif __contains_word "$verb" ${VERBS[ARCHITECTURES]}; then | |
209 | if [[ $cur = -* ]]; then | |
210 | comps='--help --version --no-pager --json=off --json=pretty --json=short --no-legend' | |
211 | else | |
212 | comps=$( __get_architectures ) | |
213 | fi | |
843cfcb1 ZJS |
214 | fi |
215 | ||
216 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
217 | return 0 | |
83cb95b5 HH |
218 | } |
219 | ||
220 | complete -F _systemd_analyze systemd-analyze |