2019-09-21 06:14:31 +00:00
|
|
|
if [[ "$ENABLE_CORRECTION" == "true" ]]; then
|
|
|
|
alias sudo='nocorrect noglob _do_sudo '
|
|
|
|
else
|
|
|
|
alias sudo='noglob _do_sudo '
|
|
|
|
fi
|
|
|
|
|
|
|
|
function _do_sudo() {
|
2019-09-21 11:05:22 +00:00
|
|
|
[[ -z ${__do_sudo_glob+x} ]] && __do_sudo_glob=1
|
|
|
|
[[ -z ${__do_sudo_expanded+x} ]] && declare -A __do_sudo_expanded
|
|
|
|
local -a args
|
|
|
|
local -a cmd_alias_arr
|
|
|
|
local cmd_alias
|
2019-09-21 06:14:31 +00:00
|
|
|
while (($#)); do
|
|
|
|
case "$1" in
|
|
|
|
command|exec|-) shift; break ;;
|
|
|
|
nocorrect) shift ;;
|
2019-09-21 11:05:22 +00:00
|
|
|
noglob) __do_sudo_glob=0; shift ;;
|
|
|
|
*)
|
|
|
|
cmd_alias="$(command -v 2>/dev/null -- "$1")"
|
|
|
|
if [[ "$?" -eq 0 ]]; then
|
|
|
|
if [[ "$cmd_alias" == 'alias'* ]] && [[ -z "$__do_sudo_expanded["$1"]" ]]; then
|
|
|
|
__do_sudo_expanded["$1"]=1
|
|
|
|
IFS=' ' read -A cmd_alias_arr <<< "$(sed -e "s/[^=]*=//" -e "s/^'//" -e "s/'$//" <<< "$cmd_alias")"
|
|
|
|
args+=( "${cmd_alias_arr[@]}" )
|
|
|
|
else
|
|
|
|
args+=( "$(sed "s/[^=]*=//" <<< "$(hash -v 2>/dev/null -- "$1")")" )
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
else
|
|
|
|
args+=( $1 )
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
;;
|
2019-09-21 06:14:31 +00:00
|
|
|
esac
|
|
|
|
done
|
2019-09-21 11:05:22 +00:00
|
|
|
if [[ ${#cmd_alias_arr[@]} -gt 0 ]]; then
|
|
|
|
_do_sudo "${args[@]}" $==*
|
2019-09-21 06:14:31 +00:00
|
|
|
else
|
2019-09-21 11:05:22 +00:00
|
|
|
if ((__do_sudo_glob)); then
|
|
|
|
PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH" command sudo "${args[@]}" $~==*
|
|
|
|
else
|
|
|
|
PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH" command sudo "${args[@]}" $==*
|
|
|
|
fi
|
|
|
|
unset __do_sudo_glob
|
|
|
|
unset __do_sudo_expanded
|
2019-09-21 06:14:31 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
command -v _sudo >/dev/null 2>&1
|
|
|
|
[[ $? -eq 0 ]] && compdef _sudo '_do_sudo'
|