mirror of
https://github.com/notwa/rc
synced 2025-02-05 07:43:22 -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
|
have() { if [ -z "$ZSH_VERSION" ]; then which -- "$1"; else whence -p -- "$1"; fi; } 2>/dev/null
|
||||||
has() { have "$@"; } >/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.
|
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; }
|
[ $# = 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.
|
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; }
|
{ 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.
|
nocom() ### @- strip single-line C-like and shell-like comments.
|
||||||
{ grep -Ev --line-buffered --color=never "^[[:space:]]*(//|#)" "$@"; }
|
{ 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.
|
### mimic certain features of `sudo` for systems without it installed.
|
||||||
### as it stands, this mostly just handles some environment variables.
|
### 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=
|
name=
|
||||||
env_cleanup=0
|
env_cleanup=0
|
||||||
|
|
||||||
while getopts :AEHKPSVbhiklnsvC:U:g:p:r:t:u: name; do
|
while getopts :AEHKPSVbhiklnsvC:U:g:p:r:t:u: name; do
|
||||||
case "$name" in
|
case "$name" in
|
||||||
K|V|k)
|
([KVk])
|
||||||
# K: sure kill
|
# K: sure kill
|
||||||
# V: version
|
# V: version
|
||||||
# k: kill
|
# k: kill
|
||||||
note 'maybesudo: your system does not have sudo installed!'
|
complain 'your system does not have sudo installed!'
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
h)
|
(h)
|
||||||
printf "%s\n" 'maybesudo - a dumb utility for systems without sudo'
|
usage
|
||||||
printf "\n"
|
|
||||||
printf "%s\n" 'usage: maybesudo -h | -V'
|
|
||||||
printf "%s\n" 'usage: maybesudo [command]'
|
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
A|E|H|P|S|n|p)
|
([AEHPSnp])
|
||||||
# A: askpass
|
# A: askpass
|
||||||
# E: preserve environment
|
# E: preserve environment
|
||||||
# H: HOME
|
# H: HOME
|
||||||
|
@ -36,10 +47,10 @@ __maybesudo() { ### @maybesudo
|
||||||
# S: stdin (password)
|
# S: stdin (password)
|
||||||
# n: non-interactive
|
# n: non-interactive
|
||||||
# p: prompt
|
# 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)
|
# C: close from (fd)
|
||||||
# U: other user (in conjunction with -l)
|
# U: other user (in conjunction with -l)
|
||||||
# b: background
|
# b: background
|
||||||
|
@ -49,24 +60,25 @@ __maybesudo() { ### @maybesudo
|
||||||
# r: role (SELinux)
|
# r: role (SELinux)
|
||||||
# s: shell (TODO)
|
# s: shell (TODO)
|
||||||
# t: type (SELinux)
|
# t: type (SELinux)
|
||||||
note "maybesudo: unsupported option --" "'$name'"
|
# v: validate
|
||||||
|
complain 'unsupported option --' "'$name'"
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
|
|
||||||
u) # user
|
(u) # user
|
||||||
if [ -z "$USER" ] || [ "$OPTARG" != "$USER" ]; then
|
if [ -z "$USER" ] || [ "$OPTARG" != "$USER" ]; then
|
||||||
note 'maybesudo: users other than yourself are unsupported!'
|
complain 'users other than yourself are unsupported!'
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
env_cleanup=1
|
env_cleanup=1
|
||||||
;;
|
;;
|
||||||
|
|
||||||
:)
|
(:)
|
||||||
note 'maybesudo: option requires an argument --' "'$OPTARG'"
|
complain 'option requires an argument --' "'$OPTARG'"
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
default)
|
(*)
|
||||||
note 'maybesudo: invalid option --' "'$OPTARG'"
|
complain 'invalid option --' "'$OPTARG'"
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
@ -74,6 +86,11 @@ __maybesudo() { ### @maybesudo
|
||||||
|
|
||||||
shift "$((OPTIND-1))"
|
shift "$((OPTIND-1))"
|
||||||
|
|
||||||
|
if [ $# = 0 ]; then
|
||||||
|
usage >&2
|
||||||
|
exit 64 # EX_USAGE
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "$env_cleanup" = 1 ]; then
|
if [ "$env_cleanup" = 1 ]; then
|
||||||
# portably listing exported variable names is virtually impossible
|
# portably listing exported variable names is virtually impossible
|
||||||
# without the blessing of null-terminated strings, so don't even try.
|
# without the blessing of null-terminated strings, so don't even try.
|
||||||
|
@ -107,10 +124,14 @@ __maybesudo() { ### @maybesudo
|
||||||
[ -z "$DISPLAY" ] || set -- DISPLAY="$DISPLAY" "$@"
|
[ -z "$DISPLAY" ] || set -- DISPLAY="$DISPLAY" "$@"
|
||||||
[ -z "$COLORS" ] || set -- COLORS="$COLORS" "$@"
|
[ -z "$COLORS" ] || set -- COLORS="$COLORS" "$@"
|
||||||
set -- -i "$@"
|
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
|
fi
|
||||||
|
|
||||||
env "$@"
|
exec env "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
maybesudo_()(__maybesudo "$@")
|
maybesudo_()(__maybesudo "$@") # deprecated
|
||||||
[ -n "${preload+-}" ] || __maybesudo "$@"
|
[ -n "${preload+-}" ] || __maybesudo "$@"
|
||||||
|
|
Loading…
Add table
Reference in a new issue