-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
65 lines (56 loc) · 1.71 KB
/
utils.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
#!/bin/bash
# ANSI color codes
BOLDRED='\033[1;31m'
BOLDGREEN='\033[1;32m'
BOLDYELLOW='\033[1;33m'
NC='\033[0m' # No Color
LOGFILE=script.log
ERRORLOG=errors.log
# Function to print error message with timestamp to stderr and log file, and exit with stack trace
print_error() {
local message="$1"
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "$timestamp: $message" >> "$ERRORLOG"
printf "%s: ${BOLDRED}%s${NC}\n" "$timestamp" "$message" >&2
print_stack_trace
exit 1
}
# Function to print stack trace
print_stack_trace() {
echo "Stack trace:" >&2
for ((i=${#BASH_SOURCE[@]}-1; i>=0; i--)); do
echo " ${BASH_SOURCE[i]}:${BASH_LINENO[i]}" >&2
done
}
# Function to log message to a file
log() {
local message="$1"
echo "$(date): $message" >> "$LOGFILE"
}
# Function to execute adb command and handle errors
adb_execute() {
local command="$1"
adb_command="adb -s $DEVICE_ID $command"
log "Executing: $adb_command"
eval "$adb_command" &>> "$LOGFILE"
if [ $? -ne 0 ]; then
print_error "Failed to execute: $adb_command"
fi
}
# Function to validate device ID
validate_device_id() {
[ -z "$DEVICE_ID" ] && print_error "No device ID provided. Exiting."
}
# Function to validate source and target directories
validate_dirs() {
[ -z "$ANDROID_DIR" ] || [ -z "$LOCAL_DIR" ] && print_error "Source or target directory not defined. Exiting."
[ ! -d "$ANDROID_DIR" ] && print_error "Source directory '$ANDROID_DIR' does not exist. Exiting."
[ ! -d "$LOCAL_DIR" ] && mkdir -p "$LOCAL_DIR"
}
# Function to execute commands in parallel
execute_parallel() {
local command="$1"
local device_id="$2"
echo -e "${BOLDGREEN}Executing command on device $device_id: $command${NC}"
adb_execute "shell $command"
}