Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH] Completion: Add _crontab
- X-seq: zsh-workers 43186
- From: dana <dana@xxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: [PATCH] Completion: Add _crontab
- Date: Wed, 18 Jul 2018 17:17:21 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=FcpmZ+FZAKO9goM80kHYFjLNaztrLQdBKnnh6iohyOE=; b=BQxIWHDHu5zNRn07MFDRutwhvp71D+c87zrGamNXa6ajOKw8gLlVWvtr+z35YOllDD 56qkQrCUUa2F/FKIKtnRMRYzJJ1eEHBkQm8gr+6w3u2hFnvkEnMBSf7JZSPrAidE3wnf ci49qU1I1T5usTut8cHbB9CHbxmpUVuKLbj5FIjD4abrK++2UH2Irm+60GXF9XIrfEDA ALT0ZoRVmidFbKIxdIauNh4m+SMXazWBJ+7Ss1ZPAJwcqLkUCETWGawsGrmDxNk9FT6n Ijy7aHGFzaG0OFoICFaRzuywTJLZct7bgOR5hJVBiDFb3138Qb5OcuYx7reZD1FbxSsH natg==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
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