[utility] Refactor rsync_scp wrapper function and add completion support

Changes:
- Rename `rsync_scp_wrap` to `noremoteglob` and make it more generally usable
- Enable completion support for commands wrapped with `noremoteglob`
- Tighten up internal variable usages
- Update documentation for 'noremoteglob' function
This commit is contained in:
Indrajit Raychaudhuri 2017-07-17 00:15:45 -05:00 committed by Indrajit Raychaudhuri
parent 17a59bada7
commit a60fe47359
3 changed files with 36 additions and 29 deletions

View file

@ -65,8 +65,8 @@ Aliases
- `history`
- `locate`
- `rake`
- `rsync`
- `scp`
- `rsync` (selectively enabled for local files)
- `scp` (selectively enabled for local files)
- `sftp`
### General
@ -149,6 +149,7 @@ Functions
- `mkdcd` makes a directory and changes to it.
- `popdls` pops an entry off the directory stack and lists its contents.
- `pushdls` pushes an entry onto the directory stack and lists its contents.
- `noremoteglob` enable local path globbing but disable remote path globbing.
### Developer

View file

@ -0,0 +1,11 @@
#compdef noremoteglob
#autoload
#
# Completes noremoteglob.
#
# Authors:
# Indrajit Raychaudhuri <irc+code@indrajit.com>
#
_precommand

View file

@ -41,33 +41,8 @@ alias ftp='noglob ftp'
alias history='noglob history'
alias locate='noglob locate'
alias rake='noglob rake'
alias rsync='noglob rsync_wrap'
alias scp='noglob scp_wrap'
# This function wraps rsync and scp so that remote paths are not globbed
# but local paths are globbed. This is because the programs have their own
# globbing for remote paths. The wrap function globs args starting in / and ./
# and doesn't glob paths with : in it as these are interpreted as remote paths
# by these programs unless the path starts with / or ./
function rsync_scp_wrap {
local args=( )
local cmd="$1"
shift
local i
for i in "$@"; do case $i in
( ./* ) args+=( ${~i} ) ;; # glob
( /* ) args+=( ${~i} ) ;; # glob
( *:* ) args+=( ${i} ) ;; # noglob
( * ) args+=( ${~i} ) ;; # glob
esac; done
command $cmd "${(@)args}"
}
function rsync_wrap {
rsync_scp_wrap "rsync" "$@"
}
function scp_wrap {
rsync_scp_wrap "scp" "$@"
}
alias rsync='noglob noremoteglob rsync'
alias scp='noglob noremoteglob scp'
alias sftp='noglob sftp'
# Define general aliases.
@ -239,3 +214,23 @@ function find-exec {
function psu {
ps -U "${1:-$LOGNAME}" -o 'pid,%cpu,%mem,command' "${(@)argv[2,-1]}"
}
# Enables globbing selectively on path arguments.
# Globbing is enabled on local paths (starting in '/' and './') and disabled
# on remote paths (containing ':' but not starting in '/' and './'). This is
# useful for programs that have their own globbing for remote paths.
# Currently, this is used by default for 'rsync' and 'scp'.
# Example:
# - Local: '*.txt', './foo:2017*.txt', '/var/*:log.txt'
# - Remote: user@localhost:foo/
function noremoteglob {
local -a argo
local cmd="$1"
for arg in ${argv:2}; do case $arg in
( ./* ) argo+=( ${~arg} ) ;; # local relative, glob
( /* ) argo+=( ${~arg} ) ;; # local absolute, glob
( *:* ) argo+=( ${arg} ) ;; # remote, noglob
( * ) argo+=( ${~arg} ) ;; # default, glob
esac; done
command $cmd "${(@)argo}"
}