1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 07:29:04 -08:00
rc/sh/stfu

100 lines
3.2 KiB
Text
Raw Normal View History

2021-07-29 00:37:35 -07:00
#!/usr/bin/env sh
# YES_ZSH
# YES_BASH
# YES_DASH
# YES_ASH
2021-07-29 00:37:35 -07:00
stfu() { ### @-
2021-08-01 09:27:25 -07:00
### invoke a command, silencing stdout and stderr *unless* the command fails.
###
### **NOTE:** don't use `stfu` for handling sensitive data or commands!
### use it for 7zip.
###
### note that the tail commands in the example below are from `stfu` itself;
### they are echoed to reveal the temp paths for any further investigation.
###
2021-08-01 09:27:25 -07:00
### ```
### $ touch butts
### $ STFU_TAIL=5
2021-08-01 09:27:25 -07:00
### $ stfu 7z a butts.7z butts
### $ stfu 7z a butts.7z asses
### command failed with exit status 1:
### 7z a butts.7z asses
###
### $ tail -n 5 /tmp/stfu.CoJ0vmJsqA/out_1627942118
2021-08-01 09:27:25 -07:00
### Scan WARNINGS for files and folders:
###
### asses : The system cannot find the file specified.
### ----------------
### Scan WARNINGS: 1
###
### $ tail -n 5 /tmp/stfu.CoJ0vmJsqA/err_1627942118
2021-08-01 09:27:25 -07:00
###
### WARNING: The system cannot find the file specified.
### asses
###
###
2021-08-01 09:27:25 -07:00
### ```
argc -ge 1 "$0" "$@" || return
local dirty=0 temp="$STFU_DIR"
if [ -z "$temp" ]; then
temp="$(mktemp -dt stfu.XXXXXXXXXX)"
[ $? -eq 0 ] || { printf "%s\n" "$0: failed create temporary directory" >&2; return 1; }
dirty=1
fi
# NOTE: this stat command will not work on BSD-likes,
# but it will work with GNU coreutils and busybox.
local perms="$(stat -c '%a' "$temp")"
local realtemp="$(readlink -f "$temp")"
[ -z "$MSYSTEM" ] || perms=700 # MSYS2 is insecure, oh well.
if [ -d "$temp" ] && [ "$realtemp" = "$temp" ] && [ "$perms" = 700 ]; then
if [ $dirty -ne 0 ]; then
export STFU_DIR="$temp"
fi
else
if [ -n "$STFU_DIR" ]; then
printf "%s\n" "$0: invalid temporary directory, please unset STFU_DIR" >&2
else
printf "%s\n" "$0: something went horribly wrong, maybe you can tell?" "$perms" "$realtemp" >&2
fi
return 1
fi
2021-07-29 00:37:35 -07:00
local time="$(date -u '+%s')"
[ $? -eq 0 ] || { printf "%s\n" "$0: failed to get current time" >&2; return 1; }
mkdir -p "$temp" || { printf "%s\n" "$0: failed to create temp directory" >&2; return 1; }
while [ -e "$temp/out_$time" -o -e "$temp/err_$time" ]; do time=$((time+1)); done
local out="$temp/out_$time"
local err="$temp/err_$time"
touch "$out" && touch "$err" || { printf "%s\n" "$0: failed to create temp files" >&2; return 1; }
2021-07-29 00:37:35 -07:00
#local out="$(mktemp -t -p "$temp" out_XXXXXX)"
#[ $? -eq 0 ] || { printf "%s\n" "$0: failed to create temp file" >&2; return 1; }
#local err="$(mktemp -t -p "$temp" err_XXXXXX)"
#[ $? -eq 0 ] || { printf "%s\n" "$0: failed to create temp file" >&2; return 1; }
local ret=0 tail="${STFU_TAIL:-20}"
2021-07-29 00:37:35 -07:00
"$@" > "$out" 2> "$err" || ret=$?
if [ $ret -ne 0 ]; then
printf "command failed with exit status %s:\n" $ret >&2
echo2 "$@"
echo2
note '$ tail -n' "$tail" "$out"
tail -n "$tail" "$out" >&2
2021-07-29 00:37:35 -07:00
echo2
note '$ tail -n' "$tail" "$err"
tail -n "$tail" "$err" >&2
2021-07-29 00:37:35 -07:00
fi
return $ret
}
[ -n "${preload+-}" ] || . ~/sh/preload || exit 2
eval ${preload:-preload} argc echo2 note
[ -n "${preload+-}" ] || stfu "$@"