refactor: improve templater with new functionality
This commit is contained in:
parent
d17ccfc049
commit
1bc1e3d3ed
@ -1,24 +1,132 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# "Import" our needed functions from our library
|
echo_rgb() {
|
||||||
LIBRARY_PATH="/usr/local/lib/Custom-Scripts"
|
# Echo a colored string to the terminal based on rgb values
|
||||||
# shellcheck source=/usr/local/lib/
|
#
|
||||||
if ! source "${LIBRARY_PATH}/Logging-RGB.bash"; then
|
# Positional Arguments:
|
||||||
echo "Unable to source Logging-RGB.bash at ${LIBRARY_PATH}"
|
#
|
||||||
exit 1
|
# message <type: string> <position: 1> <required: true>
|
||||||
fi
|
# - The message to be printed to stdout
|
||||||
# shellcheck source=/usr/local/lib/
|
# red <type: int> <position: 2> <required: true>
|
||||||
if ! source "${LIBRARY_PATH}/Trim.bash"; then
|
# - The red value from 0 to 255
|
||||||
log "error" "Unable to source Trim.bash at ${LIBRARY_PATH}"
|
# green <type: int> <position: 3> <required: true>
|
||||||
exit 1
|
# - The green value from 0 to 255
|
||||||
fi
|
# blue <type: int> <position: 4> <required: true>
|
||||||
|
# - The blue value from 0 to 255
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# echo_rgb "Yep" 10 8 30
|
||||||
|
#
|
||||||
|
# POSIX Compliant:
|
||||||
|
# N/A
|
||||||
|
#
|
||||||
|
|
||||||
# Tmp file used because most distros have this dir exist in RAM, use PID of our process to write to
|
local red
|
||||||
TEMP_FILE="/tmp/$$"
|
local green
|
||||||
|
local blue
|
||||||
|
local input
|
||||||
|
|
||||||
|
input="${1}"
|
||||||
|
red="${2}"
|
||||||
|
green="${3}"
|
||||||
|
blue="${4}"
|
||||||
|
|
||||||
|
if [ -t 1 ]; then
|
||||||
|
printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}"
|
||||||
|
else
|
||||||
|
printf "%s\n" "${input}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
log() {
|
||||||
|
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# level <type: string> <position: 1> <required: true>
|
||||||
|
# - The log level, defined within a case check in this function
|
||||||
|
# message <type: string> <position: 2> <required: true>
|
||||||
|
# - The info message
|
||||||
|
# line_number <type: int> <position: 3> <required: false>
|
||||||
|
# - The line number of the calling function (${LINNO})
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# log "info" "Could not find that directory"
|
||||||
|
#
|
||||||
|
# POSIX Compliant:
|
||||||
|
# Yes
|
||||||
|
#
|
||||||
|
|
||||||
|
# Set debug status depending if a global debug variable has been set to either 1 or 0
|
||||||
|
local debug
|
||||||
|
if [ ${DEBUG} ]; then
|
||||||
|
debug=${DEBUG}
|
||||||
|
else
|
||||||
|
debug=0
|
||||||
|
fi
|
||||||
|
|
||||||
|
local FORMAT
|
||||||
|
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
|
||||||
|
|
||||||
|
# Convert the level to uppercase
|
||||||
|
local level
|
||||||
|
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
|
||||||
|
|
||||||
|
local message
|
||||||
|
message="${2}"
|
||||||
|
|
||||||
|
case "${level}" in
|
||||||
|
INFO)
|
||||||
|
# Output all info log levels to stdout
|
||||||
|
printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
WARN | WARNING)
|
||||||
|
# Output all warning log levels to stdout
|
||||||
|
printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
DEBUG)
|
||||||
|
# Output all debug log levels to stdout
|
||||||
|
if [ "${DEBUG}" ]; then
|
||||||
|
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
ERROR)
|
||||||
|
# Output all error log levels to stderr
|
||||||
|
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
# Further log levels can be added by extending this switch statement with more comparisons
|
||||||
|
|
||||||
|
*) # Default case, no matches
|
||||||
|
# Returns non-zero code as an improper log option was passed, this helps with using `set -e`
|
||||||
|
printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
trim() {
|
||||||
|
local input="${1}"
|
||||||
|
|
||||||
|
while [[ "${input}" == " "* ]]; do
|
||||||
|
input="${input## }"
|
||||||
|
done
|
||||||
|
|
||||||
|
while [[ "${input}" == *" " ]]; do
|
||||||
|
input="${input%% }"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "${input}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get a temp working file so we don't pollute directories
|
||||||
|
TEMP_FILE="$(mktemp)"
|
||||||
|
|
||||||
# Set the Contents directory to the env variable, script set, or to their .contents file in the user home
|
# Set the Contents directory to the env variable, script set, or to their .contents file in the user home
|
||||||
CONTENTS_DIRECTORY="${CONTENTS_DIRECTORY:=\
|
CONTENTS_DIRECTORY="${CONTENTS_DIRECTORY:=\
|
||||||
$(mkdir -p "${HOME}/.contents" && echo "${HOME}/.contents")}"
|
$(mkdir -p "${HOME}/.contents" && echo "${HOME}/.contents")}"
|
||||||
|
|
||||||
EXIT_ON_ERROR=0
|
EXIT_ON_ERROR=0
|
||||||
|
|
||||||
@ -29,342 +137,358 @@ WRITE_TEMPLATE_TO=""
|
|||||||
declare -A template_handlers
|
declare -A template_handlers
|
||||||
|
|
||||||
template_handlers=(
|
template_handlers=(
|
||||||
["FILE"]="file_handler"
|
["FILE"]="file_handler"
|
||||||
["WRITE-TO"]="write_to_handler"
|
["WRITE-TO"]="write_to_handler"
|
||||||
["TEMPLATE"]="template_handler"
|
["TEMPLATE"]="template_handler"
|
||||||
|
["VAR"]="var_handler"
|
||||||
|
["EVAL"]="eval_handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
write_content() {
|
write_content() {
|
||||||
local content
|
local content
|
||||||
local leading_spaces
|
local leading_spaces
|
||||||
content="${1}"
|
content="${1}"
|
||||||
leading_spaces="${2}"
|
leading_spaces="${2}"
|
||||||
|
|
||||||
local string_builder
|
local string_builder
|
||||||
string_builder=""
|
string_builder=""
|
||||||
|
|
||||||
for ((i=1; i<=leading_spaces; i++)); do
|
for ((i = 1; i <= leading_spaces; i++)); do
|
||||||
string_builder="${string_builder} "
|
string_builder="${string_builder} "
|
||||||
done
|
done
|
||||||
string_builder="${string_builder}${content}"
|
string_builder="${string_builder}${content}"
|
||||||
_write_to_buffer "${string_builder}"
|
_write_to_buffer "${string_builder}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_write_to_buffer() {
|
_write_to_buffer() {
|
||||||
log "debug" "Received content to write to buffer:
|
log "debug" "Received content to write to buffer:
|
||||||
${1}"
|
${1}"
|
||||||
printf "%s\n" "${1}" >> "${TEMP_FILE}"
|
printf "%s\n" "${1}" >>"${TEMP_FILE}"
|
||||||
log "debug" "Finished writing content to buffer"
|
log "debug" "Finished writing content to buffer"
|
||||||
|
}
|
||||||
|
|
||||||
|
var_handler() {
|
||||||
|
local variable="${1}"
|
||||||
|
local leading_spaces="${2}"
|
||||||
|
while IFS="\n" read -r line; do
|
||||||
|
write_content "${line}" "${leading_spaces}"
|
||||||
|
done <<<"${variable}"
|
||||||
|
}
|
||||||
|
|
||||||
|
eval_handler() {
|
||||||
|
local eval_statement="${1}"
|
||||||
|
local leading_spaces="${2}"
|
||||||
|
while IFS="\n" read -r line; do
|
||||||
|
write_content "${line}" "${leading_spaces}"
|
||||||
|
done <<<"$(eval "${eval_statement}")"
|
||||||
}
|
}
|
||||||
|
|
||||||
template_handler() {
|
template_handler() {
|
||||||
local template_file
|
local template_file
|
||||||
template_file="${1}"
|
template_file="${1}"
|
||||||
|
|
||||||
local leading_spaces
|
local leading_spaces
|
||||||
leading_spaces="${2}"
|
leading_spaces="${2}"
|
||||||
# Check that the given file is not an absolute path
|
# Check that the given file is not an absolute path
|
||||||
if [[ ! "${template_file:0:1}" == "/" ]]; then
|
if [[ ! "${template_file:0:1}" == "/" ]]; then
|
||||||
template_file="${CONTENTS_DIRECTORY}/${template_file}"
|
template_file="${CONTENTS_DIRECTORY}/${template_file}"
|
||||||
fi
|
fi
|
||||||
if [[ ! -f "${template_file}" ]]; then
|
if [[ ! -f "${template_file}" ]]; then
|
||||||
log "error" "The given template file \"${template_file}\" does not exist"
|
log "error" "The given template file \"${template_file}\" does not exist"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
file_handler "${template_file}" "${leading_spaces}"
|
file_handler "${template_file}" "${leading_spaces}"
|
||||||
REPEAT=0
|
REPEAT=0
|
||||||
}
|
}
|
||||||
|
|
||||||
file_handler() {
|
file_handler() {
|
||||||
local file
|
local file
|
||||||
file="${1}"
|
file="${1}"
|
||||||
|
|
||||||
local leading_spaces
|
local leading_spaces
|
||||||
leading_spaces="${2}"
|
leading_spaces="${2}"
|
||||||
|
|
||||||
# Check that the given file is not an absolute path
|
# Check that the given file is not an absolute path
|
||||||
if [[ ! "${file:0:1}" == "/" ]] && [[ ! -f "${file}" ]]; then
|
if [[ ! "${file:0:1}" == "/" ]] && [[ ! -f "${file}" ]]; then
|
||||||
file="${CONTENTS_DIRECTORY}/${file}"
|
file="${CONTENTS_DIRECTORY}/${file}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [[ ! -f "${file}" ]]; then
|
||||||
|
log "error" "The given file \"${file}\" does not exist"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ ! -f "${file}" ]]; then
|
while IFS="\n" read -r line; do
|
||||||
log "error" "The given file \"${file}\" does not exist"
|
write_content "${line}" "${leading_spaces}"
|
||||||
return 1
|
done <"${file}"
|
||||||
fi
|
|
||||||
|
|
||||||
while IFS="\n" read -r line; do
|
|
||||||
write_content "${line}" "${leading_spaces}"
|
|
||||||
done < "${file}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
write_to_handler() {
|
write_to_handler() {
|
||||||
local write_path
|
local write_path
|
||||||
write_path=${1}
|
write_path=${1}
|
||||||
|
|
||||||
log "debug" "Received write path \"${write_path}\""
|
log "debug" "Received write path \"${write_path}\""
|
||||||
if [[ ! "${write_path:0:1}" == "/" ]]; then
|
if [[ ! "${write_path:0:1}" == "/" ]]; then
|
||||||
log "error" "Given write path was invalid, received \"${write_path}\", the path must be an absolute path"
|
log "error" "Given write path was invalid, received \"${write_path}\", the path must be an absolute path"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -d "${write_path}" ]]; then
|
if [[ -d "${write_path}" ]]; then
|
||||||
log "error" "Given write path was invalid, received \"${write_path}\", the path must be a path to an output file (doesn't have to exist), not a directory"
|
log "error" "Given write path was invalid, received \"${write_path}\", the path must be a path to an output file (doesn't have to exist), not a directory"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
# This means that it was NOT manually set, 0 is true in shell
|
# This means that it was NOT manually set, 0 is true in shell
|
||||||
if (("${WRITE_TEMPLATE_MANUALLY_SET}" == 1)); then
|
if (("${WRITE_TEMPLATE_MANUALLY_SET}" == 1)); then
|
||||||
WRITE_TEMPLATE_TO=${write_path}
|
WRITE_TEMPLATE_TO=${write_path}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_template_extracted() {
|
parse_template_extracted() {
|
||||||
local tmpl_extracted
|
local tmpl_extracted
|
||||||
tmpl_extracted="${1}"
|
tmpl_extracted="${1}"
|
||||||
|
|
||||||
local full_line
|
local full_line
|
||||||
full_line="${2}"
|
full_line="${2}"
|
||||||
|
|
||||||
local num_leading_spaces
|
local num_leading_spaces
|
||||||
num_leading_spaces=$(awk -F"[ ]" '{for(i=1;i<=NF && ($i=="");i++);print ""i-1""}' <<< "${full_line}")
|
num_leading_spaces=$(awk -F"[ ]" '{for(i=1;i<=NF && ($i=="");i++);print ""i-1""}' <<<"${full_line}")
|
||||||
|
|
||||||
# Basic checks to ensure template is correct, first not empty, second that an '=' was passed to give type specifier
|
# Basic checks to ensure template is correct, first not empty, second that an '=' was passed to give type specifier
|
||||||
# third is to ensure only one equals sign is passed
|
# third is to ensure only one equals sign is passed
|
||||||
if [[ -z "${tmpl_extracted}" ]]; then
|
if [[ -z "${tmpl_extracted}" ]]; then
|
||||||
log "error" "Template passed was empty... ignoring"
|
log "error" "Template passed was empty... ignoring"
|
||||||
return 1
|
return 1
|
||||||
elif [[ ! "${tmpl_extracted}" = *"="* ]]; then
|
elif [[ ! "${tmpl_extracted}" = *"="* ]]; then
|
||||||
log "error" "Template line passed was of an invalid format, missing = specifier, assumed type is missing as well"
|
log "error" "Template line passed was of an invalid format, missing = specifier, assumed type is missing as well"
|
||||||
return 1
|
return 1
|
||||||
elif [ "$(echo "${tmpl_extracted}" | grep -o "=" | wc -l)" -gt 1 ]; then
|
elif [ "$(echo "${tmpl_extracted}" | grep -o "=" | wc -l)" -gt 1 ]; then
|
||||||
log "error" "Template line passed had too many \"=\", there should only be 1"
|
log "error" "Template line passed had too many \"=\", there should only be 1"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local var_type
|
local var_type
|
||||||
var_type="$(trim "$(echo "${tmpl_extracted}" | cut -d "=" -f1)")"
|
var_type="$(trim "$(echo "${tmpl_extracted}" | cut -d "=" -f1)")"
|
||||||
|
|
||||||
# Shellcheck incorrectly reads the below line
|
# Shellcheck incorrectly reads the below line
|
||||||
# shellcheck disable=SC2116
|
# shellcheck disable=SC2116
|
||||||
var_type="$(echo "${var_type^^}")"
|
var_type="$(echo "${var_type^^}")"
|
||||||
|
|
||||||
local var_arg
|
local var_arg
|
||||||
var_arg="$(trim "$(echo "${tmpl_extracted}" | cut -d "=" -f2)")"
|
var_arg="$(trim "$(echo "${tmpl_extracted}" | cut -d "=" -f2)")"
|
||||||
|
|
||||||
local handler_to_use
|
local handler_to_use
|
||||||
handler_to_use="${template_handlers[${var_type}]}"
|
handler_to_use="${template_handlers[${var_type}]}"
|
||||||
|
|
||||||
if [[ -z "${handler_to_use}" ]]; then
|
if [[ -z "${handler_to_use}" ]]; then
|
||||||
log "error" "Invalid handler found, no such handler \"${var_type}\" exists as a handler"
|
log "error" "Invalid handler found, no such handler \"${var_type}\" exists as a handler"
|
||||||
return 1
|
return 1
|
||||||
else
|
else
|
||||||
log "debug" "Using handler \"${handler_to_use}\""
|
log "debug" "Using handler \"${handler_to_use}\""
|
||||||
if ! eval "${handler_to_use}" "${var_arg}" "${num_leading_spaces}"; then
|
if ! eval "${handler_to_use}" "${var_arg}" "${num_leading_spaces}"; then
|
||||||
log "error" "Arguments passed for \"${var_type}\" were invalid, check the argument"
|
log "error" "Arguments passed for \"${var_type}\" were invalid, check the argument"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
log "debug" "Finished using handler \"${handler_to_use}\""
|
log "debug" "Finished using handler \"${handler_to_use}\""
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
output_finished_template() {
|
output_finished_template() {
|
||||||
log "debug" "Outputting finished template"
|
log "debug" "Outputting finished template"
|
||||||
mkdir -p "$(dirname "${WRITE_TEMPLATE_TO}")"
|
mkdir -p "$(dirname "${WRITE_TEMPLATE_TO}")"
|
||||||
# We want globbing here
|
# We want globbing here
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
if [[ -z "${WRITE_TEMPLATE_TO}" ]]; then
|
if [[ -z "${WRITE_TEMPLATE_TO}" ]]; then
|
||||||
log "error" "Was never given a path to write the template to, check your template file or explicitly specify an output location"
|
log "error" "Was never given a path to write the template to, check your template file or explicitly specify an output location"
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
mv "${TEMP_FILE}" ${WRITE_TEMPLATE_TO}
|
mv "${TEMP_FILE}" ${WRITE_TEMPLATE_TO}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_template_line() {
|
parse_template_line() {
|
||||||
local line
|
local line
|
||||||
line="${1}"
|
line="${1}"
|
||||||
|
|
||||||
local tmpl_extracted
|
local tmpl_extracted
|
||||||
if [[ "${line}" = *\{#%*%#\}* ]]; then
|
if [[ "${line}" = *\{#%*%#\}* ]]; then
|
||||||
tmpl_extracted="${line#*\{#%}"
|
tmpl_extracted="${line#*\{#%}"
|
||||||
tmpl_extracted="${tmpl_extracted%\%#\}*}"
|
tmpl_extracted="${tmpl_extracted%\%#\}*}"
|
||||||
tmpl_extracted="$(trim "${tmpl_extracted}")"
|
tmpl_extracted="$(trim "${tmpl_extracted}")"
|
||||||
|
|
||||||
log "debug" "Extracted a template: ${tmpl_extracted}"
|
log "debug" "Extracted a template: ${tmpl_extracted}"
|
||||||
# Check for non zero status codes and parse the extracted template
|
# Check for non zero status codes and parse the extracted template
|
||||||
if ! parse_template_extracted "${tmpl_extracted}" "${line}"; then
|
if ! parse_template_extracted "${tmpl_extracted}" "${line}"; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
log "debug" "Finished extracting template: ${tmpl_extracted}"
|
log "debug" "Finished extracting template: ${tmpl_extracted}"
|
||||||
else
|
else
|
||||||
write_content "${line}" "0"
|
write_content "${line}" "0"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
read_template() {
|
read_template() {
|
||||||
local read_file
|
local read_file
|
||||||
local line
|
local line
|
||||||
local line_num
|
local line_num
|
||||||
line_num=1
|
line_num=1
|
||||||
read_file="${1}"
|
read_file="${1}"
|
||||||
log "info" "Parsing template file \"${read_file}\""
|
log "info" "Parsing template file \"${read_file}\""
|
||||||
while IFS="\n" read -r line; do
|
while IFS="\n" read -r line; do
|
||||||
# Check for non zero status code
|
# Check for non zero status code
|
||||||
if ! parse_template_line "${line}"; then
|
if ! parse_template_line "${line}"; then
|
||||||
log "error" "Invalid template passed from \"${read_file}\", check line ${line_num}"
|
log "error" "Invalid template passed from \"${read_file}\", check line ${line_num}"
|
||||||
if (( "${EXIT_ON_ERROR}" == 0 )); then
|
if (("${EXIT_ON_ERROR}" == 0)); then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
line_num=$((line_num + 1))
|
line_num=$((line_num + 1))
|
||||||
done <"${read_file}"
|
done <"${read_file}"
|
||||||
}
|
}
|
||||||
|
|
||||||
templater() {
|
templater() {
|
||||||
local read_file
|
local read_file
|
||||||
read_file="${1}"
|
read_file="${1}"
|
||||||
[[ ! -f "${read_file}" ]] &&
|
[[ ! -f "${read_file}" ]] &&
|
||||||
log "error" "$(important "${read_file}") does not exist!" &&
|
log "error" "$(important "${read_file}") does not exist!" &&
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
read_template "${read_file}"
|
read_template "${read_file}"
|
||||||
|
|
||||||
if (( "${REPEAT}" == 0 )); then
|
if (("${REPEAT}" == 0)); then
|
||||||
REPEAT=1
|
REPEAT=1
|
||||||
log "debug" "Repeat set to 0, repeating on the finished file located at ${TEMP_FILE}"
|
log "debug" "Repeat set to 0, repeating on the finished file located at ${TEMP_FILE}"
|
||||||
mv "${TEMP_FILE}" "${TEMP_FILE}-repeat"
|
mv "${TEMP_FILE}" "${TEMP_FILE}-repeat"
|
||||||
main "-t" "${TEMP_FILE}-repeat"
|
main "-t" "${TEMP_FILE}-repeat"
|
||||||
else
|
else
|
||||||
log "info" "Finished parsing template file \"${read_file}\""
|
log "info" "Finished parsing template file \"${read_file}\""
|
||||||
log "info" "Writing finished template..."
|
log "info" "Writing finished template..."
|
||||||
output_finished_template
|
output_finished_template
|
||||||
log "info" "Finished writing template for \"${read_file}\" to \"${WRITE_TEMPLATE_TO}\""
|
log "info" "Finished writing template for \"${read_file}\" to \"${WRITE_TEMPLATE_TO}\""
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
arg_required() {
|
arg_required() {
|
||||||
echo_rgb "${1}" 255 183 0
|
echo_rgb "${1}" 255 183 0
|
||||||
}
|
}
|
||||||
|
|
||||||
arg_optional(){
|
arg_optional() {
|
||||||
echo_rgb "${1}" 117 255 255
|
echo_rgb "${1}" 117 255 255
|
||||||
}
|
}
|
||||||
|
|
||||||
arg_description(){
|
arg_description() {
|
||||||
echo_rgb "${@}" 220 190 255
|
echo_rgb "${@}" 220 190 255
|
||||||
}
|
}
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
# Print out usage instructions for the local script
|
# Print out usage instructions for the local script
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# None
|
# None
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# usage
|
# usage
|
||||||
#
|
#
|
||||||
# POSIX Compliant:
|
# POSIX Compliant:
|
||||||
# Yes
|
# Yes
|
||||||
#
|
#
|
||||||
printf "Usage: %s\n" \
|
printf "Usage: %s\n" \
|
||||||
"$(basename ${0}) [OPTIONS]
|
"$(basename ${0}) [OPTIONS]
|
||||||
$(arg_required "REQUIRED")
|
$(arg_required "REQUIRED")
|
||||||
$(arg_optional "OPTIONAL")
|
$(arg_optional "OPTIONAL")
|
||||||
|
|
||||||
$(arg_required "--template-file") <templateFile: string> | $(arg_required "-t") <templateFile: string>
|
$(arg_required "--template-file") <templateFile: string> | $(arg_required "-t") <templateFile: string>
|
||||||
$(arg_description "A template file to parse and apply rules for
|
$(arg_description "A template file to parse and apply rules for
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
--template-file example.tmpl")
|
--template-file example.tmpl")
|
||||||
|
|
||||||
$(arg_optional "--contents-dir") <contentsDirectory: string> | $(arg_optional "-c") <contentsDirectory: string>
|
$(arg_optional "--contents-dir") <contentsDirectory: string> | $(arg_optional "-c") <contentsDirectory: string>
|
||||||
$(arg_description "The files that contain contents read by the templates.
|
$(arg_description "The files that contain contents read by the templates.
|
||||||
|
|
||||||
By default this is set to ~/.contents/ if unset, to set via variable you can either use this argument or pass
|
By default this is set to ~/.contents/ if unset, to set via variable you can either use this argument or pass
|
||||||
export CONTENTS_DIRECTORY=\`your contents directory\`
|
export CONTENTS_DIRECTORY=\`your contents directory\`
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
--contents-dir ~/Desktop/contents")
|
--contents-dir ~/Desktop/contents")
|
||||||
|
|
||||||
$(arg_optional "--output-to") <outputFile: string> | $(arg_optional "-o") <outputFile: string>
|
$(arg_optional "--output-to") <outputFile: string> | $(arg_optional "-o") <outputFile: string>
|
||||||
$(arg_description "The file to output the applied template to, this can be set within a template, but can be overriden with this option
|
$(arg_description "The file to output the applied template to, this can be set within a template, but can be overriden with this option
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
--output-to myfile.out")
|
--output-to myfile.out")
|
||||||
|
|
||||||
$(arg_optional "--no-exit") | $(arg_optional "-n")
|
$(arg_optional "--no-exit") | $(arg_optional "-n")
|
||||||
$(arg_description "Do not exit on any errors, continue executing
|
$(arg_description "Do not exit on any errors, continue executing
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
--no-exit")"
|
--no-exit")"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
# Parse input arguments
|
# Parse input arguments
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# Consult the `usage` function
|
# Consult the `usage` function
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# parse_args "$@"
|
# parse_args "$@"
|
||||||
# - All arguments should be ingested by parse_args first for variable setting
|
# - All arguments should be ingested by parse_args first for variable setting
|
||||||
#
|
#
|
||||||
# POSIX Compliant:
|
# POSIX Compliant:
|
||||||
# Yes
|
# Yes
|
||||||
#
|
#
|
||||||
local template_file
|
local template_file
|
||||||
|
|
||||||
while :; do
|
while :; do
|
||||||
case ${1} in
|
case ${1} in
|
||||||
-h | -\? | --help)
|
-h | -\? | --help)
|
||||||
usage # Display a usage synopsis.
|
usage # Display a usage synopsis.
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
--) # End of all options.
|
--) # End of all options.
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
-t | --template-file)
|
-t | --template-file)
|
||||||
shift
|
shift
|
||||||
template_file="${1}"
|
template_file="${1}"
|
||||||
[[ ! -f "${template_file}" ]] &&
|
[[ ! -f "${template_file}" ]] &&
|
||||||
log "error" "The given template file \"${template_file}\" does not exist" &&
|
log "error" "The given template file \"${template_file}\" does not exist" &&
|
||||||
exit 1
|
exit 1
|
||||||
log "info" "Set template file to \"${template_file}\""
|
log "info" "Set template file to \"${template_file}\""
|
||||||
;;
|
;;
|
||||||
-c | --contents-dir)
|
-c | --contents-dir)
|
||||||
shift
|
shift
|
||||||
CONTENTS_DIRECTORY="${1}"
|
CONTENTS_DIRECTORY="${1}"
|
||||||
[[ ! -d "${CONTENTS_DIRECTORY}" ]] &&
|
[[ ! -d "${CONTENTS_DIRECTORY}" ]] &&
|
||||||
log "error" "\"${CONTENTS_DIRECTORY}\" is an invalid path, contents directory must be an absolute path" &&
|
log "error" "\"${CONTENTS_DIRECTORY}\" is an invalid path, contents directory must be an absolute path" &&
|
||||||
exit 1
|
exit 1
|
||||||
log "info" "Set contents directory to \"${CONTENTS_DIRECTORY}\""
|
log "info" "Set contents directory to \"${CONTENTS_DIRECTORY}\""
|
||||||
;;
|
;;
|
||||||
-o | --output-to)
|
-o | --output-to)
|
||||||
shift
|
shift
|
||||||
WRITE_TEMPLATE_MANUALLY_SET=0
|
WRITE_TEMPLATE_MANUALLY_SET=0
|
||||||
WRITE_TEMPLATE_TO="${1}"
|
WRITE_TEMPLATE_TO="${1}"
|
||||||
;;
|
;;
|
||||||
-n | --no-exit)
|
-n | --no-exit)
|
||||||
EXIT_ON_ERROR=1
|
EXIT_ON_ERROR=1
|
||||||
log "info" "No longer exiting on errors."
|
log "info" "No longer exiting on errors."
|
||||||
;;
|
;;
|
||||||
-?*)
|
-?*)
|
||||||
printf 'Unknown option: %s\n' "$1" >&2
|
printf 'Unknown option: %s\n' "$1" >&2
|
||||||
usage
|
usage
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
*) # Default case: No more options, so break out of the loop.
|
*) # Default case: No more options, so break out of the loop.
|
||||||
break ;;
|
break ;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
[[ -z "${template_file}" ]] &&
|
[[ -z "${template_file}" ]] &&
|
||||||
log "error" "No template file provided, exiting" &&
|
log "error" "No template file provided, exiting" &&
|
||||||
exit 1
|
exit 1
|
||||||
templater "${template_file}"
|
templater "${template_file}"
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
@ -17,7 +17,32 @@ For example
|
|||||||
- This is used to tell the current process where to write to, only one should exist
|
- This is used to tell the current process where to write to, only one should exist
|
||||||
in a template and sub-templates if multiple templates are chained together
|
in a template and sub-templates if multiple templates are chained together
|
||||||
- TEMPLATE
|
- TEMPLATE
|
||||||
- This is used to parse the contents of another template and include the contents that are parsed.
|
- This is used to parse the contents of another template and include the contents that are parsed.
|
||||||
For example, say we have a primary template `main.tmpl` that uses the sub-template `sub.tmpl` then
|
For example, say we have a primary template `main.tmpl` that uses the sub-template `sub.tmpl` then
|
||||||
when the parsing process begins it will also parse the contents of `sub.tmpl` and append them to the contents
|
when the parsing process begins it will also parse the contents of `sub.tmpl` and append them to the contents
|
||||||
parsed by `main.tmpl`
|
parsed by `main.tmpl`
|
||||||
|
- VAR
|
||||||
|
- This is used when you want to expand a variable from your environment, say "${HOME}"
|
||||||
|
- EVAL
|
||||||
|
- This is used when you want to evaluate a statement in bash, be careful as any errors here can really nuke things and
|
||||||
|
cause security issues.
|
||||||
|
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
Given a template file with content like so:
|
||||||
|
|
||||||
|
```
|
||||||
|
{#% VAR=$HOME %#}
|
||||||
|
{#% EVAL="ls -alh" %#}
|
||||||
|
```
|
||||||
|
|
||||||
|
The output would be something like
|
||||||
|
```
|
||||||
|
/home/username
|
||||||
|
total 52K
|
||||||
|
drwxr-xr-x 1 sam sam 196 Sep 15 17:03 .
|
||||||
|
drwxr-xr-x 1 sam sam 94 Sep 15 16:42 ..
|
||||||
|
-rwxr-xr-x 1 sam sam 13K Sep 15 17:01 Templater.bash
|
||||||
|
-rw-r--r-- 1 sam sam 1.1K Sep 15 17:03 Templater.md
|
||||||
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user