| 1 |
# shellcheck shell=bash |
| 2 |
# systemd-cgls(1) completion -*- shell-script -*- |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
# |
| 5 |
# This file is part of systemd. |
| 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 <https://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 --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | |
| 30 |
{ while read -r a b; do echo " $a"; done; } | |
| 31 |
sort -u |
| 32 |
} |
| 33 |
|
| 34 |
__get_units_have_cgroup() { |
| 35 |
systemctl $1 --full --no-legend --no-pager --plain list-units 2>/dev/null | { |
| 36 |
while read -r a b c d; do |
| 37 |
[[ $c == "active" && ${a##*.} =~ (service|socket|mount|swap|slice|scope) ]] && echo " $a" |
| 38 |
done |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
_systemd_cgls() { |
| 43 |
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword |
| 44 |
local i verb comps |
| 45 |
|
| 46 |
local -A OPTS=( |
| 47 |
[STANDALONE]='-h --help --version --all -l --full -k --no-pager --xattr=no --cgroup-id=no' |
| 48 |
[ARG]='-c --cgroup-id -M --machine -u --unit --user-unit -x --xattr' |
| 49 |
) |
| 50 |
|
| 51 |
_init_completion || return |
| 52 |
|
| 53 |
if __contains_word "$prev" ${OPTS[ARG]}; then |
| 54 |
case $prev in |
| 55 |
--machine|-M) |
| 56 |
comps=$( __get_machines ) |
| 57 |
;; |
| 58 |
--unit|-u) |
| 59 |
comps=$( __get_units_have_cgroup --system ) |
| 60 |
;; |
| 61 |
-c|--cgroup-id|-x|--xattr) |
| 62 |
comps='yes no' |
| 63 |
;; |
| 64 |
--user-unit) |
| 65 |
comps=$( __get_units_have_cgroup --user ) |
| 66 |
;; |
| 67 |
esac |
| 68 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 69 |
return 0 |
| 70 |
fi |
| 71 |
|
| 72 |
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) |
| 73 |
} |
| 74 |
|
| 75 |
complete -F _systemd_cgls systemd-cgls |