Skip to content

Instantly share code, notes, and snippets.

@malys
Last active August 14, 2020 08:39
Show Gist options
  • Save malys/d4ea856154ccbe3a9d9217947dcadbba to your computer and use it in GitHub Desktop.
Save malys/d4ea856154ccbe3a9d9217947dcadbba to your computer and use it in GitHub Desktop.
[Templater] Template engine in bash for Openshift #openshift #bash #template #kubernetes
#!/bin/bash
#set -x
#bash templater.sh < oc-template.tpl.yml | oc process -f - | oc create -f -
#bash templater.sh < oc-template.tpl.yml > oc-template.yml && oc process -f oc-template.yml | oc create -f -
while IFS= read -r line
do
end_offset=${#line}
IS_VAR=true
# Read variable in file
while [[ "${line:0:$end_offset}" =~ (.*)(\$\{([a-zA-Z_][a-zA-Z_0-9]*)\})(.*) ]] ; do
IS_VAR=false
# pre string
PRE="${BASH_REMATCH[1]}"
# post string
POST="${BASH_REMATCH[4]}${line:$end_offset:${#line}}"
# variable name
VARNAME="${BASH_REMATCH[3]}"
eval 'VARVAL="$(cat '$VARNAME'.yml)"'
if [ -z "$VARVAL" ]; then
# value is varname
VARVAL="\${$VARNAME}"
fi
li=''
while IFS= read -r linev
do
li="$PRE$linev$POST"
cleaned=${li//[$'\r\n']} && cleaned=${cleaned%%*( )}
echo -n "$cleaned"
echo
done < <(printf '%s\n' "$VARVAL")
end_offset=${#PRE}
done
if "$IS_VAR" = "true" ] ; then
cleaned=${line//[$'\r\n']} && cleaned=${cleaned%%*( )}
echo -n "$cleaned"
echo
fi
done < <(printf '%s\n' "$(cat; echo -n)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment