Skip to content

Instantly share code, notes, and snippets.

@kolomenkin
Forked from vi/envtemplater
Last active April 25, 2016 12:57
Show Gist options
  • Save kolomenkin/2bfb3ea296648f12049c1c8b3350baea to your computer and use it in GitHub Desktop.
Save kolomenkin/2bfb3ea296648f12049c1c8b3350baea to your computer and use it in GitHub Desktop.
Simplify setting multiple environment variables using templates
#!/bin/bash
script_version="1.08"
SUBST="@repo@"
SAVE_IFS="$IFS"
if [[ -z $1 || $1 = "--help" ]]; then
echo >&2 "Git-specialized version of tool: $script_version"
echo >&2 "Usage: T_template1=substitution1 ... T_templateN=substitutionN ./envtemplater.sh repo1@template ... repoN@template -- command [arguments]"
echo >&2 "Example: T_github=ALTURL_${SUBST}=https://github.com/vi/${SUBST}.git ./envtemplater.sh dive@github fdlinecombine@github -- ./submodule_update_alturl.sh --force"
exit 1
fi
set -e
#set -x
# mangle_submodule_name: 'repo/1/name' -> 'repo_1_name'"
mangle_submodule_name() {
echo $1 | sed 's/\//_/g'
}
# export mangle_submodule_name for child process:
export -f mangle_submodule_name
while [ "$1" != '--' ]; do
if [[ $# == 0 ]]; then
echo >&2 "-- not found in command line"
exit 4;
fi
IFS="@"
Q=($1)
IFS=$SAVE_IFS
reponame=$(mangle_submodule_name "${Q[0]}")
servicename=${Q[1]}
TEMPLATE="T_${servicename}"
TEMPLATE_CONTENT=$(eval "echo \"\$$TEMPLATE\"")
#echo "reponame = $reponame"
#echo "servicename = $servicename"
#echo "TEMPLATE = $TEMPLATE"
#echo "TEMPLATE_CONTENT = $TEMPLATE_CONTENT"
if [ -z $TEMPLATE_CONTENT ]; then
echo >&2 "Template environment variable $TEMPLATE was not found. Please read envtemplater.sh help again and prepare environment correctly!"
exit 2
fi
VALUE="${TEMPLATE_CONTENT//${SUBST}/${reponame}}"
#echo "VALUE = $VALUE"
eval "export $VALUE"
shift
done
shift
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment