-
Notifications
You must be signed in to change notification settings - Fork 2
/
yaml-to-properties.sh
executable file
·77 lines (60 loc) · 1.45 KB
/
yaml-to-properties.sh
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
#!/usr/bin/env bash
FILE=
errorExit () {
echo; echo "ERROR: $1"; echo
exit 1
}
usage () {
cat << END_USAGE
${SCRIPT_NAME} - Convert a YAML formatted file to properties format
Usage: ${SCRIPT_NAME} <options>
-f | --file <name> : [MANDATORY] Yaml file to process
-h | --help : Show this usage
Examples:
========
$ ${SCRIPT_NAME} --file examples/simple.yaml
END_USAGE
exit 1
}
checkYq () {
[[ $(yq -V) =~ ( 4.|v4.) ]] || errorExit "Must have yq v4 installed (https://github.com/mikefarah/yq)"
}
processOptions () {
while [[ $# -gt 0 ]]; do
case "$1" in
-f | --file)
FILE="$2"
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
usage
;;
esac
done
}
checkFileExists () {
[ -n "${FILE}" ] || usage
[ -f "${FILE}" ] || errorExit "File ${FILE} does not exist"
}
removeEmptyArrayAndMap () {
TMP_FILE=$(mktemp)
cp "$FILE" "$TMP_FILE"
sed -e 's,\[[[:space:]]*\],,g' -e 's,{[[:space:]]*},,g' "$FILE" > "$TMP_FILE"
}
processYaml () {
removeEmptyArrayAndMap
yq eval '.. | select((tag == "!!map" or tag == "!!seq") | not) | (path | join(".")) + "=" + .' "$TMP_FILE" || errorExit "yq failed"
rm -f "$TMP_FILE"
}
main () {
checkYq
processOptions "$@"
checkFileExists
processYaml
}
######### Main #########
main "$@"