te: Add flag to open editor after creation

This commit is contained in:
Salt 2020-05-20 12:18:02 -05:00
parent 493b96f136
commit 8fdd604845

24
te
View File

@ -11,6 +11,7 @@ set -e
# Read-only set-once variables # Read-only set-once variables
declare -r _name="$(basename -- "$0")" declare -r _name="$(basename -- "$0")"
# Options # Options
declare -i _optedit
declare -i _optforce declare -i _optforce
declare -i _opthelp declare -i _opthelp
declare -i _optverbose declare -i _optverbose
@ -140,6 +141,8 @@ printhelp() {
Usage: $_name [OPTION]... [FILENAME]... Usage: $_name [OPTION]... [FILENAME]...
Template out a file based on its extension Template out a file based on its extension
-e Open \$EDITOR on file after creation. Only works with
exactly one argument.
-f Use force, clobber when necessary -f Use force, clobber when necessary
-h Print this help text -h Print this help text
-v Print more status messages. Stacks -v Print more status messages. Stacks
@ -186,6 +189,13 @@ createtemplates() {
local perms="$(stat -c '%a' $_return)" local perms="$(stat -c '%a' $_return)"
log "Applying perms: $perms" 2 log "Applying perms: $perms" 2
chmod "$perms" "$arg" chmod "$perms" "$arg"
# If we're supposed to open the editor, do it now
if (( _optedit > 0 )); then
if ! has "$EDITOR"; then
error "Could not find editor: $EDITOR" 3
fi
exec "$EDITOR" "$arg"
fi
else else
case $_return in case $_return in
fail) fail)
@ -204,8 +214,11 @@ main() {
# Parse out arguments # Parse out arguments
while [ -n "$1" ]; do while [ -n "$1" ]; do
# Parse out flags # Parse out flags
while getopts ":fhv" opt; do while getopts ":efhv" opt; do
case $opt in case $opt in
e)
_optedit=1
;;
f) f)
_optforce+=1 _optforce+=1
;; ;;
@ -233,10 +246,17 @@ main() {
done done
# Early hook for help # Early hook for help
[ -n "$_opthelp" ] && printhelp && exit 0 [ -n "$_opthelp" ] && printhelp && exit 0
# Validate critical options
if (( _optedit > 0 )) && (( ${#_args[@]} > 1 )); then
error "Option -e only works with one argument" 1
fi
if [ "${#_args[@]}" == "0" ]; then
error "Missing required operand, see \"$_name -h\"" 1
fi
# Validate core program dependencies # Validate core program dependencies
log "Validating dependencies" 2 log "Validating dependencies" 2
if ! has basename chmod cp find readlink; then if ! has basename chmod cp find readlink; then
error "Failed to find program: $_return" 1 error "Failed to find program: $_return" 2
fi fi
# Do the do # Do the do