157 lines
3.4 KiB
Bash
Executable File
157 lines
3.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# force verbose
|
|
#_verbose=true
|
|
verbose(){ [[ $_verbose == true ]] && echo "$@"; }
|
|
debug(){ [[ $_verbose == true ]] && verbose -e "$(declare -p $@ | sed "s/^declare -\(.\) \([^=]*\)=/\\$c_purple\1 \\$c_cyan\2\\$c_yellow=\\$c_/")"; }
|
|
|
|
args=("$@")
|
|
opts=()
|
|
mandatories=()
|
|
envs=()
|
|
declare -A params=()
|
|
file=""
|
|
need_help=""
|
|
|
|
_icons=(🔘 ⚪ ⚫)
|
|
|
|
debug args
|
|
|
|
if [[ -r "${args[0]}" ]]
|
|
then
|
|
file="${args[0]}"
|
|
unset args[0]
|
|
elif [[ -r "${args[1]}" ]]
|
|
then
|
|
file="${args[1]}"
|
|
opts=(${args[0]})
|
|
unset args[0] args[1]
|
|
fi
|
|
|
|
args=("${args[@]}") # Re-index array after unset of some indexes
|
|
|
|
#declare -p args file opts
|
|
|
|
given()
|
|
{
|
|
[[ -z "${!1}" ]] && echo false || echo true
|
|
}
|
|
|
|
show_help()
|
|
{
|
|
local mands=$(for i in ${!mandatories[@]}
|
|
do
|
|
local mand="${mandatories[$i]}"
|
|
# debug mand
|
|
[[ $(given ${mandatories[$i]}) == true ]] &&
|
|
printf "$c_cyan⚫${mandatories[$i]}$c_ = $c_green\"${!mand}\"$c_\n " ||
|
|
printf "$c_light$c_red🔘${mandatories[$i]}$c_ is mandatory\n "
|
|
done)
|
|
local opts=$(for i in ${!envs[@]}
|
|
do
|
|
declare -a envar=(`echo "${envs[$i]}" | sed 's/=/ /'`)
|
|
printf "$c_light$c_blue⚪${envar[0]}$c_, defaults to $c_green\"${envar[1]}\"$c_\n "
|
|
done)
|
|
local exps=""
|
|
|
|
cat << EOF
|
|
|
|
SHTPL template $(echo -e "$c_purple$file$c_")
|
|
This template $([[ $isPipable == true ]] && echo is pipable and" ")needs:
|
|
$mands
|
|
and accepts optional:
|
|
$opts
|
|
|
|
Examples:
|
|
$(for i in ${!mandatories[@]}; do printf "${mandatories[$i]}=foo "; done)$file
|
|
$(for i in ${!mandatories[@]}; do printf "${mandatories[$i]}=foo "; done)$(for i in ${!envs[@]}; do printf "$(echo "${envs[$i]}" | sed 's/=.*//')=foo "; done)$file
|
|
$([[ $isPipable == true ]] && echo "something | ")$(for i in ${!mandatories[@]}; do printf "${mandatories[$i]}=foo "; done)$file
|
|
|
|
EOF
|
|
exit 0
|
|
}
|
|
|
|
param()
|
|
{
|
|
# echo param "$1"
|
|
local name="$(echo "$1" | sed 's/^-*\([^=]*\).*/\1/')" # | awk -F= '{print $1}')"
|
|
local value="$(echo "$1" | sed 's/^-+//' | awk -F= '{print $2}')"
|
|
# debug name value
|
|
params[$name]="${value:-true}"
|
|
case "$name" in
|
|
h|help) need_help=true; params[h]="${value:-true}"; params[help]="${value:-true}" ;;
|
|
v|verbose) _verbose=true; params[v]="${value:-true}"; params[verbose]="${value:-true}" ;;
|
|
p|pipable) isPipable=true; params[p]="${value:-true}"; params[pipable]="${value:-true}" ;;
|
|
esac
|
|
}
|
|
|
|
for i in ${!opts[@]}
|
|
do
|
|
case "${opts[$i]}" in
|
|
-*|--*) param "${opts[$i]}" ;;
|
|
*=*) envs+=("${opts[$i]}") ;;
|
|
*) mandatories+=("${opts[$i]}"); [[ $(given ${opts[$i]}) == false ]] && need_help=true ;;
|
|
esac
|
|
done
|
|
|
|
for i in ${!args[@]}
|
|
do
|
|
case "${args[$i]}" in
|
|
-*|--*) param "${args[$i]}" ;;
|
|
# *) ;;
|
|
esac
|
|
|
|
# [[ "${args[$i]}" == '-h' || "${args[$i]}" == '--help' ]] && need_help=true
|
|
done
|
|
|
|
|
|
debug args opts mandatories envs params file need_help _verbose isPipable
|
|
|
|
[[ $need_help == true ]] && show_help;
|
|
|
|
|
|
|
|
|
|
# Utils
|
|
|
|
__shtpl_path__=$(realpath $0)
|
|
__shtpl_dir__=$(dirname $0)
|
|
__tpl_path__=$(realpath $file)
|
|
__tpl_name__=$(basename $__tpl_path__)
|
|
__tpl_dir__=$(dirname $__tpl_path__)
|
|
|
|
|
|
# Broken, don't use!
|
|
pipe()
|
|
{
|
|
[[ ! -t 0 ]] && cat
|
|
}
|
|
|
|
user-input()
|
|
{
|
|
echo "$@ (ctrl-D): " 1>&2
|
|
res="$(sed '/^\04$/q')"
|
|
echo "$res"
|
|
}
|
|
|
|
lowerCase()
|
|
{
|
|
( [[ ! -z "$@" ]] && echo $@ || cat ) | awk '{print tolower($0)}'
|
|
}
|
|
|
|
slug()
|
|
{
|
|
( [[ ! -z "$@" ]] && echo $@ || cat ) | lowerCase | sed 's/[^a-zA-Z0-9-]/_/g'
|
|
}
|
|
|
|
|
|
|
|
# Execution
|
|
|
|
eval "echo \"$(cat $file | sed '
|
|
# Suppress hashbang
|
|
1{ /#\!.*shtpl.*/ d }
|
|
# Excape quotes
|
|
s/"/\\"/g
|
|
')\""
|