Debian Codegraph @

shell-completion/bash/timedatectl

raw
1 # shellcheck shell=bash
2 # timedatectl(1) completion -*- shell-script -*-
3 # SPDX-License-Identifier: LGPL-2.1-or-later
4 #
5 # This file is part of systemd.
6 #
7 # Copyright © 2010 Ran Benita
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
20 # along with systemd; If not, see <https://www.gnu.org/licenses/>.
21
22 __contains_word () {
23 local w word=$1; shift
24 for w in "$@"; do
25 [[ $w = "$word" ]] && return
26 done
27 }
28
29 __get_machines() {
30 local a b
31 { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } |
32 { while read -r a b; do echo " $a"; done; } |
33 sort -u
34 }
35
36 __get_interfaces(){
37 local name
38 for name in $(cd /sys/class/net && command ls); do
39 [[ "$name" != "lo" ]] && echo "$name"
40 done
41 }
42 _timedatectl() {
43 local i verb comps
44 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
45 local OPTS='-h --help --version --adjust-system-clock --no-pager
46 --no-ask-password -H --host -M --machine --monitor
47 -p --property -a --all --value'
48
49 if __contains_word "$prev" $OPTS; then
50 case $prev in
51 --host|-H)
52 comps=$(compgen -A hostname)
53 ;;
54 --machine|-M)
55 comps=$( __get_machines )
56 ;;
57 esac
58 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
59 return 0
60 fi
61
62 if [[ $cur = -* ]]; then
63 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
64 return 0
65 fi
66
67 local -A VERBS=(
68 [BOOLEAN]='set-local-rtc set-ntp'
69 [STANDALONE]='status show list-timezones timesync-status show-timesync'
70 [TIMEZONES]='set-timezone'
71 [TIME]='set-time'
72 [IFNAME]='ntp-servers revert'
73 )
74
75 for ((i=0; i < COMP_CWORD; i++)); do
76 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
77 verb=${COMP_WORDS[i]}
78 break
79 fi
80 done
81
82 if [[ -z ${verb-} ]]; then
83 comps=${VERBS[*]}
84 elif __contains_word "$verb" ${VERBS[BOOLEAN]}; then
85 comps='true false'
86 elif __contains_word "$verb" ${VERBS[TIMEZONES]}; then
87 comps=$(command timedatectl list-timezones)
88 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[TIME]}; then
89 comps=''
90 elif __contains_word "$verb" ${VERBS[TIMEZONES]}; then
91 comps=$( __get_interfaces )
92 fi
93
94 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
95 return 0
96 }
97
98 complete -F _timedatectl timedatectl