Skip to content

Commit

Permalink
Adjust exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed Oct 29, 2016
1 parent 63bf820 commit f63f9c7
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions bin/cget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ display_help () {
This script retrieves and caches HTTP resources on disk.
A previously cached resource is retrieved via HTTP Conditional
GET [RFC 7232]. If the web server responds with HTTP 200 OK,
the resource is cached and written to stdout. If the web
the new resource is cached and written to stdout. If the web
server responds with 304 Not Modified, the cached resource
is output instead.
Expand All @@ -49,15 +49,17 @@ display_help () {
Option -h is mutually exclusive of all other options.
The default behavior of the script may be modified by using
The default behavior of the script is modified by using
option -F or -C, which are mutually exclusive. Force Output
Mode (option -F) forces the return of a fresh resource. The
resource is output on stdout if and only if the server
responds with 200. If the response is 304, an error is thrown.
responds with 200. If the response is 304, the script silently
fails with exit code 1.
Cache Only Mode (option -C) bypasses the GET request altogether
and goes directly to cache. If the resource resides in cache,
it is output on stdout, otherwise an error is thrown.
it is output on stdout, otherwise the script silently fails
with exit code 1.
LIBRARY
Expand Down Expand Up @@ -152,8 +154,8 @@ if [ ! -d "$CACHE_DIR" ]; then
/bin/mkdir "$CACHE_DIR"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $script_name failed to create dir: $CACHE_DIR" >&2
exit $exit_code
echo "ERROR: $script_name failed to create dir $CACHE_DIR (exit code: $exit_code)" >&2
exit 2
fi
fi
$verbose_mode && printf "$script_name using cache directory: %s\n" "$CACHE_DIR"
Expand All @@ -180,8 +182,8 @@ for lib_filename in $LIB_FILENAMES; do
source "$lib_file" >&2
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $script_name failed to source script $lib_file" >&2
exit $exit_code
echo "ERROR: $script_name failed to source script $lib_file (exit code: $exit_code)" >&2
exit 2
fi
done

Expand All @@ -203,10 +205,10 @@ else
fi
if [ ! -d "$TMP_DIR" ]; then
/bin/mkdir "$TMP_DIR"
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "ERROR: $script_name failed to create dir: $TMP_DIR" >&2
exit $exit_status
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $script_name failed to create dir $TMP_DIR (exit code: $exit_code)" >&2
exit 2
fi
fi

Expand All @@ -217,14 +219,20 @@ tmp_file="${TMP_DIR}/http_resource_$$.xml"
# Main processing
#######################################################################

# get the resource
# The conditional_get function is defined and documented
# in library file http_tools.sh.
#
$verbose_mode && printf "$script_name requesting resource: %s\n" "$location"
conditional_get $local_opts -d "$CACHE_DIR" -t "$TMP_DIR" "$location" > "$tmp_file"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $script_name failed to get resource: $location" >&2
printf "See output log: %s\n" "$TMP_DIR/$conditional_get_log" >&2
if [ $exit_code -gt 1 ]; then
echo "ERROR: $script_name failed to get resource: $location" >&2
printf "See output log: %s\n" "$TMP_DIR/$conditional_get_log" >&2
fi
exit $exit_code
fi
$verbose_mode && printf "$script_name successfully obtained resource: %s\n" "$tmp_file"

/bin/cat "$tmp_file"
exit 0

0 comments on commit f63f9c7

Please sign in to comment.