@@ -1003,6 +1003,48 @@ function applyPatch() {
1003
1003
return 0
1004
1004
}
1005
1005
1006
+ # # @fn runCurl
1007
+ # # @params ... commandline arguments to pass to curl
1008
+ # # @brief Run curl with chosen parameters and handle curl errors
1009
+ # # @details Runs curl with the provided parameters, whilst also capturing the output and extracting
1010
+ # # any error message, which is stored in the global variable __NET_ERRMSG. Function returns the return
1011
+ # # code provided by curl. The environment variable __curl_opts can be set to override default curl
1012
+ # # parameters, eg - timeouts etc.
1013
+ # # @retval curl return value
1014
+ function runCurl() {
1015
+ local params=(" $@ " )
1016
+ # add any user supplied curl opts - timeouts can be overridden as curl uses the last parameters given
1017
+ [[ -z " $__curl_opts " ]] && params+=($__curl_opts )
1018
+
1019
+ local cmd_err
1020
+ local ret
1021
+
1022
+ # get the last non zero exit status (ignoring tee)
1023
+ set -o pipefail
1024
+
1025
+ # set up additional file descriptor for stdin
1026
+ exec 3>&1
1027
+
1028
+ # capture stderr - while passing both stdout and stderr to terminal
1029
+ # curl like wget outputs the progress meter to stderr, so we will extract the error line later
1030
+ cmd_err=$( curl " ${params[@]} " 2>&1 1>&3 | tee /dev/stderr)
1031
+ ret=" $? "
1032
+
1033
+ # remove stdin copy
1034
+ exec 3>& -
1035
+
1036
+ set +o pipefail
1037
+
1038
+ # if there was an error, extract it and put in __NET_ERRMSG
1039
+ if [[ " $ret " -ne 0 ]]; then
1040
+ # as we also capture the curl progress output, extract the last line which contains the error
1041
+ __NET_ERRMSG=" ${cmd_err##* $' \n ' } "
1042
+ else
1043
+ __NET_ERRMSG=" "
1044
+ fi
1045
+ return " $ret "
1046
+ }
1047
+
1006
1048
# # @fn download()
1007
1049
# # @param url url of file
1008
1050
# # @param dest destination name (optional), use - for stdout
@@ -1030,43 +1072,21 @@ function download() {
1030
1072
printMsgs " console" " Downloading $url to $dest ..."
1031
1073
params+=(-o " $dest " )
1032
1074
fi
1033
- params+=(--connect-timeout 60 --speed-limit 1 --speed-time 60)
1034
- # add any user supplied curl opts - timeouts can be overridden as curl uses the last parameters given
1035
- [[ -z " $__curl_opts " ]] && params+=($__curl_opts )
1075
+ params+=(--connect-timeout 10 --speed-limit 1 --speed-time 60)
1036
1076
# add the url
1037
1077
params+=(" $url " )
1038
1078
1039
- local cmd_err
1040
1079
local ret
1041
-
1042
- # get the last non zero exit status (ignoring tee)
1043
- set -o pipefail
1044
-
1045
- # capture stderr - while passing both stdout and stderr to terminal
1046
- # curl like wget outputs the progress meter to stderr, so we will extract the error line later
1047
-
1048
- # set up additional file descriptor for stdin
1049
- exec 3>&1
1050
-
1051
- cmd_err=$( curl " ${params[@]} " 2>&1 1>&3 | tee /dev/stderr)
1080
+ runCurl " ${params[@]} "
1052
1081
ret=" $? "
1053
1082
1054
- # remove stdin copy
1055
- exec 3>& -
1056
-
1057
- set +o pipefail
1058
-
1059
1083
# if download failed, remove file, log error and return error code
1060
1084
if [[ " $ret " -ne 0 ]]; then
1061
1085
# remove dest if not set to stdout and exists
1062
1086
[[ " $dest " != " -" && -f " $dest " ]] && rm " $dest "
1063
- # as we also capture the curl progress output, extract the last line which contains the error
1064
- cmd_err=" ${cmd_err##* $' \n ' } "
1065
-
1066
- md_ret_errors+=(" URL $url failed to download.\n\n$cmd_err " )
1067
- return " $ret "
1087
+ md_ret_errors+=(" URL $url failed to download.\n\n$__NET_ERRMSG " )
1068
1088
fi
1069
- return 0
1089
+ return " $ret "
1070
1090
}
1071
1091
1072
1092
# # @fn downloadAndVerify()
@@ -1546,6 +1566,21 @@ function getIPAddress() {
1546
1566
[[ -n " $ip_route " ]] && grep -oP " src \K[^\s]+" <<< " $ip_route"
1547
1567
}
1548
1568
1569
+ # # @fn isConnected()
1570
+ # # @brief Simple check to see if there is a connection to the Internet.
1571
+ # # @details Uses the getIPAddress function to check if we have a route to the Internet. Also sets
1572
+ # # __NET_ERRMSG with an error message for use in packages / setup to display to the user if not.
1573
+ # # @retval 0 on success
1574
+ # # @retval 1 on failure
1575
+ function isConnected() {
1576
+ local ip=" $( getIPAddress) "
1577
+ if [[ -z " $ip " ]]; then
1578
+ __NET_ERRMSG=" Not connected to the Internet"
1579
+ return 1
1580
+ fi
1581
+ return 0
1582
+ }
1583
+
1549
1584
# # @fn adminRsync()
1550
1585
# # @param src src folder on local system - eg "$__tmpdir/stats/"
1551
1586
# # @param dest destination folder on remote system - eg "stats/"
0 commit comments