mirror of
https://github.com/notwa/rc
synced 2024-11-09 15:39:03 -08:00
do a pass over maybesudo
This commit is contained in:
parent
7a3dc6f821
commit
e145f53955
2 changed files with 57 additions and 21 deletions
17
home/shrc
17
home/shrc
|
@ -21,7 +21,21 @@ unset cache
|
|||
have() { if [ -z "$ZSH_VERSION" ]; then which -- "$1"; else whence -p -- "$1"; fi; } 2>/dev/null
|
||||
has() { have "$@"; } >/dev/null
|
||||
|
||||
has sudo && maybesudo() { sudo "$@"; } || maybesudo() { maybesudo_ "$@"; }
|
||||
if has sudo; then
|
||||
doas() {
|
||||
printf '\033[7m %s \033[m\n' 'warning: you ran sudo when you meant doas!'
|
||||
sudo "$@"
|
||||
}
|
||||
maybesudo() { sudo "$@"; }
|
||||
elif has doas; then
|
||||
sudo() {
|
||||
printf '\033[7m %s \033[m\n' 'warning: you ran doas when you meant sudo!'
|
||||
doas "$@"
|
||||
}
|
||||
maybesudo() { doas "$@"; }
|
||||
else
|
||||
maybesudo() (__maybesudo "$@")
|
||||
fi
|
||||
|
||||
ADDPATH() { ### @- append a directory to `$PATH` if it isn't already present.
|
||||
[ $# = 1 ] || { printf >&2 'ADDPATH: expected exactly 1 argument, got %s\n' $#; return 64; }
|
||||
|
@ -183,6 +197,7 @@ counts() ### @- count files in the current directory, including files found recu
|
|||
exts() ### @- count and sort file extensions in the current directory, including files found recursively.
|
||||
{ find -type f | sed -e 's@.*/@@' -e 's@.*\.@@' | tr A-Z a-z | scount; }
|
||||
|
||||
# TODO: don't use GNU options when busybox is detected!
|
||||
nocom() ### @- strip single-line C-like and shell-like comments.
|
||||
{ grep -Ev --line-buffered --color=never "^[[:space:]]*(//|#)" "$@"; }
|
||||
|
||||
|
|
61
sh/maybesudo
61
sh/maybesudo
|
@ -5,30 +5,41 @@ __maybesudo() { ### @maybesudo
|
|||
### mimic certain features of `sudo` for systems without it installed.
|
||||
### as it stands, this mostly just handles some environment variables.
|
||||
###
|
||||
### try this: `maybesudo_ -u "$USER" printenv`
|
||||
### try this: `maybesudo -u "$USER" printenv`
|
||||
|
||||
complain() {
|
||||
printf >&2 'maybesudo: %s\n' "$*"
|
||||
}
|
||||
|
||||
usage() {
|
||||
printf %s\\n \
|
||||
'maybesudo - a dumb utility for systems without sudo' \
|
||||
'' \
|
||||
'usage: maybesudo -h | -V' \
|
||||
'usage: maybesudo [-u user] command [arg ...]' \
|
||||
'usage: maybesudo [-u user] file ...' \
|
||||
;
|
||||
}
|
||||
|
||||
name=
|
||||
env_cleanup=0
|
||||
|
||||
while getopts :AEHKPSVbhiklnsvC:U:g:p:r:t:u: name; do
|
||||
case "$name" in
|
||||
K|V|k)
|
||||
([KVk])
|
||||
# K: sure kill
|
||||
# V: version
|
||||
# k: kill
|
||||
note 'maybesudo: your system does not have sudo installed!'
|
||||
complain 'your system does not have sudo installed!'
|
||||
return 0
|
||||
;;
|
||||
|
||||
h)
|
||||
printf "%s\n" 'maybesudo - a dumb utility for systems without sudo'
|
||||
printf "\n"
|
||||
printf "%s\n" 'usage: maybesudo -h | -V'
|
||||
printf "%s\n" 'usage: maybesudo [command]'
|
||||
(h)
|
||||
usage
|
||||
return 0
|
||||
;;
|
||||
|
||||
A|E|H|P|S|n|p)
|
||||
([AEHPSnp])
|
||||
# A: askpass
|
||||
# E: preserve environment
|
||||
# H: HOME
|
||||
|
@ -36,10 +47,10 @@ __maybesudo() { ### @maybesudo
|
|||
# S: stdin (password)
|
||||
# n: non-interactive
|
||||
# p: prompt
|
||||
note 'maybesudo: option has no effect --' "'$name'"
|
||||
complain 'option has no effect --' "'$name'"
|
||||
;;
|
||||
|
||||
C|U|b|g|i|l|r|s|t)
|
||||
([CUbgilrstv])
|
||||
# C: close from (fd)
|
||||
# U: other user (in conjunction with -l)
|
||||
# b: background
|
||||
|
@ -49,24 +60,25 @@ __maybesudo() { ### @maybesudo
|
|||
# r: role (SELinux)
|
||||
# s: shell (TODO)
|
||||
# t: type (SELinux)
|
||||
note "maybesudo: unsupported option --" "'$name'"
|
||||
# v: validate
|
||||
complain 'unsupported option --' "'$name'"
|
||||
return 1
|
||||
;;
|
||||
|
||||
u) # user
|
||||
(u) # user
|
||||
if [ -z "$USER" ] || [ "$OPTARG" != "$USER" ]; then
|
||||
note 'maybesudo: users other than yourself are unsupported!'
|
||||
complain 'users other than yourself are unsupported!'
|
||||
return 1
|
||||
fi
|
||||
env_cleanup=1
|
||||
;;
|
||||
|
||||
:)
|
||||
note 'maybesudo: option requires an argument --' "'$OPTARG'"
|
||||
(:)
|
||||
complain 'option requires an argument --' "'$OPTARG'"
|
||||
return 1
|
||||
;;
|
||||
default)
|
||||
note 'maybesudo: invalid option --' "'$OPTARG'"
|
||||
(*)
|
||||
complain 'invalid option --' "'$OPTARG'"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
@ -74,6 +86,11 @@ __maybesudo() { ### @maybesudo
|
|||
|
||||
shift "$((OPTIND-1))"
|
||||
|
||||
if [ $# = 0 ]; then
|
||||
usage >&2
|
||||
exit 64 # EX_USAGE
|
||||
fi
|
||||
|
||||
if [ "$env_cleanup" = 1 ]; then
|
||||
# portably listing exported variable names is virtually impossible
|
||||
# without the blessing of null-terminated strings, so don't even try.
|
||||
|
@ -107,10 +124,14 @@ __maybesudo() { ### @maybesudo
|
|||
[ -z "$DISPLAY" ] || set -- DISPLAY="$DISPLAY" "$@"
|
||||
[ -z "$COLORS" ] || set -- COLORS="$COLORS" "$@"
|
||||
set -- -i "$@"
|
||||
|
||||
elif ! [ -d /C ]; then # no such thing as sudo on Windows (kinda)
|
||||
complain 'you requested root permissions, but you do not have sudo installed.'
|
||||
return 69 # EX_UNAVAILABLE
|
||||
fi
|
||||
|
||||
env "$@"
|
||||
exec env "$@"
|
||||
}
|
||||
|
||||
maybesudo_()(__maybesudo "$@")
|
||||
maybesudo_()(__maybesudo "$@") # deprecated
|
||||
[ -n "${preload+-}" ] || __maybesudo "$@"
|
||||
|
|
Loading…
Reference in a new issue