]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
zsh: add busctl completions
authorRonan Pigott <rpigott@berkeley.edu>
Sat, 10 Aug 2019 08:21:35 +0000 (01:21 -0700)
committerRonan Pigott <rpigott@berkeley.edu>
Sat, 10 Aug 2019 08:21:35 +0000 (01:21 -0700)
shell-completion/zsh/_busctl

index 4e0362423c6549e8b2445d286c8dcc7f4ebe9447..9c3654316eabcba0fffcfaa07c104c7bb9766ca7 100644 (file)
@@ -37,6 +37,7 @@
     else
         local curcontext="$curcontext"
         cmd="${${_busctl_cmds[(r)$words[1]:*]%%:*}}"
+        curcontext="${curcontext%:*:*}:busctl-${cmd}:"
         if (( $+functions[_busctl_$cmd] )); then
             _busctl_$cmd
         else
     fi
 }
 
+__busctl() {
+    busctl $_bus_address --no-pager --no-legend "$@" 2>/dev/null
+}
+
+(( $+functions[_busctl_get_json] )) || _busctl_get_json()
+{
+    local -a _json_forms
+    _json_forms=( $(__busctl --json=help; echo help) )
+    _values 'format' $_json_forms
+}
+
+(( $+functions[_busctl_get_service_names] )) || _busctl_get_service_names()
+{
+    local -a bus_names
+    bus_names=( $(__busctl call \
+        "org.freedesktop.DBus"  \
+        "/org/freedesktop/DBus" \
+        "org.freedesktop.DBus"  \
+        ListNames) )
+    echo ${(Q)bus_names[3,-1]}
+}
+
+(( $+functions[_busctl_get_objects] )) || _busctl_get_objects()
+{
+    local -a objects
+    local name="$1"
+    objects=($(__busctl --list tree $name ))
+    echo $objects
+}
+
+(( $+functions[_busctl_get_interfaces] )) || _busctl_get_interfaces()
+{
+    local NAME TYPE OTHER
+    __busctl introspect "$1" "$2" |
+    while read NAME TYPE OTHER; do
+        if [[ ${TYPE} == "interface" ]]; then
+            echo ${NAME}
+        fi
+    done
+}
+
+(( $+functions[_busctl_get_members] )) || _busctl_get_members()
+{
+    local member="$4"
+    local required="$5"
+    local NAME TYPE SIGNATURE VALUE FLAGS
+    __busctl introspect "$1" "$2" "$3" |
+    while read NAME TYPE SIGNATURE VALUE FLAGS; do
+        [[ -z "$member" || ${TYPE} == "$member" ]] &&
+        [[ -z "$required" || ${${(s: :)FLAGS}[-1]} == "$required" ]] &&
+        echo ${NAME#.}
+    done
+}
+
+(( $+functions[_busctl_get_signature] )) || _busctl_get_signature()
+{
+    local NAME TYPE SIGNATURE VALUE FLAGS
+    __busctl introspect "$1" "$2" "$3" |
+    while read NAME TYPE SIGNATURE VALUE FLAGS; do
+        if [[ ${NAME#.} == "$4" ]]; then
+            [[ ${SIGNATURE} != "-" ]] && echo ${SIGNATURE}
+        fi
+    done
+}
+
+(( $+functions[_busctl_status] )) || _busctl_status()
+{
+    local expl
+    _wanted busname expl 'busname' compadd "$@" - $(_busctl_get_service_names)
+}
+
+(( $+functions[_busctl_monitor] )) || _busctl_monitor()
+{
+    local expl
+    _wanted busname expl 'busname' compadd "$@" - $(_busctl_get_service_names)
+}
+
+(( $+functions[_busctl_tree] )) || _busctl_tree()
+{
+    local expl
+    _wanted busname expl 'busname' compadd "$@" - $(_busctl_get_service_names)
+}
+
+(( $+functions[_busctl_introspect] )) || _busctl_introspect()
+{
+    local expl
+    case $CURRENT in
+        2)
+            _wanted busname expl 'busname' \
+            compadd "$@" - $(_busctl_get_service_names)
+            ;;
+        3)
+            _wanted path expl 'path' \
+            compadd "$@" - $(_busctl_get_objects $words[2])
+            ;;
+        4)
+            _wanted interface expl 'interface' \
+            compadd "$@" - $(_busctl_get_interfaces $words[2,3])
+            ;;
+        *)
+            _message "no more options"
+    esac
+}
+
+(( $+functions[_busctl_call] )) || _busctl_call()
+{
+    local expl
+    case $CURRENT in
+        2)
+            _wanted busname expl 'busname' \
+            compadd "$@" - $(_busctl_get_service_names)
+            ;;
+        3)
+            _wanted path expl 'path' \
+            compadd "$@" - $(_busctl_get_objects $words[2])
+            ;;
+        4)
+            _wanted interface expl 'interface' \
+            compadd "$@" - $(_busctl_get_interfaces $words[2,3])
+            ;;
+        5)
+            _wanted method expl 'method' \
+            compadd "$@" - $(_busctl_get_members $words[2,4] "method")
+            ;;
+        6)
+            compadd "$@" - $(_busctl_get_signature $words[2,5])
+            ;;
+        *)
+            _message "no more options"
+    esac
+}
+
+(( $+functions[_busctl_get-property] )) || _busctl_get-property()
+{
+    local expl
+    case $CURRENT in
+        2)
+            _wanted busname expl 'busname' \
+            compadd "$@" - $(_busctl_get_service_names)
+            ;;
+        3)
+            _wanted path expl 'path' \
+            compadd "$@" - $(_busctl_get_objects $words[2])
+            ;;
+        4)
+            _wanted interface expl 'interface' \
+            compadd "$@" - $(_busctl_get_interfaces $words[2,3])
+            ;;
+        5)
+            _wanted property expl 'property' \
+            compadd "$@" - $(_busctl_get_members $words[2,4] "property")
+            ;;
+        *)
+            _message "no more options"
+    esac
+}
+
+(( $+functions[_busctl_set-property] )) || _busctl_set-property()
+{
+    local expl
+    case $CURRENT in
+        2)
+            _wanted busname expl 'busname' \
+            compadd "$@" - $(_busctl_get_service_names)
+            ;;
+        3)
+            _wanted path expl 'path' \
+            compadd "$@" - $(_busctl_get_objects $words[2])
+            ;;
+        4)
+            _wanted interface expl 'interface' \
+            compadd "$@" - $(_busctl_get_interfaces $words[2,3])
+            ;;
+        5)
+            _wanted property expl 'property' \
+            compadd "$@" - $(_busctl_get_members $words[2,4] "property" "writable")
+            ;;
+        6)
+            compadd "$@" - $(_busctl_get_signature $words[2,5])
+            ;;
+        *)
+            _message "no more options"
+    esac
+}
+
+local -a _modes; _modes=("--user" "--system")
+# Use the last mode (they are exclusive and the last one is used).
+local _bus_address=${${words:*_modes}[(R)(${(j.|.)_modes})]}
 _arguments \
     {-h,--help}'[Prints a short help text and exits.]' \
     '--version[Prints a short version string and exits.]' \
@@ -61,8 +250,10 @@ _arguments \
     '--activatable[Only show activatable names]' \
     '--match=[Only show matching messages]:match' \
     '--list[Do not show tree, but simple object path list]' \
-    '--quiet[Do not show method call reply]'\
+    {-q,--quiet}'[Do not show method call reply]'\
     '--verbose[Show result values in long format]' \
+    '--json=[Show result values in long format]:format:_busctl_get_json' \
+    '-j[Show pretty json in interactive sessions, short json otherwise]' \
     '--expect-reply=[Expect a method call reply]:boolean:(1 0)' \
     '--auto-start=[Auto-start destination service]:boolean:(1 0)' \
     '--allow-interactive-authorization=[Allow interactive authorization for operation]:boolean:(1 0)' \