f73c29a8203f8df539a72d28763bc5f521b775c0

Author
Michele Bologna <michele.bologna@gmail.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Feat: add Salt completion plugin (#7031)

* Feat: add Salt completion

* Docs: add README

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/salt/README.md b/plugins/salt/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..3d224cce7f7aaeac244290d01f115a79f66c23cd
  4--- /dev/null
  5+++ b/plugins/salt/README.md
  6@@ -0,0 +1,5 @@
  7+## Salt autocomplete plugin
  8+
  9+A copy of the completion script from the
 10+[salt](https://github.com/saltstack/salt/blob/develop/pkg/zsh_completion.zsh)
 11+git repo.
 12diff --git a/plugins/salt/_salt b/plugins/salt/_salt
 13new file mode 100644
 14index 0000000000000000000000000000000000000000..10b782af4095bed7d9a177d6cd1000d3e15b4389
 15--- /dev/null
 16+++ b/plugins/salt/_salt
 17@@ -0,0 +1,279 @@
 18+#compdef salt salt-call salt-cp salt-run salt-key
 19+# The use-cache style is checked in a few places to allow caching minions, modules,
 20+# or the directory salt is installed in.
 21+# you can cache those three with:
 22+# zstyle ':completion:*:salt(|-cp|-call|-run|-key):*' use-cache true
 23+# and/or selectively:
 24+# zstyle ':completion::complete:salt-key:set-option-a-1:'             use-cache false
 25+# zstyle ':completion::complete:salt(|-cp|-call):minions:'            use-cache true
 26+# zstyle ':completion::complete:salt(|-call):modules:'                use-cache true
 27+# zstyle ':completion::complete:salt(|-cp|-call|-run|-key):salt_dir:' use-cache true
 28+#
 29+# cache validation can be controled with the style cache-ttl.
 30+# it expects two arguments: number (days|hours|weeks|months)
 31+# to invalidate the minion cache after four days:
 32+# zstyle ':completion::complete:salt(|-cp|-call):minions:'            cache-ttl 4 days
 33+
 34+
 35+local state line curcontext="$curcontext" salt_dir
 36+
 37+_modules(){
 38+    local _funcs expl curcontext=${curcontext%:*}:modules
 39+
 40+    if ! zstyle -m ":completion:$curcontext:" cache-policy '*'; then
 41+        zstyle ":completion:$curcontext:" cache-policy _salt_caching_policy
 42+    fi
 43+
 44+    if _cache_invalid salt/modules || ! _retrieve_cache salt/modules; then
 45+        _funcs=( ${${(Q)${${(s. .)"$(_call_program salt-call-cmd salt-call --local --log-level error --out txt sys.list_functions)"}%%[],]##}#\[}:#local:} )
 46+        _store_cache salt/modules _funcs
 47+    fi
 48+
 49+    _wanted modules expl modules _multi_parts "$@" . _funcs
 50+}
 51+
 52+_runners(){
 53+    local _runs expl curcontext=${curcontext%:*}:runners
 54+
 55+    if ! zstyle -m ":completion:$curcontext:" cache-policy '*'; then
 56+        zstyle ":completion:$curcontext:" cache-policy _salt_caching_policy
 57+    fi
 58+
 59+    if _cache_invalid salt/runners || ! _retrieve_cache salt/runners; then
 60+        _runs=( ${${(Q)${${(s. .)"$(_call_program salt-call-cmd salt-call --local --log-level error --out txt sys.list_runner_functions)"}%%[],]##}#\[}:#local:} )
 61+        _store_cache salt/runners _runs
 62+    fi
 63+
 64+    _wanted modules expl runners _multi_parts "$@" . _runs
 65+}
 66+
 67+_minions(){
 68+    local type requested_type include_all key expl; typeset -A _peons
 69+
 70+    # when completing the minion argument for salt and salt-cp, set the argument section
 71+    # of the context to `minion' not `argument-1'
 72+    if [[ $service = salt(|-cp) ]]; then
 73+       curcontext=${curcontext%:*}:minions
 74+    fi
 75+
 76+    # only pass the argument accepted, unaccepted, rejected, denied or all to -t/-T
 77+    # the argument is used as part of an tag, accepted-minions, rejected-minions, etc.
 78+    # while un, acc, den, etc will work, you will possibly ignore user customized tags.
 79+    zparseopts -D -E 't+:=requested_type' 'T+:=include_all'
 80+
 81+    if ! zstyle -m ":completion:$curcontext:" cache-policy '*'; then
 82+        zstyle ":completion:$curcontext:" cache-policy _salt_caching_policy
 83+    fi
 84+
 85+    if _cache_invalid salt/minions || ! _retrieve_cache salt/minions; then
 86+        # it would be awesome if salt-key could prefix or suffix a word to denote
 87+        # the key's state. It would remove the need for this loop, calling salt-key N times.
 88+        for type in accepted unaccepted rejected denied; do
 89+            salt-key -l $type 2>/dev/null | while read -r key; do
 90+              [[ $key == *' Keys:' ]] && continue
 91+              _peons+=( "$key" $type )
 92+            done
 93+        done
 94+        _store_cache salt/minions _peons
 95+    fi
 96+
 97+    # if salt-key's --include-all option isn't on the line, ignore the -T options
 98+    (( words[(I)--include-all] )) || unset include_all
 99+
100+    if (( requested_type[(I)all] )); then
101+        requested_type=( -t accepted -t unaccepted -t rejected -t denied )
102+        unset include_all
103+    fi
104+
105+    for type in ${${requested_type:#-t}:-accepted} ${include_all:#-T}; do
106+        _wanted $type-minions expl minion compadd "$@" -M 'r:|.=* r:|=*' ${(k)_peons[(R)$~type]}
107+    done
108+}
109+
110+(( $+functions[_salt_caching_policy] )) ||
111+_salt_caching_policy() {
112+    local oldp ttl d t
113+    zstyle -a ":completion:$curcontext:" cache-ttl ttl
114+
115+    if (( $#ttl >= 2 )); then
116+      [[ $ttl[1] == <-> ]] && integer t=$ttl[1]