Skip to content

Commit 1cc3010

Browse files
committed
2022.08.09:
* new: bash/github/init-yq-workflow.sh: added `ENABLE_YAML_DIFF_PRINT_BEFORE_PATCH`, `ENABLE_YAML_PRINT_AFTER_PATCH` variables to print diff/yaml file before patch instead of after edit (has priority over `ENABLE_YAML_DIFF_PRINT_AFTER_EDIT`, `ENABLE_YAML_PRINT_AFTER_EDIT` variables)
1 parent 477f409 commit 1cc3010

File tree

3 files changed

+84
-34
lines changed

3 files changed

+84
-34
lines changed

bash/github/init-yq-workflow.sh

Lines changed: 78 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
# Yaml specific user variables (to set):
1818
#
1919
# * ENABLE_YAML_PRINT_AFTER_EDIT
20+
# * ENABLE_YAML_PRINT_AFTER_PATCH # has priority over `ENABLE_YAML_PRINT_AFTER_EDIT` variable
2021
# * ENABLE_YAML_DIFF_PRINT_AFTER_EDIT
22+
# * ENABLE_YAML_DIFF_PRINT_BEFORE_PATCH # has priority over `ENABLE_YAML_DIFF_PRINT_AFTER_EDIT` variable
2123
# * ENABLE_YAML_PATCH_DIFF_PAUSE_MODE
2224
#
2325

@@ -36,7 +38,7 @@
3638
# <list-of-yq-eval-strings> && \
3739
# yq_diff "$TEMP_DIR/<output-yaml-edited>" "<input-yaml>" "$TEMP_DIR/<output-diff-edited>" && \
3840
# yq_restore_edited_uniform_diff "$TEMP_DIR/<output-diff-edited>" "$TEMP_DIR/<output-diff-edited-restored>" && \
39-
# yq_patch "$TEMP_DIR/<output-yaml-edited>" "$TEMP_DIR/<output-diff-edited-restored>" "$TEMP_DIR/<output-yaml-edited-restored>" "<output-yaml>"
41+
# yq_patch "$TEMP_DIR/<output-yaml-edited>" "$TEMP_DIR/<output-diff-edited-restored>" "$TEMP_DIR/<output-yaml-edited-restored>" "<output-yaml>" ["<input-yaml>"]
4042
#
4143
# , where:
4244
#
@@ -98,7 +100,9 @@ function yq_init()
98100
fi
99101

100102
[[ -z "$ENABLE_YAML_PRINT_AFTER_EDIT" ]] && ENABLE_YAML_PRINT_AFTER_EDIT=0
103+
[[ -z "$ENABLE_YAML_PRINT_AFTER_PATCH" ]] && ENABLE_YAML_PRINT_AFTER_PATCH=0
101104
[[ -z "$ENABLE_YAML_DIFF_PRINT_AFTER_EDIT" ]] && ENABLE_YAML_DIFF_PRINT_AFTER_EDIT=0
105+
[[ -z "$ENABLE_YAML_DIFF_PRINT_BEFORE_PATCH" ]] && ENABLE_YAML_DIFF_PRINT_BEFORE_PATCH=0
102106

103107
return 0
104108
}
@@ -128,6 +132,15 @@ function yq_fix_null()
128132
done
129133
}
130134

135+
function yq_print_file()
136+
{
137+
local file_path="$1"
138+
139+
echo ">\`$file_path\`:"
140+
echo "$(<"$file_path")"
141+
echo '==='
142+
}
143+
131144
function yq_edit()
132145
{
133146
if [[ -z "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
@@ -137,8 +150,8 @@ function yq_edit()
137150

138151
local prefix_name="$1"
139152
local suffix_name="$2"
140-
local input_file="$3"
141-
local output_file="$4"
153+
local input_yaml_file="$3"
154+
local output_yaml_edited_file="$4"
142155
local edit_list
143156
local last_error
144157

@@ -149,10 +162,8 @@ function yq_edit()
149162

150163
function on_return_handler()
151164
{
152-
if (( ENABLE_YAML_PRINT_AFTER_EDIT )); then
153-
echo ">\`$TEMP_DIR/${prefix_name}-${suffix_name}-0.yml\`:"
154-
echo "$(<"$TEMP_DIR/${prefix_name}-${suffix_name}-0.yml")"
155-
echo '==='
165+
if (( ENABLE_YAML_PRINT_AFTER_EDIT && ! ENABLE_YAML_PRINT_AFTER_PATCH )); then
166+
yq_print_file "$TEMP_DIR/${prefix_name}-${suffix_name}-0.yml"
156167
fi
157168
}
158169

@@ -161,16 +172,14 @@ function yq_edit()
161172

162173
"${YQ_CMDLINE_WRITE[@]}" \
163174
"${edit_list[0]}" \
164-
"$input_file" > "$TEMP_DIR/${prefix_name}-${suffix_name}-0.yml" || return $?
175+
"$input_yaml_file" > "$TEMP_DIR/${prefix_name}-${suffix_name}-0.yml" || return $?
165176

166177
local arg
167178
for arg in "${edit_list[@]:1}"; do
168179
function on_return_handler()
169180
{
170-
if (( ENABLE_YAML_PRINT_AFTER_EDIT )); then
171-
echo ">\`$TEMP_DIR/${prefix_name}-${suffix_name}-${j}.yml\`:"
172-
echo "$(<"$TEMP_DIR/${prefix_name}-${suffix_name}-${j}.yml")"
173-
echo '==='
181+
if (( ENABLE_YAML_PRINT_AFTER_EDIT && ! ENABLE_YAML_PRINT_AFTER_PATCH )); then
182+
yq_print_file "$TEMP_DIR/${prefix_name}-${suffix_name}-${j}.yml"
174183
fi
175184
}
176185

@@ -182,24 +191,22 @@ function yq_edit()
182191
(( j++ ))
183192
done
184193

185-
cp -Tf "$TEMP_DIR/${prefix_name}-${suffix_name}-${i}.yml" "$output_file"
194+
cp -Tf "$TEMP_DIR/${prefix_name}-${suffix_name}-${i}.yml" "$output_yaml_edited_file"
186195
last_error=$?
187196

188197
if [[ ! -f "$TEMP_DIR/${prefix_name}-${suffix_name}-${i}.yml" ]]; then
189198
function on_return_handler()
190199
{
191-
if (( ENABLE_YAML_PRINT_AFTER_EDIT )); then
192-
echo ">\`$output_file\`:"
193-
echo "$(<"$output_file")"
194-
echo '==='
200+
if (( ENABLE_YAML_PRINT_AFTER_EDIT && ! ENABLE_YAML_PRINT_AFTER_PATCH )); then
201+
yq_print_file "$output_yaml_edited_file"
195202
fi
196203
}
197204
fi
198205

199206
return $last_error
200207
}
201208

202-
function yq_diff()
209+
function yq_diff_impl()
203210
{
204211
local input_file_before="$1"
205212
local input_file_after="$2"
@@ -225,18 +232,19 @@ function yq_diff()
225232

226233
(( last_error > 1 )) && return $last_error
227234

228-
# Cut off all empty line removes.
229-
# Based on: https://stackoverflow.com/questions/5410757/how-to-delete-from-a-text-file-all-lines-that-contain-a-specific-string/5410784#5410784
230-
#
231-
#sed -i '/^-$/d' "$output_diff_file"
235+
return 0
236+
}
237+
238+
function yq_diff()
239+
{
240+
yq_diff_impl "$@"
241+
last_error=$?
232242

233-
if (( ENABLE_YAML_DIFF_PRINT_AFTER_EDIT )); then
234-
echo ">\`$output_diff_file\`:"
235-
echo "$(<"$output_diff_file")"
236-
echo '==='
243+
if (( ENABLE_YAML_DIFF_PRINT_AFTER_EDIT && ! ENABLE_YAML_DIFF_PRINT_BEFORE_PATCH )); then
244+
yq_print_file "$output_diff_file"
237245
fi
238246

239-
return 0
247+
return $last_error
240248
}
241249

242250
# Does convert all chunks with at least one `remove` line into separate chunks
@@ -1009,15 +1017,51 @@ function yq_patch_diff_pause()
10091017

10101018
function yq_patch()
10111019
{
1012-
local input_file="$1"
1020+
if [[ -z "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
1021+
echo "$0: error: \`TEMP_DIR\` directory must be defined and exist: \`$TEMP_DIR\`." >&2
1022+
return 255
1023+
fi
1024+
1025+
local input_yaml_edited_file="$1"
10131026
local input_diff_file="$2"
1014-
local temp_file="$3"
1015-
local output_file="$4"
1027+
local temp_yaml_file="$3"
1028+
local inout_yaml_file="$4"
1029+
local input_yaml_file="${4:-"$inout_yaml_file"}" # if empty, then output yaml is used as input yaml to generate diff to print
10161030

1017-
{
1018-
"${YQ_PATCH_DIFF_CMDLINE[@]}" -o "$temp_file" -i "$input_diff_file" "$input_file" && yq_patch_diff_pause && \
1019-
mv -Tf "$temp_file" "$output_file"
1020-
} || yq_patch_diff_pause
1031+
local last_error=0
1032+
1033+
local temp_yaml_file_name="${temp_yaml_file##*/}"
1034+
1035+
"${YQ_PATCH_DIFF_CMDLINE[@]}" -o "$temp_yaml_file" -i "$input_diff_file" "$input_yaml_edited_file" \
1036+
> "$TEMP_DIR/${temp_yaml_file_name}.patch.stdout.log" 2> "$TEMP_DIR/${temp_yaml_file_name}.patch.stderr.log"
1037+
last_error=$?
1038+
1039+
if (( ! last_error )); then
1040+
if (( ENABLE_YAML_DIFF_PRINT_BEFORE_PATCH )); then # technically - after patch, logically - before patched file output rewrite
1041+
yq_diff_impl "$input_yaml_file" "$temp_yaml_file" "$TEMP_DIR/${temp_yaml_file_name}.diff" || return 255
1042+
yq_print_file "$TEMP_DIR/${temp_yaml_file_name}.diff"
1043+
fi
1044+
1045+
cat "$TEMP_DIR/${temp_yaml_file_name}.patch.stderr.log"
1046+
cat "$TEMP_DIR/${temp_yaml_file_name}.patch.stdout.log"
1047+
1048+
if (( ENABLE_YAML_PRINT_AFTER_PATCH )); then
1049+
yq_print_file "$temp_yaml_file"
1050+
fi
1051+
1052+
yq_patch_diff_pause
1053+
1054+
mv -Tf "$temp_yaml_file" "$inout_yaml_file"
1055+
1056+
return $?
1057+
fi
1058+
1059+
cat "$TEMP_DIR/${temp_yaml_file_name}.patch.stderr.log"
1060+
cat "$TEMP_DIR/${temp_yaml_file_name}.patch.stdout.log"
1061+
1062+
yq_patch_diff_pause
1063+
1064+
return $last_error
10211065
}
10221066

10231067
tkl_set_return

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2022.08.09:
2+
* new: bash/github/init-yq-workflow.sh: added `ENABLE_YAML_DIFF_PRINT_BEFORE_PATCH`, `ENABLE_YAML_PRINT_AFTER_PATCH` variables to print diff/yaml file before patch instead of after edit (has priority over `ENABLE_YAML_DIFF_PRINT_AFTER_EDIT`, `ENABLE_YAML_PRINT_AFTER_EDIT` variables)
3+
14
2022.08.09:
25
* fixed: bash/cache/accum-content.sh: missed to process forwarding backslash from the `md5sum` utility
36
* changed: bash: support `TEMP_DIR` to be declared externally

userlog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
> :warning: to find all changes use [changelog.txt](https://github.com/andry81-devops/gh-workflow/blob/master/changelog.txt) file in a directory
44
5+
## 2022.08.09:
6+
* new: bash/github/init-yq-workflow.sh: added `ENABLE_YAML_DIFF_PRINT_BEFORE_PATCH`, `ENABLE_YAML_PRINT_AFTER_PATCH` variables to print diff/yaml file before patch instead of after edit (has priority over `ENABLE_YAML_DIFF_PRINT_AFTER_EDIT`, `ENABLE_YAML_PRINT_AFTER_EDIT` variables)
7+
58
## 2022.08.08:
69
* fixed: bash/github/init-yq-workflow.sh: `yq_restore_edited_uniform_diff` inaccurate blank lines position
710

0 commit comments

Comments
 (0)