-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.subr
86 lines (72 loc) · 2.01 KB
/
fetch.subr
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
if [ ! "$_CBSD_FETCH_SUBR" ]; then
_CBSD_FETCH_SUBR=1
###
# Wrapper for fetch and dpv
# fetchme -u URL -o /tmp/out
# use optional TF variable for output ( legacy from repo )
# require: cbsdsh as shell
# include: tools for humanize, DIALOG env for dpv
fetchme()
{
local _DPV="/usr/bin/dpv"
local _res
local _out="${TF}"
local _quiet=0
local _url=
local _TONULL
local _filename
local _fromrepo
# global variable. Need for most dialog-based action
[ -n "${quiet}" ] && _quiet="${quiet}"
local _FETCH_CMD="/usr/bin/fetch"
while getopts "u:q:o:" opt; do
case "$opt" in
u) _url="${OPTARG}" ;;
q) _quiet="${OPTARG}" ;;
o) _out="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
[ -z "${_url}" ] && echo "Empty URL" && return 1
local _sizebyte=
local _humansize=
# We use size output for non-quiet and for dpv(1) mode
[ ${_quiet} -eq 0 ] && _sizebyte=$( ${_FETCH_CMD} -qs ${_url} 2>/dev/null| /usr/bin/awk '{printf $1}' )
# test for human
if [ -n "${_sizebyte}" ]; then
if ! is_number ${_sizebyte}; then
if conv2human "${_sizebyte}"; then
local _humansize="${convval}"
else
unset _sizebyte
fi
else
unset _sizebyte
fi
fi
[ ! -f "${_DPV}" -o -z "${DIALOG}" ] && unset _DPV
if [ ${_quiet} -eq 1 ]; then
unset _DPV
_FETCH_CMD="${_FETCH_CMD} -q"
fi
_filename=$( /usr/bin/basename ${_url} 2>&1 )
[ -z "${_filename}" ] && _filename="Resource"
_fromrepo=$( echo ${_url} | /usr/bin/cut -d "/" -f 3 )
if [ -n "${_DPV}" ]; then
${_FETCH_CMD} -q -o '-' ${_url}| ${_DPV} -b "${product}" -p "Fetching from ${_fromrepo}:" -t " CBSD Repository " -x "/bin/cat > ${_out}" ${_sizebyte}:${_filename}
# check for size, cause "fetch | dpv " is always true
if [ -s "${_out}" ]; then
_res=0
else
/bin/rm -f ${_out}
_res=1
fi
else
[ -n "${_humansize}" ] && ${ECHO} "${MAGENTA}Retrieve ${LCYAN}${_filename}${MAGENTA} from ${LYELLOW}${_fromrepo}${MAGENTA}, size: ${GREEN}${_humansize}${NORMAL}"
${_FETCH_CMD} -o ${_out} ${_url} ${_TONULL}
_res=$?
fi
return ${_res}
}
###
fi