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
1 changed files with 22 additions and 2 deletions

24
te
View File

@ -11,6 +11,7 @@ set -e
# Read-only set-once variables
declare -r _name="$(basename -- "$0")"
# Options
declare -i _optedit
declare -i _optforce
declare -i _opthelp
declare -i _optverbose
@ -140,6 +141,8 @@ printhelp() {
Usage: $_name [OPTION]... [FILENAME]...
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
-h Print this help text
-v Print more status messages. Stacks
@ -186,6 +189,13 @@ createtemplates() {
local perms="$(stat -c '%a' $_return)"
log "Applying perms: $perms" 2
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
case $_return in
fail)
@ -204,8 +214,11 @@ main() {
# Parse out arguments
while [ -n "$1" ]; do
# Parse out flags
while getopts ":fhv" opt; do
while getopts ":efhv" opt; do
case $opt in
e)
_optedit=1
;;
f)
_optforce+=1
;;
@ -233,10 +246,17 @@ main() {
done
# Early hook for help
[ -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
log "Validating dependencies" 2
if ! has basename chmod cp find readlink; then
error "Failed to find program: $_return" 1
error "Failed to find program: $_return" 2
fi
# Do the do