Last active
September 17, 2020 13:17
-
-
Save JParkinson1991/c92b07f6955a95e254be2ce66e6b77f4 to your computer and use it in GitHub Desktop.
envsubst-recursive.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Usage envsubst-recursive.sh [source root] | |
# Options: | |
# -a|--append - Set string to append to processed source file names | |
# -f|--filter - Set the filename filter (only process files matching this filter) | |
# -o|--output - Set the output directory for substituted file | |
# -q|--quiet - Enable quiet mode, no script output | |
# -v|--variables - Set the variable names to replace only. IMPORTANT: Single quote this argument | |
# Script variable defaults | |
sourceRoot="" | |
outputRoot="" | |
replacementVariables="" | |
processedAppend="" | |
fileNameFilter="" | |
quietModeEnabled=1 #false | |
# Process CLI Arguments | |
positional=() | |
while [[ $# -gt 0 ]] | |
do | |
case "$1" in | |
-a|--append) | |
processedAppend="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-f|--filter) | |
fileNameFilter="-name $2" | |
shift # past argument | |
shift # past value | |
;; | |
-o|--output) | |
outputRoot="${2%/}" | |
shift # past argument | |
shift # past value | |
;; | |
-q|--quiet) | |
quietModeEnabled=0 # true | |
shift # past argument | |
;; | |
-v|--variables) | |
replacementVariables="$2" | |
shift # past argument | |
shift # past value | |
;; | |
*) | |
#Unhandled options treated as positional arguments | |
positional+=("$1") | |
shift # past argument | |
;; | |
esac | |
done | |
set -- "${positional[@]}" # restore positional parameters | |
# Grab the source root, trimming any trailing / | |
# Ensure not empty, error and exit if it is | |
# Create glob patterns for source root | |
sourceRoot=${1%/} | |
if [[ -z "$sourceRoot" ]]; then | |
echo "Error: No source root defined" | |
exit 1 | |
fi | |
if [[ ! -d "$sourceRoot" ]]; then | |
echo "Error: $sourceRoot not found" | |
exit 1 | |
fi | |
sourceGlob=($sourceRoot/**/*) | |
# If no output set via options, use the validated source root | |
if [[ -z "$outputRoot" ]]; then | |
outputRoot="$sourceRoot" | |
fi | |
# Output start message if not in quiet mode | |
if [[ $quietModeEnabled -eq 1 ]]; then | |
echo "Substituting environment variables" | |
fi | |
# Loop every directory | |
for file in $(find "$sourceRoot" -type f $fileNameFilter); do | |
# Determine paths post processing | |
sourcePath="$file$processedAppend" | |
outputPath="${file/$sourceRoot/$outputRoot}" | |
# If output root directory does not exist create it | |
if [[ ! -d $(dirname "$outputPath") ]]; then | |
mkdir -p $(dirname "$outputPath") | |
fi | |
# If file being processed and final source path different (ie has been appended to), move to location | |
if [[ "$file" != "$sourcePath" ]]; then | |
mv "$file" "$sourcePath" | |
fi | |
# Do the variable substitution | |
if [[ -z "$replacementVariables" ]]; then | |
envsubst < "$sourcePath" > $outputPath | |
else | |
envsubst "$replacementVariables" < "$sourcePath" > $outputPath | |
fi | |
# Output process record if not in quiet mode | |
if [[ $quietModeEnabled -eq 1 ]]; then | |
echo "Processed: $sourcePath -> $outputPath" | |
fi | |
done | |
# Output end message if not in quiet mode | |
if [[ $quietModeEnabled -eq 1 ]]; then | |
echo "Done" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment