chiark / gitweb /
test: fix some tests when running inside a container
[elogind.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 __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
28                   ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE
29                   _{P,U,G}ID _COMM _EXE _CMDLINE
30                   _AUDIT_{SESSION,LOGINUID}
31                   _SYSTEMD_{CGROUP,SESSION,UNIT,OWNER_UID}
32                   _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
33                   _{BOOT,MACHINE}_ID _HOSTNAME _TRANSPORT
34                   _KERNEL_{DEVICE,SUBSYSTEM}
35                   _UDEV_{SYSNAME,DEVNODE,DEVLINK}
36                   __CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP)
37
38 __syslog_priorities=(emerg alert crit err warning notice info debug)
39
40 _journalctl() {
41         local field_vals= cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
42         local -A OPTS=(
43                 [STANDALONE]='-a --all --full --system --user
44                               --disk-usage -f --follow --header
45                               -h --help -l --local --new-id128 -m --merge --no-pager
46                               --no-tail -q --quiet --setup-keys --this-boot --verify
47                               --version --list-catalog --update-catalog --list-boots'
48                        [ARG]='-b --boot --this-boot -D --directory --file -F --field
49                               -o --output -u --unit --user-unit -p --priority'
50                 [ARGUNKNOWN]='-c --cursor --interval -n --lines --since --until
51                               --verify-key'
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-monotonic verbose export json cat'
69                         ;;
70                         --field|-F)
71                                 comps=${__journal_fields[*]}
72                         ;;
73                         --priority|-p)
74                                 comps=${__syslog_priorities[*]}
75                         ;;
76                         --unit|-u)
77                                 comps=$(journalctl -F '_SYSTEMD_UNIT' 2>/dev/null)
78                         ;;
79                         --user-unit)
80                                 comps=$(journalctl -F '_SYSTEMD_USER_UNIT' 2>/dev/null)
81                         ;;
82                         *)
83                                 return 0
84                         ;;
85                 esac
86                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
87                 return 0
88         fi
89
90         if [[ $cur = -* ]]; then
91                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
92                 return 0
93         elif [[ $cur = *=* ]]; then
94                 mapfile -t field_vals < <(journalctl -F "${prev%=}" 2>/dev/null)
95                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
96         elif [[ $cur = /dev* ]]; then
97                 compopt -o filenames
98                 COMPREPLY=( $(compgen -f -- "${cur}") )
99         elif [[ $cur = /* ]]; then
100                 # Append /dev/ to the list of completions, so that
101                 # after typing /<TAB><TAB> the user sees /dev/ as one
102                 # of the alternatives. Later on the rule above will
103                 # take care of showing device files in /dev/.
104                 mapfile -t field_vals < <(journalctl -F "_EXE" 2>/dev/null; echo '/dev/')
105                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur}") )
106                 if [[ "${COMPREPLY[@]}" = '/dev/' ]]; then
107                     compopt -o filenames
108                     COMPREPLY=( $(compgen -f -- "${cur}") )
109                 fi
110         elif [[ $prev = '=' ]]; then
111                 mapfile -t field_vals < <(journalctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
112                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "$cur") )
113         else
114                 compopt -o nospace
115                 COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") )
116         fi
117 }
118
119 complete -F _journalctl journalctl