]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/journalctl
Merge pull request #5112 from yuwata/fix-5105
[thirdparty/systemd.git] / shell-completion / bash / journalctl
1 # journalctl(1) completion -*- shell-script -*-
2 #
3 # This file is part of systemd.
4 #
5 # Copyright 2010 Ran Benita
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 <http://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_machines() {
28 local a b
29 (machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; echo ".host") | \
30 { while read a b; do echo " $a"; done; } | sort -u;
31 }
32
33 __syslog_priorities=(emerg alert crit err warning notice info debug)
34
35 _journalctl() {
36 local field_vals= cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
37 local -A OPTS=(
38 [STANDALONE]='-a --all --full --system --user
39 --disk-usage -f --follow --header
40 -h --help -l --local --new-id128 -m --merge --no-pager
41 --no-tail -q --quiet --setup-keys --this-boot --verify
42 --version --list-catalog --update-catalog --list-boots
43 --show-cursor --dmesg -k --pager-end -e -r --reverse
44 --utc -x --catalog --no-full --force --dump-catalog
45 --flush --rotate --sync --no-hostname'
46 [ARG]='-b --boot --this-boot -D --directory --file -F --field
47 -M --machine -o --output -u --unit --user-unit -p --priority
48 --vacuum-size --vacuum-time --vacuum-files'
49 [ARGUNKNOWN]='-c --cursor --interval -n --lines -S --since -U --until
50 --after-cursor --verify-key -t --identifier
51 --root'
52 )
53
54 if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then
55 case $prev in
56 --boot|--this-boot|-b)
57 comps=$(journalctl -F '_BOOT_ID' 2>/dev/null)
58 ;;
59 --directory|-D)
60 comps=$(compgen -d -- "$cur")
61 compopt -o filenames
62 ;;
63 --file)
64 comps=$(compgen -f -- "$cur")
65 compopt -o filenames
66 ;;
67 --output|-o)
68 comps='short short-full short-iso short-precise short-monotonic short-unix verbose export json json-pretty json-sse cat'
69 ;;
70 --field|-F)
71 comps=$(journalctl --fields | sort 2>/dev/null)
72 ;;
73 --machine|-M)
74 comps=$( __get_machines )
75 ;;
76 --priority|-p)
77 comps=${__syslog_priorities[*]}
78 ;;
79 --unit|-u)
80 comps=$(journalctl -F '_SYSTEMD_UNIT' 2>/dev/null)
81 ;;
82 --user-unit)
83 comps=$(journalctl -F '_SYSTEMD_USER_UNIT' 2>/dev/null)
84 ;;
85 --identifier|-t)
86 comps=$(journalctl -F 'SYSLOG_IDENTIFIER' 2>/dev/null)
87 ;;
88 *)
89 return 0
90 ;;
91 esac
92 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
93 return 0
94 fi
95
96 if [[ $cur = -* ]]; then
97 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
98 return 0
99 elif [[ $cur = *=* ]]; then
100 mapfile -t field_vals < <(journalctl -F "${prev%=}" 2>/dev/null)
101 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
102 elif [[ $cur = /dev* ]]; then
103 compopt -o filenames
104 COMPREPLY=( $(compgen -f -- "${cur}") )
105 elif [[ $cur = /* ]]; then
106 # Append /dev/ to the list of completions, so that
107 # after typing /<TAB><TAB> the user sees /dev/ as one
108 # of the alternatives. Later on the rule above will
109 # take care of showing device files in /dev/.
110 mapfile -t field_vals < <(journalctl -F "_EXE" 2>/dev/null; echo '/dev/')
111 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur}") )
112 if [[ "${COMPREPLY[@]}" = '/dev/' ]]; then
113 compopt -o filenames
114 COMPREPLY=( $(compgen -f -- "${cur}") )
115 fi
116 elif [[ $prev = '=' ]]; then
117 mapfile -t field_vals < <(journalctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
118 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "$cur") )
119 else
120 mapfile -t field_vals < <(journalctl --fields 2>/dev/null)
121 compopt -o nospace
122 COMPREPLY=( $(compgen -W '${field_vals[*]}' -S= -- "$cur") )
123 fi
124 }
125
126 complete -F _journalctl journalctl