Debian Codegraph @

shell-completion/bash/systemd-run

raw
1 # shellcheck shell=bash
2 # systemd-run(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 __systemctl() {
21 local mode=$1; shift 1
22 systemctl $mode --full --no-legend --no-pager --plain "$@"
23 }
24
25 __get_slice_units () { __systemctl $1 list-units --all -t slice 2>/dev/null |
26 { while read -r a b c d; do echo " $a"; done; }; }
27
28 __get_properties () {
29 systemd-analyze transient-settings scope service 2>/dev/null |
30 while read -r p; do [ -z "$p" ] || echo "$p="; done
31 }
32
33 __get_path_properties () {
34 systemd-analyze transient-settings path 2>/dev/null |
35 while read -r p; do [ -z "$p" ] || echo "$p="; done
36 }
37
38 __get_socket_properties () {
39 systemd-analyze transient-settings socket 2>/dev/null |
40 while read -r p; do [ -z "$p" ] || echo "$p="; done
41 }
42
43 __get_timer_properties () {
44 systemd-analyze transient-settings timer 2>/dev/null |
45 while read -r p; do [ -z "$p" ] || echo "$p="; done
46 }
47
48 __get_machines() {
49 local a b
50 { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } |
51 { while read -r a b; do echo " $a"; done; } |
52 sort -u
53 }
54
55 _systemd_run() {
56 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
57 local opts_with_values=(
58 --unit -p --property --slice --description --service-type --uid --gid --nice --working-directory
59 -E --setenv --on-active --on-boot --on-startup --on-unit-active --on-unit-inactive --on-calendar
60 --path-property --socket-property --timer-property -H --host -M --machine --expand-environment
61 --background --json --job-mode
62 )
63 local OPTS="${opts_with_values[*]} --no-ask-password --no-pager
64 --scope -u --slice-inherit -r --remain-after-exit
65 --send-sighup -d --same-dir -t --pty -P --pipe -S --shell -q --quiet --ignore-failure
66 --on-clock-change --on-timezone-change --no-block --wait -G --collect --user --system -h --help --version -v --verbose"
67 local mode=--system
68 local i
69 for (( i=1; i <= COMP_CWORD; i++ )); do
70 if [[ ${COMP_WORDS[i]} != -* ]]; then
71 local root_command=${COMP_WORDS[i]}
72 _command_offset $i
73 return
74 fi
75
76 [[ ${COMP_WORDS[i]} == "--user" ]] && mode=--user
77
78 [[ $i -lt $COMP_CWORD && " ${opts_with_values[*]} " =~ " ${COMP_WORDS[i]} " ]] && ((i++))
79 done
80
81 case "$prev" in
82 --unit|--description|--nice|-E|--setenv|--on-active|--on-boot|--on-startup|--on-unit-active|--on-unit-inactive|--on-calendar|--background)
83 # argument required but no completions available
84 return
85 ;;
86 --uid)
87 _comp_compgen -- -u
88 return
89 ;;
90 --gid)
91 _comp_compgen -- -g
92 return
93 ;;
94 --expand-environment)
95 local comps='yes no'
96
97 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
98 return 0
99 ;;
100 --slice)
101 local comps=$(__get_slice_units $mode)
102
103 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
104 return 0
105 ;;
106 --service-type)
107 local comps='simple forking oneshot dbus notify idle'
108
109 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
110 return 0
111 ;;
112 -p|--property)
113 local comps=$(__get_properties)
114
115 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
116 return 0
117 ;;
118 -H|--host)
119 local comps=$(compgen -A hostname)
120
121 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
122 return 0
123 ;;
124 -M|--machine)
125 local comps=$( __get_machines )
126 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
127 return 0
128 ;;
129 --path-property)
130 local comps=$(__get_path_properties)
131 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
132 return 0
133 ;;
134 --socket-property)
135 local comps=$(__get_socket_properties)
136 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
137 return 0
138 ;;
139 --timer-property)
140 local comps=$(__get_timer_properties)
141 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
142 return 0
143 ;;
144 --working-directory)
145 local comps
146 if [[ -z $cur ]]; then
147 comps=$(compgen -A directory -- "/" )
148 else
149 comps=$(compgen -A directory -- "$cur" )
150 fi
151 compopt -o filenames
152 COMPREPLY=( $(compgen -W '$comps' -- "$cur" ) )
153 return 0
154 ;;
155 --json)
156 local comps='pretty short off'
157 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
158 return 0
159 ;;
160 --job-mode)
161 local comps=$( systemd-run --job-mode=help 2>/dev/null )
162 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
163 return 0
164 ;;
165 esac
166
167 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
168 return 0
169 }
170
171 complete -F _systemd_run systemd-run