zsh-workers
 help / color / mirror / code / Atom feed
* zsh completion for upstart commands initctl start stop restart reload and status
@ 2011-05-25 17:17 Bernhard Tittelbach
  2011-05-25 20:50 ` Marc Weber
  2011-05-26  9:36 ` Peter Stephenson
  0 siblings, 2 replies; 9+ messages in thread
From: Bernhard Tittelbach @ 2011-05-25 17:17 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 434 bytes --]

Hi,

I wrote an extensive zsh completion script for initctl and it's aliases.
It's based on Mildred original script from 2007 but updated for natty and with 
lot's of improvements. (had quite some fun, zsh is a great shell :-)

It's also been thoroughly tested by myself and friends.

So now I'm posting it here in the hope that it's helpful :-)

File is attached and/or available here: http://paste.grml.org/429/

Cheers,
Bernhard



[-- Attachment #2: _initctl --]
[-- Type: text/plain, Size: 6279 bytes --]

#compdef initctl start stop restart reload status
# Written by Bernhard Tittelbach
# based on completion script by Mildred

local -a common_args
common_args=(
  '--session[use D-Bus session bus to connect to init daemon (for testing)]'
  '--system[talk via DBUS system bus instead of socket]'
  '(-q --quiet)'{-q,--quiet}'[reduce output to errors only]'
  '(-v --verbose)'{-v,--verbose}'[increase output to include informational messages]'
  '--dest=[D-Bus name for init, defaults to com.ubuntu.Upstart]'
  '--help[display help and exit]'
  '--version[output version information and exit]'
)

# don't overwrite work that we might have already done
if (( ${+_initctl_events_list} + ${+_initctl_eventargs_list} != 2 )); then
  typeset -g -a -U _initctl_events_list _initctl_eventargs_list
fi

# map each initctl function to a completion function
local -A cmd_completion_funcs
cmd_completion_funcs=( start startstop stop startstop restart startstop reload startstop show-config show-config status argjob emit emit check-config check-config )

# define fallback completion function
local cmd_completion_default=basic

# run show-config -e and if possible parse out all events and KEY= argumnts
# otherwise provide some common values
_initctl_fillarray_events_args ()
{
  setopt extendedglob
  local showconfig="$(initctl show-config -e 2>| /dev/null)"
  if [[ -n "$showconfig" ]]; then
    _initctl_events_list=()
    _initctl_eventargs_list=()
    for cline in "${(f)showconfig}"; do
      if [[ "$cline" == (#s)\ \ (stop\ on|start\ on|emit)\ (#b)([[:alpha:]-_]##)(*)(#e) ]]; then
        _initctl_events_list+=($match[1])
        # this is a bit tricky, we take the string right of the matched event
        # and parse for \sUPPERCASE=\S (in perl-re syntax) substrings until there are no more matches
        # since we can't do multiple matches, we concatenated the remaing strings and try again
        local stml="$match[2]"
        while [[ "$stml" == (#b)(*)\ ([[:upper:]_]##\=)[^[:space:]](#b)(*) ]]; do
          _initctl_eventargs_list+=($match[2])
          stml="$match[1] $match[3]"
        done
        unset stml
      fi
    done
  else
    _initctl_events_list=( socket login-session-start desktop-session-start virtual-filesystems local-filesystems remote-filesystems all-swaps filesystem mounting mounted net-device-up start-portmap runlevel unmounted-remote-filesystems )
    _initctl_eventargs_list=( PRIMARY_DEVICE_FOR_DISPLAY= EXIT_STATUS= EXIT_SIGNAL= RUNLEVEL= MOUNTPOINT= TYPE= INTERFACE= )
  fi
  return 0
}

# list all upstart jobs, i.e. all files in /etc/init/
_initctl_helper_jobs()
{
    _path_files -W "/etc/init/" -g "*.conf(.:r)"
}

# list events, generate array if necessary
_initctl_known_events()
{
  [[ ${#_initctl_events_list} -eq 0 ]] && _initctl_fillarray_events_args
  _values "Event" "$_initctl_events_list[@]"
}

# list events, allow multiple choices, generate array if necessary
_initctl_multiple_known_events()
{
  [[ ${#_initctl_events_list} -eq 0 ]] && _initctl_fillarray_events_args
  _values -s "," "Events" "$_initctl_events_list[@]"
}

# list KEY= arguments, generate array if necessary
_initctl_known_eventargs()
{
  [[ ${#_initctl_eventargs_list} -eq 0 ]] && _initctl_fillarray_events_args
  _values "Argument Keys" "$_initctl_eventargs_list[@]"
}

# describe and offer commands for initctl, then call matching completion function
_initctl_command()
{
    local cmds
    cmds=(
        start:"Start jobs"
        stop:"Stop jobs"
        restart:"Restart jobs"
        reload:"Send SIGHUP to process instance"
        status:"Query status of jobs"
        list:"List known jobs"
        emit:"Emit an event"
        reload-configuration:"tell init to reload config files (generally inotify is used)"
        version:"Request the version of the init daemon"
        log-priority:"Change the minimum priority of log messages from the init daemon"
        show-config:"Show start/stop/emit for processes"
        check-config:"Find jobs than can't be started"
        help:"display list of commands"
    )

    if (( CURRENT == 1 )); then
        _describe -t command "initctl Commands" cmds
    fi

    local cmd=$words[1]

    local curcontext="${curcontext%:*}:initctl-${cmd}"
    _call_function ret _initctl_${cmd_completion_funcs[${cmd}]-${cmd_completion_default}}
}

# completion for start/stop/restart/reload i.e. anything that take one job and multiple KEY= arguments's
_initctl_startstop()
{
    _arguments \
        '--no-wait[do not wait for operation to complete before exiting]' \
        "${common_args[@]}" \
        ':Upstart Jobs:_initctl_helper_jobs' \
        '*::Argument Keys:_initctl_known_eventargs'
}

# completion for anything that takes one job
_initctl_argjob()
{
    _arguments \
        "${common_args[@]}" \
        ':Upstart Jobs:_initctl_helper_jobs' \
        '*::'
}

# completion for emit, providing options,  one event and multiple KEY= arguments's
_initctl_emit()
{
    _arguments \
        '--no-wait[do not wait for event to finish before exiting]' \
        "${common_args[@]}" \
        ':Events:_initctl_known_events' \
        '*::Argument Keys:_initctl_known_eventargs'
}

# the fallback, just the options
_initctl_basic()
{
    _arguments \
        "${common_args[@]}"
}

# completion for show-config, additional option and one job
_initctl_show-config()
{
    _arguments \
      "(-e --enumerate)"{-e,--enumerate}"[enumerate emit lines]" \
        "${common_args[@]}" \
        '::Upstart Jobs:_initctl_helper_jobs' \
        '*::'
}

# completion for show-config, additional options and one job
_initctl_check-config()
{
    _arguments \
      "(-i --ignore-events)"{-i,--ignore-events}"[list of comma-separated events to ignore]:Events:_initctl_multiple_known_events" \
      "(-w --warn)"{-w,--warn}"[treat any unknown jobs or events as error]" \
        "${common_args[@]}" \
        '::Upstart Jobs:_initctl_helper_jobs' \
        '*::'
}

# depending on which command was used, call different completion funtions
case $service in
  initctl)
    _arguments "${common_args[@]}" '*::Initctl Commands:_initctl_command'
  ;;
  start|stop|restart|reload|status)
    _call_function ret _initctl_${cmd_completion_funcs[${service}]-${cmd_completion_default}}
  ;;
  *)  return 1  ;;
esac

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: zsh completion for upstart commands initctl start stop restart reload and status
  2011-05-25 17:17 zsh completion for upstart commands initctl start stop restart reload and status Bernhard Tittelbach
@ 2011-05-25 20:50 ` Marc Weber
  2011-05-26  9:36 ` Peter Stephenson
  1 sibling, 0 replies; 9+ messages in thread
From: Marc Weber @ 2011-05-25 20:50 UTC (permalink / raw)
  To: zsh-workers

Mmh - wouldn't it make sense to start a collaborative repository where
all the completion scripts could be comitted to?

Then everybody could pick the repo and take the files he/she likes.

Maybe it already exists?

Marc Weber


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: zsh completion for upstart commands initctl start stop restart reload and status
  2011-05-25 17:17 zsh completion for upstart commands initctl start stop restart reload and status Bernhard Tittelbach
  2011-05-25 20:50 ` Marc Weber
@ 2011-05-26  9:36 ` Peter Stephenson
  2011-05-26 16:05   ` PATCH: _initctl: only define helper functions once Mikael Magnusson
  1 sibling, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2011-05-26  9:36 UTC (permalink / raw)
  To: Bernhard Tittelbach; +Cc: zsh-workers

On Wed, 25 May 2011 19:17:18 +0200
Bernhard Tittelbach <xro@realraum.at> wrote:
> I wrote an extensive zsh completion script for initctl and it's
> aliases. It's based on Mildred original script from 2007 but updated
> for natty and with lot's of improvements. (had quite some fun, zsh is
> a great shell :-)
> 
> It's also been thoroughly tested by myself and friends.
> 
> So now I'm posting it here in the hope that it's helpful :-)

Thanks, it looks like no one else has done this so I've committed it to
the archive and it'll be in 4.3.12.

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


^ permalink raw reply	[flat|nested] 9+ messages in thread

* PATCH: _initctl: only define helper functions once
  2011-05-26  9:36 ` Peter Stephenson
@ 2011-05-26 16:05   ` Mikael Magnusson
  2011-05-26 16:11     ` Peter Stephenson
  0 siblings, 1 reply; 9+ messages in thread
From: Mikael Magnusson @ 2011-05-26 16:05 UTC (permalink / raw)
  To: zsh-workers

---
 Completion/Unix/Command/_initctl |   69 ++++++++++++++++++++------------------
 1 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/Completion/Unix/Command/_initctl b/Completion/Unix/Command/_initctl
index 08145b1..850064d 100644
--- a/Completion/Unix/Command/_initctl
+++ b/Completion/Unix/Command/_initctl
@@ -2,29 +2,6 @@
 # Written by Bernhard Tittelbach
 # based on completion script by Mildred
 
-local -a common_args
-common_args=(
-  '--session[use D-Bus session bus to connect to init daemon (for testing)]'
-  '--system[talk via DBUS system bus instead of socket]'
-  '(-q --quiet)'{-q,--quiet}'[reduce output to errors only]'
-  '(-v --verbose)'{-v,--verbose}'[increase output to include informational messages]'
-  '--dest=[D-Bus name for init, defaults to com.ubuntu.Upstart]'
-  '--help[display help and exit]'
-  '--version[output version information and exit]'
-)
-
-# don't overwrite work that we might have already done
-if (( ${+_initctl_events_list} + ${+_initctl_eventargs_list} != 2 )); then
-  typeset -g -a -U _initctl_events_list _initctl_eventargs_list
-fi
-
-# map each initctl function to a completion function
-local -A cmd_completion_funcs
-cmd_completion_funcs=( start startstop stop startstop restart startstop reload startstop show-config show-config status argjob emit emit check-config check-config )
-
-# define fallback completion function
-local cmd_completion_default=basic
-
 # run show-config -e and if possible parse out all events and KEY= argumnts
 # otherwise provide some common values
 _initctl_fillarray_events_args ()
@@ -169,13 +146,39 @@ _initctl_check-config()
         '*::'
 }
 
-# depending on which command was used, call different completion funtions
-case $service in
-  initctl)
-    _arguments "${common_args[@]}" '*::Initctl Commands:_initctl_command'
-  ;;
-  start|stop|restart|reload|status)
-    _call_function ret _initctl_${cmd_completion_funcs[${service}]-${cmd_completion_default}}
-  ;;
-  *)  return 1  ;;
-esac
+_initctl()
+{
+  local -a common_args
+  common_args=(
+    '--session[use D-Bus session bus to connect to init daemon (for testing)]'
+    '--system[talk via DBUS system bus instead of socket]'
+    '(-q --quiet)'{-q,--quiet}'[reduce output to errors only]'
+    '(-v --verbose)'{-v,--verbose}'[increase output to include informational messages]'
+    '--dest=[D-Bus name for init, defaults to com.ubuntu.Upstart]'
+    '--help[display help and exit]'
+    '--version[output version information and exit]'
+  )
+
+  # don't overwrite work that we might have already done
+  if (( ${+_initctl_events_list} + ${+_initctl_eventargs_list} != 2 )); then
+    typeset -g -a -U _initctl_events_list _initctl_eventargs_list
+  fi
+
+  # map each initctl function to a completion function
+  local -A cmd_completion_funcs
+  cmd_completion_funcs=( start startstop stop startstop restart startstop reload startstop show-config show-config status argjob emit emit check-config check-config )
+
+  # define fallback completion function
+  local cmd_completion_default=basic
+
+  # depending on which command was used, call different completion funtions
+  case $service in
+    initctl)
+      _arguments "${common_args[@]}" '*::Initctl Commands:_initctl_command'
+    ;;
+    start|stop|restart|reload|status)
+      _call_function ret _initctl_${cmd_completion_funcs[${service}]-${cmd_completion_default}}
+    ;;
+    *)  return 1  ;;
+  esac
+}
-- 
1.7.4-rc1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: _initctl: only define helper functions once
  2011-05-26 16:05   ` PATCH: _initctl: only define helper functions once Mikael Magnusson
@ 2011-05-26 16:11     ` Peter Stephenson
  2011-05-26 16:49       ` Mikael Magnusson
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2011-05-26 16:11 UTC (permalink / raw)
  To: zsh-workers

On Thu, 26 May 2011 18:05:23 +0200
Mikael Magnusson <mikachu@gmail.com> wrote:
> +}

Don't you need

_initctl "$@"

?

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: _initctl: only define helper functions once
  2011-05-26 16:11     ` Peter Stephenson
@ 2011-05-26 16:49       ` Mikael Magnusson
  2011-05-26 19:37         ` Bernhard Tittelbach
  0 siblings, 1 reply; 9+ messages in thread
From: Mikael Magnusson @ 2011-05-26 16:49 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

On 26 May 2011 18:11, Peter Stephenson <Peter.Stephenson@csr.com> wrote:
> On Thu, 26 May 2011 18:05:23 +0200
> Mikael Magnusson <mikachu@gmail.com> wrote:
>> +}
>
> Don't you need
>
> _initctl "$@"
>
> ?

Er, yeah, sorry. If you slip and press tab twice when you test it, it
still works :).

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: _initctl: only define helper functions once
  2011-05-26 16:49       ` Mikael Magnusson
@ 2011-05-26 19:37         ` Bernhard Tittelbach
  2011-05-26 19:42           ` Mikael Magnusson
  0 siblings, 1 reply; 9+ messages in thread
From: Bernhard Tittelbach @ 2011-05-26 19:37 UTC (permalink / raw)
  To: zsh-workers

nice trick !

:-)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: _initctl: only define helper functions once
  2011-05-26 19:37         ` Bernhard Tittelbach
@ 2011-05-26 19:42           ` Mikael Magnusson
  2011-05-26 20:31             ` Bernhard Tittelbach
  0 siblings, 1 reply; 9+ messages in thread
From: Mikael Magnusson @ 2011-05-26 19:42 UTC (permalink / raw)
  To: Bernhard Tittelbach; +Cc: zsh-workers

On 26 May 2011 21:37, Bernhard Tittelbach <xro@realraum.at> wrote:
> nice trick !
>
> :-)

Now that I look at it again, could that typeset -g be outside the
_initctl() function without the if, so it's just run once (when the
whole file is autoloaded)?

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: _initctl: only define helper functions once
  2011-05-26 19:42           ` Mikael Magnusson
@ 2011-05-26 20:31             ` Bernhard Tittelbach
  0 siblings, 0 replies; 9+ messages in thread
From: Bernhard Tittelbach @ 2011-05-26 20:31 UTC (permalink / raw)
  To: zsh-workers

On 2011-05-26 21:42, Mikael Magnusson wrote:
> On 26 May 2011 21:37, Bernhard Tittelbach <xro@realraum.at> wrote:
>> nice trick !
>>
>> :-)
>
> Now that I look at it again, could that typeset -g be outside the
> _initctl() function without the if, so it's just run once (when the
> whole file is autoloaded)?
>

yes, that definitely works :-)

new & complete file here: http://paste.grml.org/432/



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-05-26 20:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-25 17:17 zsh completion for upstart commands initctl start stop restart reload and status Bernhard Tittelbach
2011-05-25 20:50 ` Marc Weber
2011-05-26  9:36 ` Peter Stephenson
2011-05-26 16:05   ` PATCH: _initctl: only define helper functions once Mikael Magnusson
2011-05-26 16:11     ` Peter Stephenson
2011-05-26 16:49       ` Mikael Magnusson
2011-05-26 19:37         ` Bernhard Tittelbach
2011-05-26 19:42           ` Mikael Magnusson
2011-05-26 20:31             ` Bernhard Tittelbach

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).