Debian Codegraph @

shell-completion/bash/hostnamectl

raw
1 # shellcheck shell=bash
2 # hostnamectl(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 --no-legend --no-pager 2>/dev/null |
32 { while read -r a b; do echo " $a"; done; }
33 }
34
35 _hostnamectl() {
36 local i verb comps
37 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
38 local -A OPTS=(
39 [STANDALONE]='-h --help --version --transient --static --pretty --no-ask-password -j'
40 [ARG]='-H --host -M --machine --json'
41 )
42
43 if __contains_word "$prev" ${OPTS[ARG]}; then
44 case $prev in
45 --host|-H)
46 comps=$(compgen -A hostname)
47 ;;
48 --machine|-M)
49 comps=$( __get_machines )
50 ;;
51 --json)
52 comps=$( hostnamectl --json=help 2>/dev/null )
53 ;;
54 *)
55 return 0
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 [STANDALONE]='status'
69 [ICONS]='icon-name'
70 [NAME]='hostname deployment location tags'
71 [CHASSIS]='chassis'
72 )
73
74 for ((i=0; i < COMP_CWORD; i++)); do
75 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
76 verb=${COMP_WORDS[i]}
77 break
78 fi
79 done
80
81 if [[ -z ${verb-} ]]; then
82 comps=${VERBS[*]}
83 elif __contains_word "$verb" ${VERBS[CHASSIS]}; then
84 comps='desktop laptop convertible server tablet handset watch embedded vm container'
85 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[ICONS]} ${VERBS[NAME]}; then
86 comps=''
87 fi
88
89 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
90 return 0
91 }
92
93 complete -F _hostnamectl hostnamectl