Skip to content

Commit

Permalink
Use clean_up_and_exit function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed Apr 24, 2017
1 parent 9669d74 commit 26d76b6
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions bin/cget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -201,52 +201,51 @@ location="$1"
# Initialization
#######################################################################

# determine the log file
if $quiet_mode; then
log_file=/dev/null
else
log_file="$CACHE_DIR/cget_log.txt"
local_opts="$local_opts -L $log_file"
fi

# create temporary subdirectory
/bin/mkdir "$tmp_dir"
status_code=$?
if [ $status_code -ne 0 ]; then
echo "ERROR: $script_name failed to create tmp dir ($status_code) $tmp_dir" >&2
exit 2
fi
$verbose_mode && print_log_message "$script_name creating temp dir: $tmp_dir" "$log_file"

# temporary file
tmp_file="${tmp_dir}/http_resource_$$"

# determine the log file
if $quiet_mode; then
log_file=/dev/null
else
log_file="$CACHE_DIR/cget_log.txt"
local_opts="$local_opts -L $log_file"
fi

#######################################################################
# Main processing
#######################################################################

# The print_log_message function is defined in core_lib.sh
# Functions print_log_message and clean_up_and_exit defined in core_lib.sh
# Function conditional_get defined in http_tools.sh

print_log_message "$script_name requesting resource: $location" "$log_file"

# The conditional_get function is defined in http_tools.sh
# get the resource
conditional_get $local_opts -d "$CACHE_DIR" -T "$tmp_dir" "$location" > "$tmp_file"
status_code=$?
if [ $status_code -ne 0 ]; then
# clean up and exit
/bin/rm -r "$tmp_dir"
if [ $status_code -gt 1 ]; then
echo "ERROR: $script_name failed to get resource: $location" >&2
echo "ERROR: $script_name failed to get resource ($status_code): $location" >&2
fi
exit $status_code
clean_up_and_exit -d "$tmp_dir" $status_code
fi

# output the resource
/bin/cat "$tmp_file"
status_code=$?
# clean up and exit
/bin/rm -r "$tmp_dir"
if [ $status_code -ne 0 ]; then
echo "ERROR: $script_name unable to cat output ($status_code)" >&2
exit 3
clean_up_and_exit -d "$tmp_dir" 3
fi

exit 0

0 comments on commit 26d76b6

Please sign in to comment.