Merge lp://staging/~smoser/runurl/runurl.smoser into lp://staging/runurl

Proposed by Scott Moser
Status: Merged
Approved by: Eric Hammond
Approved revision: 13
Merged at revision: 13
Proposed branch: lp://staging/~smoser/runurl/runurl.smoser
Merge into: lp://staging/runurl
Diff against target: 103 lines (+36/-41)
1 file modified
runurl (+36/-41)
To merge this branch: bzr merge lp://staging/~smoser/runurl/runurl.smoser
Reviewer Review Type Date Requested Status
Eric Hammond Approve
Review via email: mp+18097@code.staging.launchpad.net
To post a comment you must log in.
Revision history for this message
Scott Moser (smoser) wrote :

Here are some cleanups (well, i think so) for runurl.

Revision history for this message
Eric Hammond (esh) wrote :

Looks good and continues to work based on my testing.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'runurl'
2--- runurl 2009-10-10 06:41:32 +0000
3+++ runurl 2010-01-26 19:15:22 +0000
4@@ -1,64 +1,59 @@
5-#!/bin/bash
6+#!/bin/sh
7 #
8 # runurl - Download a URL and run as a program, passing in arguments
9 #
10 # Copyright (C) 2009 Eric Hammond <ehammond@thinksome.com>
11 #
12
13+error() { echo "$@" 1>&2; }
14+fail() { [ $# -eq 0 ] || error "${BNAME}:" "$1"; exit ${2:-1}; }
15+debug() { [ "$DEBUG" = "0" ] || error "${BNAME}:" "$@"; }
16+cleanup() { [ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"; }
17+
18+DEBUG="0"
19+TEMP_D=""
20+BNAME=${0##*/}
21+
22 while [ $# -gt 0 ]; do
23 case $1 in
24- -\?|--help) help=1; shift 1 ;;
25- -d|--debug) debug=1; shift 1 ;;
26- -*) echo "$0: Unrecognized option: $1 (try --help)" >&2; exit 1 ;;
27- *) url=$1; shift 1; break ;;
28+ -\?|--help) pod2text "${0}"; exit 0;;
29+ -d|--debug) DEBUG=1; shift 1 ;;
30+ -*) fail "Unrecognized option: $1 (try --help)";;
31+ *) url="$1"; shift 1; break ;;
32 esac
33 done
34
35-if [ "$help" ]; then
36- pod2text $0
37- exit 1
38-fi
39-
40-if [ "$url" = "" ]; then
41- echo "$0: Missing URL specification (try --help)"
42- exit 1;
43-fi
44-
45-runfile=$(mktemp /tmp/runurl.XXXX)
46-chmod 700 $runfile
47-wgetfile="$runfile.wget"
48-
49-if [ $debug ]; then
50- echo "runurl: downloading $url" >&2
51-fi
52-
53-wget \
54- --retry-connrefused \
55- --tries=20 \
56- --output-document=$runfile \
57- --output-file=$wgetfile \
58+[ -n "$url" ] || fail "Missing URL specification (try --help)"
59+
60+trap cleanup 0
61+
62+TEMP_D=$(mktemp -d ${TEMPDIR:-/tmp}/${BNAME}.XXXXXX) &&
63+ runfile="${TEMP_D}/runfile" && wgetfile="${TEMP_D}/wget.out" ||
64+ fail "failed to make tempdir"
65+
66+debug "downloading $url"
67+
68+wget \
69+ --retry-connrefused \
70+ --tries=20 \
71+ "--output-document=$runfile" \
72+ "--output-file=$wgetfile" \
73 "$url"
74
75 wgetstatus=$?
76 if [ $wgetstatus != 0 ]; then
77 cat $wgetfile >&2
78- echo "runurl: wget failed: $wgetstatus" >&2
79- rm $runfile $wgetfile
80- exit $wgetstatus
81-fi
82-
83-if [ $debug ]; then
84- echo "runurl: running" >&2
85-fi
86-
87+ fail "wget failed: $wgetstatus" "${wgetstatus}"
88+fi
89+
90+chmod 700 "${runfile}" || fail "failed to change perms of ${runfile}"
91+
92+debug "running"
93 $runfile "$@"
94
95 exitstatus=$?
96-rm $runfile $wgetfile
97
98-if [ $debug ]; then
99- echo "runurl: exit status: $exitstatus" >&2
100-fi
101+debug "exit status $exitstatus"
102
103 exit $exitstatus
104

Subscribers

People subscribed via source and target branches

to all changes: