Add completions for OpenRC rc-service

This patch adds completions for the rc-service command from OpenRC, as
suggested and provided by Mathieu Roy via a Debian bug [1], fixed and
extended for more options by myself.

When ran on a chroot with a freshly bootstrapped Debian system, the
following completions produce the following output.

For the options:

  # rc-service [TAB][TAB]
  -c              -h              --ifstopped     -q              savecache
  -C              --help          kmod            --quiet         udev
  cron            -i              -l              -r              -v
  -d              -I              --list          rc              -V
  -D              --ifcrashed     -N              rcS             --verbose
  --debug         --ifexists      networking      --resolve       --version
  --dry-run       --ifinactive    --nocolor       rsyslog         -Z
  -e              --ifnotstarted  --nodeps        -s
  --exists        --ifstarted     procps          -S
  # rc-service [CURSOR]

  # rc-service -h[TAB]
  # rc-service -h [CURSOR]

  # rc-service --v[TAB]
  # rc-service --ver[CURSOR]

  # rc-service --ver[TAB][TAB]
  --verbose  --version
  # rc-service --ver[CURSOR]

  # rc-service --verbose --l[TAB]
  # rc-service --verbose --list [CURSOR]

For the services:

  # rc-service --list[ENTER]
  cron
  hwclock.sh
  kmod
  networking
  procps
  rc
  rcS
  rsyslog
  savecache
  udev

  # rc-service r[TAB][TAB]
  rc       rcS      rsyslog
  # rc-service r[CURSOR]

  # rc-service --dry-run cr[TAB]
  # rc-service --dry-run cron [CURSOR]

  # rc-service --dry-run cron [TAB][TAB]
  force-reload  reload        restart       start         status        stop
  # rc-service --dry-run cron [CURSOR]

  # rc-service --dry-run cron re[TAB][TAB]
  reload   restart
  # rc-service --dry-run cron re[CURSOR]

  # rc-service --dry-run cron res[TAB]
  # rc-service --dry-run cron restart [CURSOR]

Adding `set show-all-if-ambiguous on' to .inputrc yields the same
results, but without the need for the additional [TAB] as displayed
above.

[1] https://bugs.debian.org/865548

Gbp-Pq: Name 11-add-completions-for-openrc-rc-service.patch
This commit is contained in:
Gabriel F. T. Gomes 2022-05-13 19:58:37 +08:00 committed by openKylinBot
parent 86e6286d5a
commit d7f299d813
1 changed files with 56 additions and 0 deletions

56
completions/rc-service Normal file
View File

@ -0,0 +1,56 @@
# rc-service(8) completion -*- shell-script -*-
#
# Adapted from update-rc.d
#
# Copyright (C) 2004 Servilio Afre Puentes <servilio@gmail.com>
# 2018 Mathieu Roy <yeupou@gnu.org>
# 2018 Gabriel F. T. Gomes <gabriel@inconstante.eti.br>
_rc_service()
{
local cur prev words cword
_init_completion || return
local sysvdir services options valid_options
sysvdir=/etc/init.d
services=( $( printf '%s ' $sysvdir/!(README*|*.sh|$_backup_glob) ) )
services=( ${services[@]#$sysvdir/} )
options=( -d --debug \
-D --nodeps \
-e --exists \
-c --ifcrashed \
-i --ifexists \
-I --ifinactive \
-N --ifnotstarted \
-s --ifstarted \
-S --ifstopped \
-l --list \
-r --resolve \
-Z --dry-run \
-h --help \
-C --nocolor \
-V --version \
-v --verbose \
-q --quiet \
)
if [[ ($cword -eq 1) || ("$prev" == -* ) ]]; then
COMPREPLY=( $( compgen -W '${options[@]} ${services[@]}' \
-- "$cur" ) )
elif [[ -x $sysvdir/$prev ]]; then
COMPREPLY=( $( compgen -W '`sed -e "y/|/ /" \
-ne "s/^.*Usage:[ ]*[^ ]*[ ]*{*\([^}\"]*\).*$/\1/p" \
$sysvdir/$prev`' \
-- "$cur" ) )
else
COMPREPLY=()
fi
return 0
} &&
complete -F _rc_service rc-service
# ex: filetype=sh