Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] Completion: Add _crontab



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



Messages sorted by: Reverse Date, Date, Thread, Author