-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties-merger.sh
executable file
·277 lines (243 loc) · 8.42 KB
/
properties-merger.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
#
# Copyright (c) Adrien BRICCHI and contributors. All rights reserved.
# Licensed under the MIT license. See LICENSE.md file in the project root for details.
#
VERSION_NUMBER=7
VERSION_DATE="2023/03/08"
GRAY='\033[1;30m'
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;34m'
BOLD='\033[1m'
STD='\033[0m'
# Parse input
TEST_MODE=false
HELP=false
APPEND_DELETED_VALUES=false
while [[ $# -gt 0 ]]
do
key="$1"
case ${key} in
-i|--input)
INPUT_FILE="$2"
shift
;;
-s|--sample)
SAMPLE_FILE="$2"
shift
;;
-o|--output)
OUTPUT_FILE="$2"
shift
;;
-t|--test)
TEST_MODE=true
;;
-a|--append-deleted-values)
APPEND_DELETED_VALUES=true
;;
--no-color|--no-colour)
GRAY=''
RED=''
GREEN=''
YELLOW=''
BOLD=''
STD=''
;;
-v|--version)
echo ${VERSION_NUMBER}
exit 0
;;
-h|--help)
echo -e "${BOLD}NAME${STD}"
echo -e " properties-merger - merge a sample .properties file with already existing values"
echo -e ""
echo -e "${BOLD}SYNOPSIS${STD}"
echo -e " ./properties-merger.sh -i my.properties.old -s my.properties.sample [OPTIONS]"
echo -e ""
echo -e "${BOLD}DESCRIPTION${STD}"
echo -e ""
echo -e " Mandatory arguments :"
echo -e ""
echo -e " ${BOLD}-i${STD}, ${BOLD}--input${STD}"
echo -e " The input file, where existing data will be fetch."
echo -e ""
echo -e " ${BOLD}-s${STD}, ${BOLD}--sample${STD}"
echo -e " The sample property file. The output model will be based on this one."
echo -e ""
echo -e " Optional arguments :"
echo -e ""
echo -e " ${BOLD}-o${STD}, ${BOLD}--output${STD}"
echo -e " The output file path. The file should not exists, or an error will be returned."
echo -e " If not set, results will be print on the standard output."
echo -e ""
echo -e " ${BOLD}-t${STD}, ${BOLD}--test${STD}"
echo -e " The test mode, with emphasis on merged data."
echo -e " This mode will invalidate the -o option."
echo -e ""
echo -e " ${BOLD}-a${STD}, ${BOLD}--append-deleted-values${STD}"
echo -e " Adds the unknown keys from the input file at the end of the output."
echo -e " This mode is activated automatically on test mode."
echo -e ""
echo -e " ${BOLD}--no-color${STD}, ${BOLD}--no-colour${STD}"
echo -e " Disables colours on test mode."
echo -e ""
echo -e " ${BOLD}-h${STD}, ${BOLD}--help${STD}"
echo -e " Displays this help."
echo -e ""
echo -e " ${BOLD}-v${STD}, ${BOLD}--version${STD}"
echo -e " Displays the current (int) version."
echo -e ""
echo -e "${BOLD}AUTHOR${STD}"
echo -e " Written by Adrien Bricchi"
echo -e ""
echo -e "${BOLD}COPYRIGHT${STD}"
echo -e " Copyright © 2016 Adrien BRICCHI. License MIT"
echo -e " This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law."
echo -e ""
echo -e "${BOLD}VERSION${STD}"
echo -e " v.${VERSION_NUMBER} (${VERSION_DATE})"
echo -e ""
exit 0
;;
*)
echo -e "${RED}Error : Unknown argument : $1${STD}"
echo -e "Use --help argument for the list of parameters"
exit 1
;;
esac
shift
done
# Safety checks
if [[ ! -f ${INPUT_FILE} ]];
then
echo -e "${RED}Error : Input file (--input) does not exist.${STD}"
exit 2
fi
if [[ ! -f ${SAMPLE_FILE} ]];
then
echo -e "${RED}Error : Sample file (--sample) does not exist.${STD}"
exit 3
fi
if [[ ${INPUT_FILE} == ${SAMPLE_FILE} ]];
then
echo -e "${RED}Error : Input and Sample files are the same. This is probably not what you want.${STD}"
exit 4
fi
if [[ -f ${OUTPUT_FILE} ]];
then
echo -e "${RED}Error : Output file already exists.${STD}"
exit 5
fi
if [[ ${TEST_MODE} == true ]];
then
unset OUTPUT_FILE
APPEND_DELETED_VALUES=true
fi
# Merge files
# Here we reverse the input file into a temp one (with tac)
# To get the last value directly, and break the loop as earlier.
# We append the date in ms to avoid
TMP_REVERSE_INPUT="/tmp/.properties-merger.$(date +%s%N)-${RANDOM}.tmp"
tac ${INPUT_FILE} > ${TMP_REVERSE_INPUT}
# Regex explain : ^\s*([^#]*?)=(.*)$
#
# ^\s* Ignore spaces from the begining of line
# ([^#]*?)= Catches non-# chars until the first "=" (non greedy),
# (.*)$ Catches everything, til the end of line
#
PROPERTIES_REGEX="^\s*([^#|=]*)=(.*)$"
# The read command trims leading and trailing spaces?
# The IFS value is cleared here to prevent that.
while IFS= read -r current_line
do
if [[ "${current_line}" =~ $PROPERTIES_REGEX ]];
then
current_key="${BASH_REMATCH[1]}"
current_value="${BASH_REMATCH[2]}"
unset input_value
# Fetching old value
while IFS= read -r input_line
do
if [[ "${input_line}" =~ $PROPERTIES_REGEX ]] && [[ "${BASH_REMATCH[1]}" == ${current_key} ]];
then
input_value="${BASH_REMATCH[2]}"
break
fi
done < "${TMP_REVERSE_INPUT}"
# Printing result
# Checking if old value is set, to keep existing empty values ("")
if [[ ${TEST_MODE} == true ]];
then
if [[ -z ${input_value+x} ]];
then
echo -e "[${GREEN}SAMPLE ${STD}] ${current_key}=${current_value}¶"
elif [[ ${input_value} == ${current_value} ]]
then
echo -e "[${GRAY}SAME ${STD}] ${current_key}=${current_value}¶"
else
echo -e "[${YELLOW}INPUT ${STD}] ${current_key}=${input_value}¶"
fi
elif [[ -z ${input_value+x} ]];
then
if [[ -z ${OUTPUT_FILE+x} ]];
then
echo "${current_key}=${current_value}"
else
echo "${current_key}=${current_value}" >> ${OUTPUT_FILE}
fi
else
if [[ -z ${OUTPUT_FILE+x} ]];
then
echo "${current_key}=${input_value}"
else
echo "${current_key}=${input_value}" >> ${OUTPUT_FILE}
fi
fi
else
# Empty lines and comments are simply kept
if [[ ${TEST_MODE} == true ]]
then
echo -e "[${GRAY}COMMENT${STD}] ${current_line}"
elif [[ -z ${OUTPUT_FILE+x} ]];
then
echo "${current_line}"
else
echo "${current_line}" >> ${OUTPUT_FILE}
fi
fi
done < "${SAMPLE_FILE}"
# Printing deleted values
if [[ ${APPEND_DELETED_VALUES} == true ]];
then
while IFS= read -r input_line
do
if [[ "${input_line}" =~ $PROPERTIES_REGEX ]];
then
current_key="${BASH_REMATCH[1]}"
current_value="${BASH_REMATCH[2]}"
key_exists_in_sample=false
while IFS= read -r sample_line
do
if [[ "${sample_line}" =~ $PROPERTIES_REGEX ]] && [[ "${BASH_REMATCH[1]}" == ${current_key} ]];
then
key_exists_in_sample=true
fi
done < "${SAMPLE_FILE}"
if [[ ${key_exists_in_sample} == false ]];
then
if [[ ${TEST_MODE} == true ]];
then
echo -e "[${RED}DELETED${STD}] ${current_key}=${current_value}¶"
elif [[ -z ${OUTPUT_FILE+x} ]];
then
echo "${current_key}=${current_value}"
else
echo "${current_key}=${current_value}" >> ${OUTPUT_FILE}
fi
fi
fi
done < "${INPUT_FILE}"
fi
rm ${TMP_REVERSE_INPUT}