-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-yq-workflow.sh
215 lines (176 loc) · 5.1 KB
/
init-yq-workflow.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
# NOTE:
# This is a composite script to use from a composite GitHub action.
#
# NOTE:
#
# Yaml specific system variables (to use):
#
# * YQ_CMDLINE_READ
# * YQ_CMDLINE_WRITE
# * YQ_DIFF_CMDLINE
# * YQ_DIFF_GREP_CMDLINE_0
# * YQ_PATCH_DIFF_CMDLINE
#
# Yaml specific user variables (to set):
#
# * ENABLE_YAML_PRINT_AFTER_EDIT
# * ENABLE_YAML_DIFF_PRINT_AFTER_EDIT
#
# CAUTION:
#
# The `yq` command lines designed to be able to run independently to these `yq` implementations:
#
# * https://github.com/kislyuk/yq - a `jq` wrapper (default Cygwin distribution)
# * https://github.com/mikefarah/yq - Go implementation (default Ubuntu 20.04 distribution)
#
# Script both for execution and inclusion.
[[ -z "$BASH" || (-n "$SOURCE_GHWF_INIT_YQ_WORKFLOW_SH" && SOURCE_GHWF_INIT_YQ_WORKFLOW_SH -ne 0) ]] && return
SOURCE_GHWF_INIT_YQ_WORKFLOW_SH=1 # including guard
[[ -z "$GH_WORKFLOW_ROOT" ]] && {
echo "$0: error: \`GH_WORKFLOW_ROOT\` variable must be defined." >&2
exit 255
}
source "$GH_WORKFLOW_ROOT/_externals/tacklelib/bash/tacklelib/bash_tacklelib" || exit $?
function yq_init()
{
which yq > /dev/null || return $?
local yq_help="$(yq --help)"
# CAUTION:
# Array instead of string is required here for correct expansion!
#
if grep 'https://github.com/mikefarah/yq[/ ]' - <<< "$yq_help" >/dev/null; then
YQ_CMDLINE_READ=(yq)
YQ_CMDLINE_WRITE=(yq e)
YQ_DIFF_CMDLINE=(diff -awB)
YQ_DIFF_GREP_CMDLINE_0=(grep -vE '^\s*$')
YQ_PATCH_DIFF_CMDLINE=(patch -Nlt --merge)
elif grep 'https://github.com/kislyuk/yq[/ ]' - <<< "$yq_help" >/dev/null; then
YQ_CMDLINE_READ=(yq -c -r)
YQ_CMDLINE_WRITE=(yq -y)
YQ_DIFF_CMDLINE=(diff -awB)
YQ_DIFF_GREP_CMDLINE_0=(grep -vE '^\s*(#|$)')
YQ_PATCH_DIFF_CMDLINE=(patch -Nlt --merge)
else
YQ_CMDLINE_READ=(yq)
YQ_CMDLINE_WRITE=(yq)
YQ_DIFF_CMDLINE=(diff -awB)
YQ_DIFF_GREP_CMDLINE_0=(grep -vE '^\s*$')
YQ_PATCH_DIFF_CMDLINE=(patch -Nlt --merge)
echo "$0: error: \`yq\` implementation is not known." >&2
return 255
fi
[[ -z "$ENABLE_YAML_PRINT_AFTER_EDIT" ]] && ENABLE_YAML_PRINT_AFTER_EDIT=0
[[ -z "$ENABLE_YAML_DIFF_PRINT_AFTER_EDIT" ]] && ENABLE_YAML_DIFF_PRINT_AFTER_EDIT=0
return 0
}
yq_init || exit $?
function yq_is_null()
{
(( ! ${#@} )) && return 255
eval "[[ -z \"\$$1\" || \"\$$1\" == 'null' ]]" && return 0
return 1
}
function yq_fix_null()
{
local __var_name
local __arg
for __arg in "$@"; do
__var_name="${__arg%%:*}"
yq_is_null "$__var_name" && \
if [[ "$__arg" != "$__var_name" ]]; then
tkl_declare "$__var_name" "${__arg#*:}"
else
tkl_declare "$__var_name" ''
fi
done
}
function yq_edit()
{
if [[ -z "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
echo "$0: error: `TEMP_DIR` directory must be defined and exist: \`$TEMP_DIR\`." >&2
return 255
fi
local edit_file_name="$1"
local input_file="$2"
local output_file="$3"
local edit_list
local last_error
edit_list=( "${@:4}" )
local i=0
local j=1
function on_return_handler()
{
if (( ENABLE_YAML_PRINT_AFTER_EDIT )); then
echo ">\`$TEMP_DIR/${edit_file_name}-edit0.yml\`:"
echo "$(<"$TEMP_DIR/${edit_file_name}-edit0.yml")"
echo '==='
fi
}
# on return handler
tkl_push_trap 'on_return_handler' RETURN
"${YQ_CMDLINE_WRITE[@]}" \
"${edit_list[0]}" \
"$input_file" > "$TEMP_DIR/${edit_file_name}-edit0.yml" || return $?
local arg
for arg in "${edit_list[@]:1}"; do
function on_return_handler()
{
if (( ENABLE_YAML_PRINT_AFTER_EDIT )); then
echo ">\`$TEMP_DIR/${edit_file_name}-edit${j}.yml\`:"
echo "$(<"$TEMP_DIR/${edit_file_name}-edit${j}.yml")"
echo '==='
fi
}
"${YQ_CMDLINE_WRITE[@]}" \
"$arg" \
"$TEMP_DIR/${edit_file_name}-edit${i}.yml" > "$TEMP_DIR/${edit_file_name}-edit${j}.yml" || return $?
i=$j
(( j++ ))
done
mv -Tf "$TEMP_DIR/${edit_file_name}-edit${i}.yml" "$output_file"
last_error=$?
if [[ ! -f "$TEMP_DIR/${edit_file_name}-edit${i}.yml" ]]; then
function on_return_handler()
{
if (( ENABLE_YAML_PRINT_AFTER_EDIT )); then
echo ">\`$output_file\`:"
echo "$(<"$output_file")"
echo '==='
fi
}
fi
return $last_error
}
function yq_diff()
{
local input_file_before="$1"
local input_file_after="$2"
local output_diff_file="$3"
local last_error
# 0 No differences were found.
# 1 Differences were found.
# >1 An error occurred.
#
"${YQ_DIFF_CMDLINE[@]}" \
<("${YQ_DIFF_GREP_CMDLINE_0[@]}" "$input_file_before") \
"$input_file_after" > "$output_diff_file"
last_error=$?
(( last_error > 1 )) && return $last_error
if (( ENABLE_YAML_DIFF_PRINT_AFTER_EDIT )); then
echo ">\`$output_diff_file\`:"
echo "$(<"$output_diff_file")"
echo '==='
fi
return 0
}
function yq_patch()
{
local input_file="$1"
local input_diff_file="$2"
local temp_file="$3"
local output_file="$4"
"${YQ_PATCH_DIFF_CMDLINE[@]}" -o "$temp_file" -i "$input_diff_file" "$input_file" && \
mv -Tf "$temp_file" "$output_file"
}
tkl_set_return