tmp: Add templates support, help text

This commit is contained in:
Salt 2019-01-23 14:49:16 -06:00
parent 022afcb4c0
commit db8ece927f

53
tmp
View File

@ -9,25 +9,62 @@
# Define variables # Define variables
name="$(basename "$0" .sh)" name="$(basename "$0" .sh)"
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
# Verify them # Verify them
[ -z ${tmpdir+x} ] && exit 2 [ -z "$tmpdir" ] && exit 2
# Define functions # Define functions
function log() { log() {
[ -z ${1+x} ] && return 1 [ -z "$1" ] && return 1
# shellcheck disable=1117 # shellcheck disable=1117
printf "%b: %s\n" \ printf "%b: %s\n" \
"$name" \ "$name" \
"$1" "$1"
} }
function cleanup() { cleanup() {
rm -rf "${tmpdir}" [ -z "$tmpdir" ] && exit 2
log "Cleaning up \"$tmpdir\""
rm -rf "$tmpdir"
} }
# Handle args
while getopts ":h" opt; do
case $opt in
h)
cat << EOF
Usage: $name [TEMPLATE]
Create a temporary directory, copying the folder [TEMPLATE] from
~/Templates/tmp, if it exists.
-h Show this help text
https://gitlab.com/rehashedsalt/bin
EOF
exit 0
;;
*)
log "Invaild argumenet: $OPTARG"
exit 1
;;
esac
done
# Copy in template directory
if [ -n "$1" ]; then
copydir="$HOME/Templates/tmp/$1"
if [ -d "$copydir" ]; then
log "Using template \"$1\""
cp -r "$copydir"/* "$tmpdir"
else
log "Could not find template directory \"$copydir\""
fi
fi
trap "cleanup" EXIT trap "cleanup" EXIT
# Do the do # Do the do
log "This folder will be removed when this shell exits" log "This directory will be removed when this shell exits"
( (
cd "${tmpdir}" || exit 50 cd "$tmpdir" || exit 50
exec "$SHELL" exec "$SHELL"
) )
cleanup