-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-values.sh
More file actions
executable file
·45 lines (35 loc) · 1.03 KB
/
read-values.sh
File metadata and controls
executable file
·45 lines (35 loc) · 1.03 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
#!/usr/bin/env bash
set -euo pipefail
shopt -s extglob
FILES=()
while IFS= read -r line; do
[[ -z "$line" ]] && continue
FILES+=("$line")
done
if [ ${#FILES[@]} -eq 0 ]; then
echo "Error: No values files provided" >&2
exit 1
fi
MERGED=$(yq eval '.' "${FILES[0]}")
echo "Read file: ${FILES[0]}" >&2
# Merge each subsequent file
for ((i=1; i<${#FILES[@]}; i++)); do
echo "Read file: ${FILES[i]}" >&2
MERGED=$(echo "$MERGED" | yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' - "${FILES[i]}")
done
echo "Merged values: " >&2
echo ${MERGED} >&2
# Each argument format:
# key = .yq.expression (spaces around '=' are optional)
for out in "$@"; do
[[ -z "$out" ]] && continue
IFS='=' read -r KEY EXPR <<< "$out"
# trim surrounding whitespace
KEY="${KEY##+([[:space:]])}"
KEY="${KEY%%+([[:space:]])}"
EXPR="${EXPR##+([[:space:]])}"
EXPR="${EXPR%%+([[:space:]])}"
echo "Parsed expression: key is '${KEY}', value is '${EXPR}'" >&2
VALUE=$(echo "$MERGED" | yq eval "$EXPR" -)
echo "${KEY}=${VALUE}"
done