forked from ipdxco/unified-github-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender-template.sh
More file actions
executable file
·40 lines (34 loc) · 970 Bytes
/
render-template.sh
File metadata and controls
executable file
·40 lines (34 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env bash
# This script is used to render GitHub Workflow templates.
set -euo pipefail
# Check the number of arguments. 2 or 3 are expected.
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
echo "Usage: render.sh <template> <context> <output>"
exit 1
fi
template="$1"
context="$2"
output="${3:-}"
engine='
s#\$\{\{\{\s*\.?(.*?)\s*\}\}\}#
my $key = $1;
my $result = `jq -cjn '"'"'env.context | fromjson | .$key'"'"'`;
if($? != 0) {
print STDERR "jq: error on line: $_";
exit 1;
}
$result
#gex
'
# Render the template.
export context
# If template and output are the same, perform the rendering in-place.
if [ "$template" = "$output" ]; then
perl -p -i -e "$engine" "$template"
# Otherwise, if output is not specified, print the result to stdout.
elif [ -z "$output" ]; then
perl -p -e "$engine" "$template"
# Otherwise, write the result to the specified output file.
else
perl -p -e "$engine" "$template" > "$output"
fi