Skip to content

Instantly share code, notes, and snippets.

@lleaff
Last active December 4, 2017 10:22
Show Gist options
  • Save lleaff/43b76dc955584a70798cb9361d95cfca to your computer and use it in GitHub Desktop.
Save lleaff/43b76dc955584a70798cb9361d95cfca to your computer and use it in GitHub Desktop.
Replace marked variables with environment variables (in-place version)
#!/bin/bash
################################################################################
#
# Replace marked variables with environment variables (in-place version).
# Author: lleaff
# Version: 1.0
#
################################################################################
# Stop on error
set -e
helpfn() {
echo -e "USAGE: $(basename $0) FILE... [OPTION]..." >&2
echo -e "Replace environment variables in file(s)." >&2
echo -e "" >&2
echo -e "OPTIONS:" >&2
echo -e " -d, --no-modify Don't modify file in place, output result to stdout." >&2
echo -e " -e, --env Source environment variables from specified env file." >&2
echo -e " -o, --opening Specify hole opening character sequence (default: #{{env: )." >&2
echo -e " -c, --closing Specify hole closing character sequence (default: }} )." >&2
echo -e " -h, --help Show this message." >&2
}
CRESET="\e[0m"
CRED="\e[31m"
CLYELLOW="\e[93m"
CGREEN="\e[32m"
CBLUE="\e[34m"
echo -ne "${CRED}"
OPTS=`getopt -o ho:c:de: --long help,opening:,closing:,debug,no-modify,env -n 'parse-options' -- "$@"`
echo -ne "${CRESET}"
if [ $? != 0 ]; then
echo -e "${CRED}[ERROR] Failed parsing options.${CRESET}" >&2
helpfn
exit 1
fi
eval set -- "$OPTS"
OPENING='#{{env:'
CLOSING='}}'
HELP=false
NO_MODIFY=false
ENV_FILE=''
DEBUG=false
while true; do
case "$1" in
-h | --help ) HELP=true; break ;;
-o | --opening ) OPENING="$2"; shift 2 ;;
-c | --closing ) CLOSING="$2"; shift 2 ;;
-d | --no-modify ) NO_MODIFY=true; shift ;;
-e | --env ) ENV_FILE="$2"; shift 2 ;;
--debug ) DEBUG=true; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if $HELP; then
helpfn
exit 0
fi
if [[ $# -lt 1 ]]; then
echo -ne "${CRED}" >&2
helpfn
echo -ne "${CRESET}" >&2
exit 1
fi
#============================================================
if $DEBUG; then set -x; fi
if [[ -n "$ENV_FILE" ]]; then
set -o allexport
source "$ENV_FILE"
set +o allexport
fi
substenv_rep() {
FILE="$1"
# Find referenced variables
vars=$(grep -oe "${OPENING}\([A-Za-z0-9_]*\)${CLOSING}" "$FILE" | \
sort -u | \
sed "s/${OPENING}\([A-Za-z0-9_]*\)${CLOSING}/\1/")
echo -e "${CBLUE}$(echo $vars | wc -w) variables found: $(echo $vars).${CRESET}" >&2
tmpfile="$(dirname $FILE)/.$(basename $FILE).substenv-ng~"
cp "$FILE" "$tmpfile"
for var in $vars; do
value=$(printenv $var | sed 's/\//\\\//g')
if [[ -z $value ]]; then
echo -e "${CLYELLOW}[WARNING] Empty environment variable \"${var}\".${CRESET}" >&2
fi
sed -i "s/${OPENING}${var}${CLOSING}/${value}/g" "$tmpfile"
done
cat "$tmpfile"
rm "$tmpfile"
}
while [[ $# -ge 1 ]]; do
FILE="$1"
orig="$(dirname $FILE)/.$(basename $FILE).orig"
if [[ ! -f "$orig" ]]; then
if [[ ! -f "$FILE" ]]; then
echo -e "${CRED}[ERROR] No file found: \"${FILE}\".${CRESET}" >&2
exit 1
fi
cp "$FILE" "$orig"
else
echo -e "${CBLUE}Starting from \"$orig\".${CRESET}" >&2
fi
if $NO_MODIFY; then
substenv_rep "$orig" $OPENING $CLOSING
else
substenv_rep "$orig" $OPENING $CLOSING > "$FILE"
echo -ne "${CGREEN}Replaced environment variables in \"$FILE\". " >&2
echo -e "${CBLUE}Original backed up to \"$orig\".${CRESET}" >&2
fi
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment