]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shell-completion: support --json option for hostnamectl 19592/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 12 May 2021 16:37:41 +0000 (01:37 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 14 May 2021 00:18:29 +0000 (09:18 +0900)
shell-completion/bash/hostnamectl
shell-completion/zsh/_hostnamectl

index 5c8d3975d77fd8bce2e67e8e6502a4ea1e3030ce..af4339111e15d2508ffa19ecf37c3760a44c2e54 100644 (file)
@@ -25,11 +25,38 @@ __contains_word () {
     done
 }
 
+__get_machines() {
+    local a b
+    machinectl list --full --no-legend --no-pager 2>/dev/null |
+        { while read a b; do echo " $a"; done; };
+}
+
 _hostnamectl() {
     local i verb comps
     local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
-    local OPTS='-h --help --version --transient --static --pretty
-                --no-ask-password -H --host -M --machine'
+    local -A OPTS=(
+        [STANDALONE]='-h --help --version --transient --static --pretty --no-ask-password'
+        [ARG]='-H --host -M --machine --json'
+    )
+
+    if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then
+        case $prev in
+            --host|-H)
+                comps=$(compgen -A hostname)
+                ;;
+            --machine|-M)
+                comps=$( __get_machines )
+                ;;
+            --json)
+                comps=$( hostnamectl --json=help 2>/dev/null )
+                ;;
+            *)
+                return 0
+                ;;
+        esac
+        COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
+        return 0
+    fi
 
     if [[ $cur = -* ]]; then
         COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
index 2a6bf4d125c4f0c9b4c70696498112e7c55b6e44..2f1f51005d2b6097ed9b9c9ca318f8bf90c0404d 100644 (file)
@@ -75,6 +75,13 @@ _hostnamectl_commands() {
     fi
 }
 
+(( $+functions[_hostnamectl_get_json] )) || _hostnamectl_get_json()
+{
+    local -a _json_forms
+    _json_forms=( $(hostnamectl --json=help 2>/dev/null) )
+    _values 'format' $_json_forms
+}
+
 _arguments -s \
     {-h,--help}'[Show this help]' \
     '--version[Show package version]' \
@@ -84,4 +91,5 @@ _arguments -s \
     '--no-ask-password[Do not prompt for password]' \
     {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
     {-M+,--machine=}'[Operate on local container]:machines:_sd_machines' \
+    '--json[Shows output formatted as JSON]:format:_hostnamectl_get_json' \
     '*::hostnamectl commands:_hostnamectl_commands'