From db8ece927fee0a2e5e3984d9f6f17cf7edca9c46 Mon Sep 17 00:00:00 2001 From: Salt Date: Wed, 23 Jan 2019 14:49:16 -0600 Subject: [PATCH] tmp: Add templates support, help text --- tmp | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/tmp b/tmp index e7edb4e..78c4cd8 100755 --- a/tmp +++ b/tmp @@ -9,25 +9,62 @@ # Define variables name="$(basename "$0" .sh)" tmpdir="$(mktemp -d)" + # Verify them -[ -z ${tmpdir+x} ] && exit 2 +[ -z "$tmpdir" ] && exit 2 + # Define functions -function log() { - [ -z ${1+x} ] && return 1 +log() { + [ -z "$1" ] && return 1 # shellcheck disable=1117 printf "%b: %s\n" \ "$name" \ "$1" } -function cleanup() { - rm -rf "${tmpdir}" +cleanup() { + [ -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 # 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" ) -cleanup