Skip to content

Commit cb7077d

Browse files
committed
xseed v2.1.1
- improve logging - use standardized log format for all entries - formatting fixes - exit code tweaks
1 parent 4d41cf1 commit cb7077d

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

xseed.sh

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,28 @@
1111
VERSION='2.1.0'
1212
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1313
ENV_PATH="$SCRIPT_DIR/.env"
14+
15+
# Function to log messages
16+
log_message() {
17+
local log_type="$1"
18+
local message="$2"
19+
echo "$(date '+%Y-%m-%d %H:%M:%S') [$log_type] $message" | tee -a "$LOG_FILE"
20+
}
21+
log_message "INFO" "xseed.sh script started $VERSION"
1422
EVAR=false
1523
if [ -f "$ENV_PATH" ]; then
1624
# shellcheck source=.env
17-
echo "Loading environment variables from $ENV_PATH file"
25+
log_message "INFO" "Loading environment variables from $ENV_PATH file"
1826
# shellcheck disable=SC1090 # shellcheck sucks
1927
if source "$ENV_PATH"; then
20-
echo "Environment variables loaded successfully"
28+
log_message "INFO" "Environment variables loaded successfully"
2129
EVAR=true
2230
else
23-
echo "Error loading environment variables" >&2
24-
exit 1
31+
log_message "ERROR" "Error loading environment variables" >&2
32+
exit 2
2533
fi
2634
else
27-
echo ".env file not found in script directory ($ENV_PATH)"
35+
log_message "DEBUG" ".env file not found in script directory ($ENV_PATH)"
2836
fi
2937

3038
# Use environment variables with descriptive default values
@@ -33,19 +41,9 @@ USENET_CLIENT_NAME=${USENET_CLIENT_NAME:-SABnzbd}
3341
XSEED_HOST=${XSEED_HOST:-crossseed}
3442
XSEED_PORT=${XSEED_PORT:-8080}
3543
LOG_FILE=${LOG_FILE:-/config/xseed.log}
36-
# shellcheck disable=SC2269
37-
XSEED_APIKEY=${XSEED_APIKEY}
38-
39-
# Function to log messages
40-
log_message() {
41-
local log_type="$1"
42-
local message="$2"
43-
echo "$(date '+%Y-%m-%d %H:%M:%S') [$log_type] $message" | tee -a "$LOG_FILE"
44-
}
45-
46-
log_message "INFO" "xseed.sh script started $VERSION"
44+
XSEED_APIKEY=${XSEED_APIKEY:-}
4745
log_message "DEBUG" "Using '.env' file for config?: $EVAR"
48-
log_message "INFO" "Gathered Starr environment variables:"
46+
log_message "INFO" "Using Configuration:"
4947
log_message "INFO" "TORRENT_CLIENT_NAME=$TORRENT_CLIENT_NAME"
5048
log_message "INFO" "USENET_CLIENT_NAME=$USENET_CLIENT_NAME"
5149
log_message "INFO" "XSEED_HOST=$XSEED_HOST"
@@ -61,11 +59,11 @@ cross_seed_request() {
6159
headers+=(-H "X-Api-Key: $XSEED_APIKEY")
6260
fi
6361
response=$(curl --silent --output /dev/null --write-out "%{http_code}" "${headers[@]}")
64-
62+
6563
if [ "$response" == "000" ]; then
6664
log_message "ERROR" "Failed to connect to $XSEED_HOST:$XSEED_PORT. Timeout or connection refused."
6765
fi
68-
66+
6967
echo "$response"
7068
}
7169

@@ -94,23 +92,23 @@ detect_application() {
9492
# shellcheck disable=SC2154 # These are set by Starr on call
9593
folderPath="$sonarr_destinationpath"
9694
else
97-
if [ -z "$sonarr_release_releasetype" ]; then {
95+
if [ -z "$sonarr_release_releasetype" ]; then
9896
# shellcheck disable=SC2154 # These are set by Starr on call
9997
folderPath="$sonarr_episodefile_sourcefolder"
10098
# shellcheck disable=SC2154 # These are set by Starr on call
10199
filePath="$sonarr_episodefile_path"
102-
}
103100
else
104101
# shellcheck disable=SC2154 # These are set by Starr on call
105102
filePath="$sonarr_episodefile_paths"
106103
fi
107104
fi
105+
108106
# shellcheck disable=SC2154 # These are set by Starr on call
109107
eventType="$sonarr_eventtype"
110108
fi
111109
if [ "$app" == "unknown" ]; then
112110
log_message "ERROR" "Unknown application type detected. Exiting."
113-
exit 1
111+
exit 2
114112
fi
115113
log_message "INFO" "Detected application: $app"
116114
}
@@ -142,14 +140,14 @@ validate_process() {
142140

143141
if [ -z "$filePath" ] && [ -z "$folderPath" ] && [ -z "$downloadID" ]; then
144142
log_message "ERROR" "Essential parameters missing. Exiting."
145-
exit 1
143+
exit 2
146144
fi
147145

148146
if [ -z "$downloadID" ]; then
149147
log_message "ERROR" "Download ID is missing. Checking if file path works for data/path based cross-seeding."
150148
if [[ -z "$filePath" && -z "$folderPath" ]]; then
151149
log_message "ERROR" "File and Folder paths are missing. Exiting."
152-
exit 1
150+
exit 2
153151
fi
154152
fi
155153
}
@@ -188,7 +186,7 @@ handle_operations() {
188186
;;
189187
*)
190188
log_message "ERROR" "Unrecognized client $clientID. Exiting."
191-
exit 1
189+
exit 2
192190
;;
193191
esac
194192

@@ -197,6 +195,9 @@ handle_operations() {
197195
echo "$unique_id" >>"$LOG_FILE"
198196
log_message "INFO" "Process completed successfully."
199197
else
198+
if [ "$xseed_resp" == "000" ]; then
199+
log_message "ERROR" "Process Timed Out. Exiting."
200+
fi
200201
log_message "ERROR" "Process failed with API response: $xseed_resp"
201202
exit 1
202203
fi

0 commit comments

Comments
 (0)