Repository navigation
Expand file tree
/
Copy pathdownload.sh
More file actions
87 lines (82 loc) · 2.42 KB
/
Copy pathdownload.sh
File metadata and controls
87 lines (82 loc) · 2.42 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
# scriptlet:_common/cmd_exists.sh
# scriptlet:bz_eval_log/log.sh
##
# Simple download utility function
#
# Uses either cURL or wget based on which is available
#
# Downloads the file to a temp location initially, then moves it to the final destination
# upon a successful download to avoid partial files.
#
# Returns 0 on success, 1 on failure
#
# Arguments:
# --no-overwrite Skip download if destination file already exists
#
# Examples:
#
# Download URL to local file
# download "https://example.tld/file.dat" "file.dat"
#
# Test downloading was successful
# if download "https://example.tld/file.dat" "file.dat"; then
# # download was successful; do some operation
# fi
#
# CHANGELOG:
# 2026.04.30 - Use logging with new logging interface
# 2026.04.21 - Add retry in curl to retry on connection issues, (looking at you Github)
# 2025.12.15 - Use cmd_exists to fix regression bug
# 2025.12.04 - Add --no-overwrite option to allow skipping download if the destination file exists
# 2025.11.23 - Download to a temp location to verify download was successful
# - use which -s for cleaner checks
# 2025.11.09 - Initial version
#
function download() {
# Argument parsing
local SOURCE="$1"
local DESTINATION="$2"
local OVERWRITE=1
local TMP=$(mktemp)
shift 2
while [ $# -ge 1 ]; do
case $1 in
--no-overwrite)
OVERWRITE=0
;;
esac
shift
done
if [ -z "$SOURCE" ] || [ -z "$DESTINATION" ]; then
log_error "download: Missing required parameters!"
return 1
fi
if [ -f "$DESTINATION" ] && [ $OVERWRITE -eq 0 ]; then
log_info "download: Destination file $DESTINATION already exists, skipping download."
return 0
fi
if cmd_exists curl; then
log_debug "download: Attempting to curl download $SOURCE"
if curl --connect-timeout 10 --retry 3 --retry-delay 10 -fsL "$SOURCE" -o "$TMP"; then
log_debug "download: Download successful, moving file to $DESTINATION"
mv $TMP "$DESTINATION"
return 0
else
log_error "download: curl failed to download $SOURCE"
return 1
fi
elif cmd_exists wget; then
log_debug "download: Attempting to wget download $SOURCE"
if wget -q "$SOURCE" -O "$TMP"; then
log_debug "download: Download successful, moving file to $DESTINATION"
mv $TMP "$DESTINATION"
return 0
else
log_error "download: wget failed to download $SOURCE"
return 1
fi
else
log_error "download: Neither curl nor wget is installed, cannot download!"
return 1
fi
}