-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash_functions.sh
385 lines (338 loc) · 11.6 KB
/
bash_functions.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/bash
BASE_DIR=${HOME}/.projectliberty
BASE_NAME=gateway-dev
ALL_PROFILES="account graph content_publishing content_watcher webhook"
COMPOSE_FILES=""
BOX_WIDTH=96
###################################################################################
# Wrangle grep because MacOS doesn't come with a PCRE-enabled grep by default.
# If we don't find one, disable our "pretty" output function.
###################################################################################
function check_pcre_grep() {
PCRE_GREP=
if echo "foobar" | grep -q -P "foo(?=bar)" >/dev/null 2>&1; then
PCRE_GREP=grep
else
# Grep is not PCRE compatible, check for other greps
if command -v ggrep >/dev/null; then # MacOS Homebrew might have ggrep
PCRE_GREP=ggrep
elif command -v pcre2grep > /dev/null; then # MacOS Homebrew could also have pcre2grep
PCRE_GREP=pcre2grep
fi
fi
if [ -z "${PCRE_GREP}" ]; then
cat << EOI
WARNING: No PCRE-capable 'grep' utility found; pretty terminal output disabled.
If you're on a Mac, try installing GNU grep:
brew install grep
EOI
read -p 'Press any key to continue... '
OUTPUT='echo -e'
else
OUTPUT="box_text -w ${BOX_WIDTH}"
fi
}
###################################################################################
# yesno
#
# Description: Prompt the user with a question requiring a yes/no response. The
# prompt will be augmented with "? [y/n]: " (with capitalization reflecting
# the default response if the user just hits <Enter>)
#
# ${1} - Prompt string
# ${2} - Default if response is empty (only first character matters)
###################################################################################
function yesno() {
DEFAULT=Y
DEFAULT_STR="Y/n"
if [ $# -gt 1 ]; then
if [[ ${2} =~ ^[^Yy] ]]; then
DEFAULT_STR="y/N"
DEFAULT=N
fi
fi
echo
read -n 1 -p "${1}? [${DEFAULT_STR}]: "
echo
if [[ ${REPLY} =~ ^[Yy]$ ]] ; then
return 0
elif [ -z "${REPLY}" -a ${DEFAULT} = Y ]; then
return 0
fi
return 1
}
###################################################################################
# display_width
#
# Description: Calculate the display width of a string considering some common wide characters
#
# ${1} - Input string
###################################################################################
function display_width() {
local str="$1"
local width=0
local char
for (( i=0; i<${#str}; i++ )); do
char="${str:i:1}"
if [ -z ${PCRE_GREP} ]; then
# Regular character, assume it takes one column
width=$((width + 1))
else
if echo "$char" | ${PCRE_GREP} -P -q '[^\x{00}-\x{7F}]' ; then
width=$((width + 2))
else
# Regular character, assume it takes one column
width=$((width + 1))
fi
fi
done
echo $width
}
###################################################################################
# box_text
#
# Description: Output text surrounded by a box; padded with blank lines inside the
# top and bottom edges. If TEXT is omitted, it will read from stdin.
#
# Usage: box_text [-w <min_width>] [TEXT]
#
# min_width - Pad the output text box to a least min_width characters.
# Default: do not pad
###################################################################################
function box_text() {
local input
local min_width=0
# Parse the optional -w argument
while getopts ":w:" opt; do
case $opt in
w)
min_width=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
return 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
return 1
;;
esac
done
shift $((OPTIND - 1))
if [ -z "$1" ]; then
# Read input from stdin if no arguments are provided
input=$(cat)
else
# Use the provided argument as input
input="$1"
fi
local IFS=$'\n'
# local lines=($input)
local lines=()
# Read the string into the array, preserving empty lines
while IFS= read -r line || [[ -n $line ]]; do
lines+=("$line")
done <<< "${input}"
local max_length=0
# Find the maximum length of a line, accounting for Unicode width
for line in "${lines[@]}"; do
line_length=$(display_width "$line")
if [ $line_length -gt $max_length ]; then
max_length=$line_length
fi
done
# Ensure the box width is at least the specified minimum width
max_length=$(( max_length > min_width ? max_length : min_width ))
# Top border
echo "┌$(printf '─%.0s' $(seq 1 $((max_length + 2))))┐"
# Print each line with padding
for line in "${lines[@]}"; do
printf "│ %-${max_length}s │\n" "$line"
done
# Bottom border
echo "└$(printf '─%.0s' $(seq 1 $((max_length + 2))))┘"
}
###################################################################################
# box_text_attention
#
# Description: Like box text, but output is surrounded by a box;
# padded with blank lines and attention-getting characters.
# ##################################################################################
function box_text_attention() {
local input
local min_width=0
local attention=false
# Parse the optional -w argument
while getopts ":w:" opt; do
case $opt in
w)
min_width=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
return 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
return 1
;;
esac
done
shift $((OPTIND - 1))
if [ -z "$1" ]; then
# Read input from stdin if no arguments are provided
input=$(cat)
else
# Use the provided argument as input
input="$1"
fi
local IFS=$'\n'
# local lines=($input)
local lines=()
# Read the string into the array, preserving empty lines
while IFS= read -r line || [[ -n $line ]]; do
lines+=("$line")
done <<< "${input}"
local max_length=0
# Find the maximum length of a line, accounting for Unicode width
for line in "${lines[@]}"; do
line_length=$(display_width "$line")
if [ $line_length -gt $max_length ]; then
max_length=$line_length
fi
done
# Ensure the box width is at least the specified minimum width
max_length=$(( max_length > min_width ? max_length : min_width ))
# Top border
echo "┌$(printf '─%.0s' $(seq 1 $((max_length + 10))))┐"
echo "│ 🔗💠📡$(printf ' %.0s' $(seq 1 $((max_length + 2)))) │"
# Print each line with padding
for line in "${lines[@]}"; do
real_length=$(( max_length ))
printf "│ 🔗💠📡 %-${real_length}s │\n" "$line"
done
# Bottom border
echo "│ 🔗💠📡$(printf ' %.0s' $(seq 1 $((max_length + 2)))) │"
echo "└$(printf '─%.0s' $(seq 1 $((max_length + 10))))┘"
}
###################################################################################
# ask_and_save
#
# Description: Prompt the user with a string, and save the response to the environment file
#
# ${1} - env var name to save to the ENV_FILE
# ${2} - prompt string
# ${3} - [optional] default response if user hits <Enter>
# ${4} - [optional] hide input if true (Default: false)
###################################################################################
function ask_and_save() {
local var_name=${1}
local prompt=${2}
local default_value=${3}
local hide_input=${4:-false}
local value=
local input=
if [ -z "${default_value}" ]
then
if [ "${hide_input}" = true ]
then
read -rsp $'\n'"${prompt} (INPUT HIDDEN): " input
echo
else
read -rp $'\n'"${prompt}: " input
fi
value=${input}
else
if [ "${hide_input}" = true ]
then
read -rsp $'\n'"${prompt} [${default_value}] (INPUT HIDDEN): " input
echo
else
read -rp $'\n'"${prompt} [${default_value}]: " input
fi
value=${input:-$default_value}
fi
# Make the variable available in the current shell, useful for subsequent commands
export ${var_name}="${value}"
echo "${var_name}=\"${value}\"" >> ${ENV_FILE}
}
###################################################################################
# redact_sensitive_values
#
# Description: Runs a 'sed' command on the input file to replace specific environment
# values with "[REDACTED]". Useful for not exposing secrets in live demos.
#
###################################################################################
function redact_sensitive_values() {
local env_file="$1"
sed \
-e 's/^PROVIDER_ACCOUNT_SEED_PHRASE=.*/PROVIDER_ACCOUNT_SEED_PHRASE=[REDACTED]/' \
-e 's/^IPFS_BASIC_AUTH_USER=.*/IPFS_BASIC_AUTH_USER=[REDACTED]/' \
-e 's/^IPFS_BASIC_AUTH_SECRET=.*/IPFS_BASIC_AUTH_SECRET=[REDACTED]/' \
"$env_file"
}
###################################################################################
# export_save_variable
#
# Description: Save a name/value pair to the environment file & simultaneously
# export to the current environment.
#
# ${1} - env variable name
# ${2} - env variable value
#
###################################################################################
function export_save_variable() {
local variable_name=$1
local value=$2
echo "${variable_name}=\"${value}\"" >> ${ENV_FILE}
export ${variable_name}="${value}"
}
###################################################################################
# prefix_postfix_values
#
# Description: Given a list of values in ${1} separated by IFS, return a composite string
# consisting of each value, prefixed by ${2} and postfixed by ${3}.
#
###################################################################################
function prefix_postfix_values() {
str=""
for val in ${1}; do
str="${str} ${2}${val}${3}"
done
echo ${str}
}
###################################################################################
# is_frequency_ready
#
# Description: Runs a command to check the health status of the 'frequency' service
#
###################################################################################
function is_frequency_ready() {
health=$( docker compose -p ${COMPOSE_PROJECT_NAME} ps --format '{{.Health}}' frequency )
if [ "${health}" = 'healthy' ]; then
return 0
fi
return 1
}
###################################################################################
# append
#
# Description: Appends a variable to another variable, creating the variable
# if it does not exist.
# Usage: append <variable> <value_to_append>
# Arguments:
# variable: The name of the variable to append to.
# value_to_append: The value to append to the variable.
###################################################################################
function append() {
local var_name="$1" # The name of the variable
local value="$2" # The value to append
# Check if the variable exists and is not unset
if [ -n "${!var_name}" ]; then
# Append the value to the existing variable
eval "$var_name=\"\${$var_name}$value\""
else
# If the variable does not exist, set it to the value
eval "$var_name=\"$value\""
fi
}