-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathexpand-workflow.sh
More file actions
executable file
·84 lines (64 loc) · 2.09 KB
/
expand-workflow.sh
File metadata and controls
executable file
·84 lines (64 loc) · 2.09 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
set -euo pipefail
print_help() {
cat <<'EOF'
expand-includes.sh — simple textual include expander
USAGE:
expand-includes.sh <input-file>
expand-includes.sh -h | --help
DESCRIPTION:
Reads <input-file> and writes the expanded result to stdout.
Lines matching the following form are replaced by the contents
of the referenced file:
include: "path/to/file.yml.in" # optional comment
Rules:
- The filepath MUST be in double quotes.
- Leading whitespace (spaces and/or tabs) is preserved and applied
to every line of the included file.
- Trailing comments starting with '#' are ignored.
- Whitespace around 'include:' and the filename is ignored.
- Included files may themselves contain include: directives.
- Expansion is purely textual; no YAML parsing is performed.
EOF
}
expand_file() {
local file="$1"
while IFS= read -r line || [[ -n "$line" ]]; do
# Remove trailing comments for matching (keep original for output)
local stripped="${line%%#*}"
# Match YAML include line:
#
# <indent>include: "path"
#
if [[ "$stripped" =~ ^([[:space:]]*)include[[:space:]]*:[[:space:]]*\"([^\"]+)\"[[:space:]]*$ ]]; then
local indent="${BASH_REMATCH[1]}"
local include_file="${BASH_REMATCH[2]}"
if [[ ! -f "$include_file" ]]; then
echo "ERROR: included file not found: $include_file" >&2
return 1
fi
# Recursively expand included file (no subshells)
if ! mapfile -t inc_lines < <(expand_file "$include_file"); then
return 1
fi
for inc_line in "${inc_lines[@]}"; do
printf "%s%s\n" "$indent" "$inc_line"
done
else
echo "$line"
fi
done < "$file"
}
# ---- main ----
if [[ $# -ne 1 ]]; then
print_help >&2
exit 1
fi
case "$1" in
-h|--help)
print_help
exit 0
;;
esac
printf "# Warning: generated file, do not edit. Generated by '%s %s'\n" "$0" "$*"
expand_file "$1"