Skip to content

Commit

Permalink
Clean up tmp dir, migrate to core lib, and implement logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed Apr 24, 2017
1 parent a84489f commit ff6b2f2
Showing 1 changed file with 100 additions and 93 deletions.
193 changes: 100 additions & 93 deletions bin/cget.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

#######################################################################
# Copyright 2016 Internet2
# Copyright 2016--2017 Internet2
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@ display_help () {
server responds with 304 Not Modified, the cached resource
is output instead.
Usage: ${0##*/} [-hv] [-F | -C] URL
Usage: ${0##*/} [-hvq] [-F | -C] URL
This script takes a single command-line argument. The URL
argument is the absolute URL of an HTTP resource. The script
Expand All @@ -43,7 +43,8 @@ display_help () {
Options:
-h Display this help message
-v Write verbose messages to stdout
-v Log verbose messages
-q Log no messages
-F Enables "Force Output Mode"
-C Enables "Cache Only Mode"
Expand Down Expand Up @@ -84,34 +85,92 @@ HELP_MSG

script_name=${0##*/} # equivalent to basename $0

# library filenames (always list command_paths first)
LIB_FILENAMES="command_paths.sh
compatible_mktemp.sh
# determine the source lib directory
if [ -z "$LIB_DIR" ]; then
echo "ERROR: $script_name requires env var LIB_DIR" >&2
exit 2
fi
if [ ! -d "$LIB_DIR" ]; then
echo "ERROR: $script_name: directory does not exist: $LIB_DIR" >&2
exit 2
fi

# library filenames (always list core_lib first)
LIB_FILENAMES="core_lib.sh
http_tools.sh"

# source lib files
for lib_filename in $LIB_FILENAMES; do
lib_file="$LIB_DIR/$lib_filename"
if [ ! -f "$lib_file" ]; then
echo "ERROR: $script_name: lib file does not exist: $lib_file" >&2
exit 2
fi
source "$lib_file"
status_code=$?
if [ $status_code -ne 0 ]; then
echo "ERROR: $script_name failed to source lib file ($status_code) $lib_file" >&2
exit 2
fi
done

# If env var TMPDIR exists, use it; otherwise
# create a new TMPDIR and use that instead.
if [ -z "$TMPDIR" ] || [ ! -d "$TMPDIR" ]; then
# create temporary directory
TMPDIR="$( make_temp_file -d )"
if [ ! -d "$TMPDIR" ] ; then
printf "ERROR: $script_name unable to create temporary dir\n" >&2
exit 2
fi
fi

# use TMPDIR directory (remove trailing slash)
tmp_dir="${TMPDIR%%/}/${script_name%%.*}_$$"

# every run of this script gets its own subdir
if [ -d "$tmp_dir" ]; then
echo "ERROR: $script_name: directory already exists: $tmp_dir" >&2
exit 2
fi

# determine the cache directory
if [ -z "$CACHE_DIR" ]; then
echo "ERROR: $script_name requires env var CACHE_DIR" >&2
exit 2
fi
if [ ! -d "$CACHE_DIR" ]; then
/bin/mkdir "$CACHE_DIR"
status_code=$?
if [ $status_code -ne 0 ]; then
echo "ERROR: $script_name failed to create dir $CACHE_DIR ($status_code)" >&2
exit 2
fi
fi

#######################################################################
# Process command-line options and arguments
#######################################################################

help_mode=false; verbose_mode=false; local_opts=
force_get_mode=false; cache_only_mode=false
while getopts ":hvFC" opt; do
help_mode=false; verbose_mode=false; quiet_mode=false; local_opts=
while getopts ":hvqFC" opt; do
case $opt in
h)
help_mode=true
;;
v)
verbose_mode=true
quiet_mode=false
local_opts="$local_opts -$opt"
;;
q)
verbose_mode=false
quiet_mode=true
;;
F)
force_get_mode=true
cache_only_mode=false
local_opts="$local_opts -$opt"
;;
C)
cache_only_mode=true
force_get_mode=false
local_opts="$local_opts -$opt"
;;
\?)
Expand All @@ -138,107 +197,55 @@ if [ $# -ne 1 ]; then
fi
location="$1"

$verbose_mode && printf "$script_name using location URL: %s\n" "$location"

#######################################################################
# Initialization
#######################################################################

# determine the cache directory
if [ -z "$CACHE_DIR" ]; then
echo "ERROR: $script_name requires env var CACHE_DIR" >&2
exit 2
fi
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 (exit code: $exit_code)" >&2
exit 2
fi
fi
$verbose_mode && printf "$script_name using cache directory: %s\n" "$CACHE_DIR"

# determine the source lib directory
if [ -z "$LIB_DIR" ]; then
echo "ERROR: $script_name requires env var LIB_DIR" >&2
exit 2
fi
if [ ! -d "$LIB_DIR" ]; then
echo "ERROR: $script_name: directory does not exist: $LIB_DIR" >&2
# 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 && printf "$script_name using source lib directory: %s\n" "$LIB_DIR"

# source lib files (always source command_paths first)
for lib_filename in $LIB_FILENAMES; do
lib_file="$LIB_DIR/$lib_filename"
if [ ! -f "$lib_file" ]; then
echo "ERROR: $script_name: file does not exist: $lib_file" >&2
exit 2
fi
$verbose_mode && printf "$script_name sourcing lib file: %s\n" "$lib_file"
source "$lib_file" >&2
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $script_name failed to source script $lib_file (exit code: $exit_code)" >&2
exit 2
fi
done
# temporary file
tmp_file="${tmp_dir}/http_resource_$$"

# determine temporary directory
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
# use system temporary directory (remove trailing slash)
TMP_DIR="${TMPDIR%%/}/${script_name%%.*}"
$verbose_mode && printf "$script_name using temp dir: %s\n" "$TMP_DIR"
# determine the log file
if $quiet_mode; then
log_file=/dev/null
else
# create temporary directory
tmp_dir="$( make_temp_file -d )"
if [ ! -d "$tmp_dir" ] ; then
printf "ERROR: $script_name unable to create temporary dir\n" >&2
exit 2
fi
# use temporary directory (remove trailing slash)
TMP_DIR="${tmp_dir%%/}/${script_name%%.*}"
$verbose_mode && printf "$script_name creating temp dir: %s\n" "$TMP_DIR"
fi
# create a subdirectory
if [ ! -d "$TMP_DIR" ]; then
/bin/mkdir "$TMP_DIR"
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
log_file="$CACHE_DIR/cget_log.txt"
local_opts="$local_opts -L $log_file"
fi

# temporary file
tmp_file="${TMP_DIR}/http_resource_$$.xml"

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

# 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
if [ $exit_code -gt 1 ]; then
# The print_log_message function is defined in core_lib.sh
print_log_message "$script_name requesting resource: $location" "$log_file"

# The conditional_get function is defined in http_tools.sh
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
printf "See output log: %s\n" "$TMP_DIR/$conditional_get_log" >&2
fi
exit $exit_code
exit $status_code
fi
$verbose_mode && printf "$script_name successfully obtained resource: %s\n" "$tmp_file"

# output the resource
/bin/cat "$tmp_file"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: ${script_name} unable to cat output with status code: $exit_code" >&2
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
fi

Expand Down

0 comments on commit ff6b2f2

Please sign in to comment.