-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-helper.shell
More file actions
300 lines (296 loc) · 11.4 KB
/
script-helper.shell
File metadata and controls
300 lines (296 loc) · 11.4 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env bash
# Description: Calling block for the script-helper.shell
#
# ## Prividing operational assistance (helper)
# if [[ ( ${assistance} == 'true' ) || \
# ( ${example_mode} == 'true' ) || \
# ( ${wizard_mode} == 'true' ) || \
# ( ${info_mode} == 'true' )
# ]]; then
# ## JSON source and object:
# local script_helper="${HOME}/.local/bin/script-helper.shell";
# if [[ -e ${script_helper} ]]; then
# source ${script_helper};
# script_helper --caller="${script_file}" \
# --examples=${example_mode} \
# --wizard=${wizard_mode} \
# --info=${info_mode} \
# ;
# return 1;
# else newline;
# echo -e "Warning: Script Helper was not found! ";
# newline;
# return 2;
# fi;
# fi;
## -----------------------------------------------------------------------------
## Objective: Printing empty lines (output)
function newline () { echo -e; return 0; };
function is_base64() {
local input_string="$1"
# Check if the input_string matches Base64 format and length is a multiple of 4
if [[ "${input_string}" =~ ^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$ ]] &&
(( ${#input_string} % 4 == 0 )); then
## Decoding and re-encoding for verification
local decoded="$( echo "$input_string" | base64 --decode 2>/dev/null )";
## echo -e "Decoded Input-String: ${decoded}";
if [[ $? -eq 0 ]] && \
[[ "$(echo "${decoded}" | base64 2>/dev/null)" == "${input_string}" ]]; then
## It's a valid Base64 input-string
return 0;
fi;
fi;
## It's not a Base64 input-string.
return 1;
}; alias is-base64='is_base64';
function script_helper () {
## -------------------------------------------------------------------------
local oIFS="${IFS}";
for xitem in "${@}"; do
IFS='='; set `echo -e "${xitem}" | sed -e '1s|^\(-\)\{1,\}||'`
[[ ${1#*\--} = "caller" ]] && local calling_script="${2}";
[[ ${1#*\--} = "examples" ]] && local example_mode="${2}";
[[ ${1#*\--} = "wizard" ]] && local wizard_mode="${2}";
[[ ${1#*\--} = "info" ]] && local info_mode="${2}";
done; IFS="${oIFS}";
## -------------------------------------------------------------------------
local oIFS="${IFS}"; IFS=$'\n';
[[ ${#example_mode} -eq 0 ]] && { local example_mode='false'; };
[[ ${#wizard_mode} -eq 0 ]] && { local wizard_mode='false'; };
[[ ${#info_mode} -eq 0 ]] && { local info_mode='false'; };
# echo -e "Example: ${example_mode}";
# echo -e " Wizard: ${wizard_mode}";
## -------------------------------------------------------------------------
## echo -e "Calling Script: ${calling_script}";
local script_filename="$( basename ${calling_script} )";
spanwidth="${#script_filename}";
local output_spacer="$(
printf "% $(echo "${#script_filename} + 1" | bc)s"
)";
local script_dirname="$( dirname ${calling_script} )";
if [[ "${#script_dirname}" -eq 0 ]]; then script_dirname='.'; fi;
## JSON source and object:
local json_file="${script_dirname}/${script_filename%\.*}.json";
## echo -e "Script JSON file: ${json_file}";
local json_object="$(
jq --compact-output '.' "${json_file%\.*}.json"
)";
local warning="\nWarning: Unable to load '${json_file}' file ! \n";
abort='false';
if [[ -e ${json_file} ]]; then
[[ ${#json_object} -lt 3 ]] && {
echo -e "${warning}";
abort='true';
};
else echo -e "${warning}";
abort='true';
fi;
## Aborting condition (abort=true)
[[ ${abort} == 'true' ]] && return 1;
## -------------------------------------------------------------------------
declare -a required=($( echo -e "${json_object}" \
| jq '.script.required[]' --raw-output
)); # echo -e "required: ${required[@]}";
declare -a optional=($( echo -e "${json_object}" \
| jq '.script.optional[]' --raw-output
)); # echo -e "Optional: ${optional[@]}";
# Aggregating Required + Optional sets
declare -a options=( ${required[@]} ${optional[@]} );
declare -a parameters=($( echo -e "${json_object}" \
| jq '.params|keys[]' --raw-output
)); # echo -e "Optional: ${parameters[@]}";
## -------------------------------------------------------------------------
total_examples=$( echo -e "${json_object}" | jq -r ".script.examples|length" );
declare -a examples=();
local example='';
for (( each=0 ; each < ${total_examples} ; each++ )); do
declare -a usage_options=($( echo -e "${json_object}" \
| jq ".script.examples[${each}][]" --raw-output
));
example="${script_filename} ";
counter=0;
for xitem in ${usage_options[@]}; do
syntax='.params."'${xitem}'"|select(.value!=null)|.value';
item_value="$(
echo -e "${json_object}" \
| jq --raw-output "${syntax}"
)";
if is_base64 "${item_value}"; then
# echo -e "String is Base64 encoded.";
local decoded="$( echo "${item_value}" | base64 --decode )";
# echo "Decoded string: $decoded";
isbase64="$(
echo -n "${item_value}" \
| base64 --decode
)";
# echo -e "Is-Base64: ${isbase64}";
# else echo "String is not Base64 encoded. Skipping."
fi;
if [[ ${#isbase64} -gt 0 ]]; then item_value="${isbase64}"; fi;
[[ $(( counter++ )) -gt 0 ]] && example+="${output_spacer}";
example+="--${xitem}";
if [[ ${#item_value} -gt 0 ]]; then
example+="=";
example+="\"${item_value}\" \\*";
else example+=" \\*";
fi;
done; example+=";";
examples+=("${example}");
done;
## -------------------------------------------------------------------------
if [[ ${wizard_mode} == 'true' ]]; then
local command='';
newline;
echo -e "$(
echo -e "${json_object}" \
| jq --raw-output ".script.headers.interview"
):";
newline;
local question=''; indexer=0;
for xitem in ${options[@]}; do
(( ++indexer ));
syntax='.params."'${xitem}'"|select(.value!=null)|.query';
question="$(
echo -e "${json_object}" \
| jq --raw-output "${syntax}"
)";
if [[ ${#question} -gt 0 ]]; then
heading="${indexer} -) ${question}: ";
read -p "${heading}" response;
if [[ ${#response} -gt 0 ]]; then
include=" --${xitem}=\"${response}\"";
echo -e " ${include}";
newline;
command+="${include}";
else newline;
fi;
else ## Parsing non-parameterized options:
heading="${indexer} -) Include '${xitem}' [Y/n] : ";
read -p "${heading}" response;
if [[ 'Y|y' =~ ${response:0:1} ]]; then
include=" --${xitem}";
echo -e " ${include}";
newline;
command+="${include}";
else newline;
fi;
fi;
done;
newline; echo -e "> ${calling_script}${command} ;"; newline;
read -p "Execute ? [Y/n] : " response;
if [[ 'Y|y' =~ ${response:0:1} ]]; then
eval $(pwd)/${script_filename}${command} ;
else newline;
fi;
return 2;
fi;
## -------------------------------------------------------------------------
if [[ ${example_mode} == 'true' ]]; then
## Listing command example:
index=1; ## Indexing each example:
for each in ${examples[@]}; do
newline;
echo -e "Example [$(( index++ ))]:";
echo -e "${each}" \
| sed -e 's|\([[:blank:]]\)\{2,\}||g' \
-e 's|\ \\\*\-\-| --|g' \
-e 's|\(\ \)\(\\\*\)\(;\)$|\1\3|' \
;
done; newline;
return 3;
fi;
## -------------------------------------------------------------------------
if [[ ${info_mode} == 'true' ]]; then
newline;
declare -a references="$( echo -e "${json_object}" \
| jq --compact-output '.script.info'
)";
## Generating GitHub properties:
## export github_[id,domain,repo,path]
eval $( echo -e "${references}" \
| jq -r ".service|to_entries" \
| jq -r "map(\"export github_\(.key)='\(.value|tostring)'\")|.[]"
);
echo -en "GitHub Project: ";
echo -e "https://${github_id}/${github_domain}/${github_repo}";
echo -e " Source: /${github_path}";
newline;
## Generating Team properties:
## export team_[id,name]
eval $( echo -e "${references}" \
| jq -r ".team|to_entries" \
| jq -r "map(\"export team_\(.key)='\(.value|tostring)'\")|.[]"
);
echo -e " Team: ${team_name}";
newline;
## Generating DevOps properties:
## export devops_[name,email]
total_users=$( echo -e "${references}" | jq -r ".devops|length" );
for (( each=0 ; each < ${total_users} ; each++ )); do
eval $( echo -e "${references}" \
| jq -r ".devops[${each}]|to_entries" \
| jq -r "map(\"export devops_\(.key)='\(.value|tostring)'\")|.[]"
);
echo -e "DevOps: ${devops_name} <${devops_email}>"
done;
newline;
return 4;
fi;
## -------------------------------------------------------------------------
declare -a headers=(
"required:Required"
"optional:Optional"
);
for header in ${headers[@]}; do
keyset="${header%\:*}";
options_header="${header#*\:}";
newline;
echo -en "${options_header} ";
options_spacer="$( printf "% $(echo "${#options_header} + 4" | bc)s" )";
case "${keyset}" in
required ) declare -a listing=(${required[@]}); ;;
optional ) declare -a listing=(${optional[@]}); ;;
esac;
counter=0;
for xitem in ${listing[@]}; do
syntax='.params."'${xitem}'"|select(.message!=null)|.message';
message="$(
echo -e "${json_object}" \
| jq --raw-output "${syntax}"
)";
[[ $(( counter++ )) -gt 0 ]] && echo -en "${options_spacer}";
echo -e "$( printf "% -20s" --${xitem} )${message}";
done;
done;
declare -a helper=($(
echo -e "${json_object}" \
| jq --raw-output '.helper | keys[]' \
| sed -e 's|help$||' -e 's|info$||' \
| sed '/^$/d' \
));
helper+=("info");
helper+=("help");
# echo -e "${helper}";
for xitem in ${helper[@]}; do
syntax='.helper."'${xitem}'".message';
message="$(
echo -e "${json_object}" \
| jq --raw-output "${syntax}"
)";
echo -en "${options_spacer}";
echo -e "$( printf "% -20s" --${xitem} )${message}";
done;
## -----------------------------------------------------------------
usage_header="Usage:";
newline;
echo -en "${usage_header}";
newline; newline;
for each in ${examples[@]}; do
echo -e "${each}" \
| sed -e 's|\(\ \)\(\\\*\)\(\ \)\{2,\}|\1\\\n'${output_spacer}'|g' \
-e 's|\(\ \)\(\\\*\)\(\;\)$|\1\\\n\3|' \
; newline;
done;
# ${calling_script} --info;
return 0;
}; alias script-helper='script_helper';