From: dana <dana@dana.is>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] Completion: Add _crontab
Date: Wed, 18 Jul 2018 17:17:21 -0500 [thread overview]
Message-ID: <7D9389BF-A420-4B9F-9BCD-B7FD34B06FB4@dana.is> (raw)
This adds completion for cronie and Vixie-style implementations of crontab. I'm
not aware of any platform where `crontab --help` is risky, but if anyone knows
of one i can try to work around it.
PS: I always feel a bit uncertain about how far one should go to keep these
functions DRY. Like i've achieved that here and it works fine, but it certainly
feels very busy for a command that only has a handful of options. I wonder if
sometimes, in cases like this, it would be more maintainable if i just
duplicated the entire set of arg specs for each variant. Both methods have their
disadvantages i suppose.
PPS: Maybe one way to deal with the BusyBox multi-call issue (cf. workers/42212)
would be to (ab)use the precommands array that's currently used to handle stuff
like `command` and `nohup`. Needs more thought than i've given it though.
dana
diff --git a/Completion/Unix/Command/_crontab b/Completion/Unix/Command/_crontab
new file mode 100644
index 000000000..920e4af61
--- /dev/null
+++ b/Completion/Unix/Command/_crontab
@@ -0,0 +1,66 @@
+#compdef crontab
+
+# Notes:
+# - We assume a cronie or Vixie-esque crontab
+# - @todo As usual, BusyBox multi-call isn't handled
+
+local variant sluser
+local -a args etargs ccargs clargs rcargs aopts
+
+_pick_variant -r variant \
+ busybox=BusyBox \
+ cronie-selinux='(#i)selinux' \
+ cronie='(#i)cluster' \
+ unix --help
+variant+=-$OSTYPE
+
+# On Solaris, instead of using -u, the user can be specified as an optional
+# first operand with -e/-l/-r. We'll treat it as an optional *argument* to one
+# of those options, though, since the logic is a bit simpler
+if [[ $variant == *-solaris* ]]; then
+ sluser='::user whose crontab to work with:_users'
+else
+ etargs+=( '(cl)-u+[specify user whose crontab to work with]: :_users' )
+fi
+
+case $variant in
+ busybox-*)
+ etargs+=( '-c+[specify crontab directory]:crontab directory:_directories' )
+ ;;
+ cronie-selinux-*)
+ ccargs+=( '(-l cl nc rc)-s[append SELinux context (with -e)]' )
+ ;& # FALL THROUGH
+ cronie-*)
+ etargs+=( '(: * -)-V[display version information]' )
+ clargs+=(
+ '(: * -)-c[display cluster host]'
+ '(: * -)-n+[specify cluster host]: :_hosts'
+ )
+ ;& # FALL THROUGH
+ *-linux*)
+ rcargs+=( '(cc cl nc)-i[prompt for confirmation (with -r)]' )
+ ;;
+ *-freebsd*)
+ rcargs+=( '(cc cl nc)-f[bypass confirmation prompt (with -r)]' )
+ ;;
+esac
+
+(( $#etargs )) && args+=( + et $etargs ) # Misc.
+(( $#clargs )) && args+=( + cl $clargs ) # Work with cluster
+args+=(
+ + nc # Install new crontab
+ '(cc cl rc sl):crontab to install:_files'
+ + cc # Edit/display current crontab
+ "(-l cl nc rc)-e[edit current crontab]$sluser"
+ "(-e -s cl nc rc)-l[display current crontab]$sluser"
+ $ccargs
+ + rc # Remove current crontab
+ "(cc cl nc)-r[remove current crontab]$sluser"
+ $rcargs
+)
+
+# Implementations that use GNU's getopt(3) probably support permutation; this
+# should be accurate enough
+[[ $OSTYPE == linux* ]] || aopts=( -A '-*' )
+
+_arguments -s -S $aopts : $args
reply other threads:[~2018-07-18 22:17 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7D9389BF-A420-4B9F-9BCD-B7FD34B06FB4@dana.is \
--to=dana@dana.is \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).