X Tutup
Skip to content

Commit f8cd3f6

Browse files
committed
shell-completion: support --json option for hostnamectl
1 parent bfc2b05 commit f8cd3f6

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

shell-completion/bash/hostnamectl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,38 @@ __contains_word () {
2525
done
2626
}
2727

28+
__get_machines() {
29+
local a b
30+
machinectl list --full --no-legend --no-pager 2>/dev/null |
31+
{ while read a b; do echo " $a"; done; };
32+
}
33+
2834
_hostnamectl() {
2935
local i verb comps
3036
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
31-
local OPTS='-h --help --version --transient --static --pretty
32-
--no-ask-password -H --host -M --machine'
37+
local -A OPTS=(
38+
[STANDALONE]='-h --help --version --transient --static --pretty --no-ask-password'
39+
[ARG]='-H --host -M --machine --json'
40+
)
41+
42+
if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then
43+
case $prev in
44+
--host|-H)
45+
comps=$(compgen -A hostname)
46+
;;
47+
--machine|-M)
48+
comps=$( __get_machines )
49+
;;
50+
--json)
51+
comps=$( hostnamectl --json=help 2>/dev/null )
52+
;;
53+
*)
54+
return 0
55+
;;
56+
esac
57+
COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
58+
return 0
59+
fi
3360

3461
if [[ $cur = -* ]]; then
3562
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )

shell-completion/zsh/_hostnamectl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ _hostnamectl_commands() {
7575
fi
7676
}
7777

78+
(( $+functions[_hostnamectl_get_json] )) || _hostnamectl_get_json()
79+
{
80+
local -a _json_forms
81+
_json_forms=( $(hostnamectl --json=help 2>/dev/null) )
82+
_values 'format' $_json_forms
83+
}
84+
7885
_arguments -s \
7986
{-h,--help}'[Show this help]' \
8087
'--version[Show package version]' \
@@ -84,4 +91,5 @@ _arguments -s \
8491
'--no-ask-password[Do not prompt for password]' \
8592
{-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
8693
{-M+,--machine=}'[Operate on local container]:machines:_sd_machines' \
94+
'--json[Shows output formatted as JSON]:format:_hostnamectl_get_json' \
8795
'*::hostnamectl commands:_hostnamectl_commands'

0 commit comments

Comments
 (0)
X Tutup