Skip to content

Commit 2252973

Browse files
Improve code readability and fix warnings
1 parent 561024f commit 2252973

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

compose/bin/log

+17-13
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ Options:
2424
configure_options() {
2525
echo -e "\n\033[96mCurrent log path: $LOGS_PATH\033[0m\n"
2626
echo -e "\033[96mCurrent container name: $CONTAINER_NAME\033[0m\n"
27-
read -p "$(echo -e "Do you want to change these values? (\033[92mY\033[0m/\033[91mn\033[0m): ")" change_option
27+
read -r -p "$(echo -e "Do you want to change these values? (\033[92mY\033[0m/\033[91mn\033[0m): ")" change_option
2828

2929
if [[ "$change_option" =~ [Yy] ]];
3030
then
31-
read -p "Enter the new log path (press Enter to keep current path): " new_logs_path
32-
read -p "Enter the new container name (press Enter to keep current name): " new_container_name
31+
read -r -p "Enter the new log path (press Enter to keep current path): " new_logs_path
32+
read -r -p "Enter the new container name (press Enter to keep current name): " new_container_name
3333

3434
if [[ -n "$new_logs_path" ]];
3535
then
@@ -74,10 +74,14 @@ search_errors () {
7474
# Loop through each log file
7575
for log_file in ./log/*.log; do
7676
# Search for different types of log messages and format the output
77-
local errors=$(grep -E -r --color=always "\(ERROR\|FATAL\|CRITICAL\)" "$log_file" | format_log_output || true)
78-
local warnings=$(grep -E -r --color=always "\bWARNING\b" "$log_file" | format_log_output || true)
79-
local info=$(grep -E -r --color=always "INFO" "$log_file" | format_log_output || true)
80-
local debug=$(grep -E -r --color=always "DEBUG" "$log_file" | format_log_output || true)
77+
local errors
78+
errors=$(grep -E -r --color=always "\(ERROR\|FATAL\|CRITICAL\)" "$log_file" | format_log_output || true)
79+
local warnings
80+
warnings=$(grep -E -r --color=always "\bWARNING\b" "$log_file" | format_log_output || true)
81+
local info
82+
info=$(grep -E -r --color=always "INFO" "$log_file" | format_log_output || true)
83+
local debug
84+
debug=$(grep -E -r --color=always "DEBUG" "$log_file" | format_log_output || true)
8185
# If any errors or warnings are found, print them
8286
if [ -n "$errors" ] || [ -n "$warnings" ] || [ -n "$info" ] || [ -n "$debug" ]; then
8387
found_errors=true
@@ -156,15 +160,15 @@ while true;
156160
echo -e "\033[96m5.\033[0m Configure options"
157161

158162
# Read user input for option selection
159-
read -p "Enter the option number (1, 2, 3, 4 or 5): " option
163+
read -r -p "Enter the option number (1, 2, 3, 4 or 5): " option
160164

161165
if [ "$option" == "1" ];
162166
then
163167
show_logs
164168
elif [ "$option" == "2" ];
165169
then
166170
show_logs
167-
read -p "Enter the log file name (with extension, e.g., system.log): " log_file
171+
read -r -p "Enter the log file name (with extension, e.g., system.log): " log_file
168172
view_log "$log_file" | format_log_output
169173
elif [ "$option" == "3" ];
170174
then
@@ -175,10 +179,10 @@ while true;
175179
echo -e "\033[96m1.\033[0m View all logs in real-time"
176180
echo -e "\033[96m2.\033[0m View only error logs in real-time"
177181

178-
read -p "Enter the option number (1 or 2): " tail_option
182+
read -r -p "Enter the option number (1 or 2): " tail_option
179183
echo -e "\n\033[96mChoose a log file to tail:\033[0m"
180184
show_logs
181-
read -p "Enter the log file name (with extension, e.g., system.log): " log_file
185+
read -r -p "Enter the log file name (with extension, e.g., system.log): " log_file
182186
echo -e "\033[96mPress [Ctrl+C] to stop tailing logs\033[0m"
183187

184188
if [ "$tail_option" == "1" ];
@@ -192,7 +196,7 @@ while true;
192196
fi
193197

194198
echo -e "\033[96mPress [Enter] to return to the menu\033[0m"
195-
read -r -n 1 -s discard
199+
read -r -n 1 -s
196200
docker exec -it "$CONTAINER_NAME" pkill -f "tail -f $LOGS_PATH/$log_file"
197201
elif [ "$option" == "5" ];
198202
then
@@ -203,7 +207,7 @@ while true;
203207
fi
204208

205209
# Ask the user if they want to continue
206-
read -p "$(echo -e "\033[92mContinue (Y/\033[91mn)\033[0m")? " continue_option
210+
read -r -p "$(echo -e "\033[92mContinue (Y/\033[91mn)\033[0m")? " continue_option
207211
if [[ "$continue_option" =~ [Nn] ]];
208212
then
209213
break

0 commit comments

Comments
 (0)