Debian Codegraph @

shell-completion/bash/networkctl

raw
1 # shellcheck shell=bash
2 # networkctl(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 return 1
26 }
27
28 __get_links() {
29 networkctl list --no-legend --no-pager --all --full 2>/dev/null | { while read -r a b c; do echo " $b"; done; }
30 }
31
32 __get_links_with_prefix() {
33 local i
34 for i in $(__get_links); do
35 echo "@$i"
36 done
37 }
38
39 __get_files() {
40 ls {/run,/usr/lib,/etc}/systemd/network/*.{network,netdev,link} 2>/dev/null
41 }
42
43 _networkctl() {
44 local i verb comps
45 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword
46 local -A OPTS=(
47 [STANDALONE]='-a --all -h --help --version --no-pager --no-legend --no-ask-password
48 -s --stats -l --full --no-reload --runtime'
49 [ARG]='-n --lines --json --drop-in'
50 )
51
52 local -A VERBS=(
53 [STANDALONE]='label reload'
54 [LINKS]='status dhcp-lease list lldp delete renew up down forcerenew reconfigure'
55 [FILES_OR_LINKS]='edit cat'
56 [FILES]='mask unmask'
57 [BOOL]='persistent-storage'
58 )
59
60 _init_completion || return
61
62 for ((i=0; i < COMP_CWORD; i++)); do
63 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
64 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
65 verb=${COMP_WORDS[i]}
66 break
67 fi
68 done
69
70 if __contains_word "$prev" ${OPTS[ARG]}; then
71 case $prev in
72 --json)
73 comps=$(networkctl --json=help 2>/dev/null)
74 ;;
75 *)
76 return 0
77 ;;
78 esac
79 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
80 return 0
81 fi
82
83 if [[ "$cur" = -* ]]; then
84 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
85 return 0
86 fi
87
88 if [[ -z ${verb-} ]]; then
89 comps=${VERBS[*]}
90 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
91 comps=''
92 elif __contains_word "$verb" ${VERBS[LINKS]}; then
93 comps=$( __get_links )
94 elif __contains_word "$verb" ${VERBS[FILES_OR_LINKS]}; then
95 comps=$( __get_files; __get_links_with_prefix )
96 elif __contains_word "$verb" ${VERBS[FILES]}; then
97 comps=$( __get_files )
98 elif __contains_word "$verb" ${VERBS[BOOL]}; then
99 comps=$( yes no )
100 fi
101
102 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
103 return 0
104 }
105
106 complete -F _networkctl networkctl