Skip to content

Commit

Permalink
Converted to built in test condition syntax.
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
Alex Honor committed Oct 26, 2012
1 parent 5551266 commit 343bb3c
Showing 1 changed file with 55 additions and 45 deletions.
100 changes: 55 additions & 45 deletions rerun
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ echo $(tput sgr 0 1)"Examples:$(tput sgr0)
txtrst () { tput sgr0 ; }
bold() { echo -e "\033[1m$*\033[0m" ; txtrst ; }
dim() { tput dim ; echo " $*" ; txtrst ; }
[ -n "$RERUN_COLOR" ] && {
[[ -n "$RERUN_COLOR" ]] && {
ul="\033[4m" ; _ul="\033[0m" ; # underline
gray="\033[38;5;238m" ; _gray="\033[0m" ; # gray
red="\033[31m" ; _red="\033[0m" ; # red
Expand All @@ -80,17 +80,17 @@ txtrst
# already exist.
# Display the man page with `nroff`.
man() {
[ ! $# = 1 ] && {
[[ ! $# = 1 ]] && {
die "wrong # args: should be: man module"
}
local module=$1
if [ ! -f "$RERUN_MODULES/$module/$module.1" \
-a -f "$RERUN_MODULES/stubbs/commands/docs/default.sh" ]
if [[ ! -f "$RERUN_MODULES/$module/$module.1"
&& -f "$RERUN_MODULES/stubbs/commands/docs/default.sh" ]]
then $RERUN stubbs:docs --module $module
fi
if [ -f "$RERUN_MODULES/$module/$module.1" ]
if [[ -f "$RERUN_MODULES/$module/$module.1" ]]
then nroff -man "$RERUN_MODULES/$module/$module.1" | ${PAGER:-more}
else echo "Manual could not be generated." >&2
else echo >&2 "Manual could not be generated."
fi
}

Expand Down Expand Up @@ -127,13 +127,13 @@ rerun_syntax_check() {
# If the user created a script named after the OS,
# then print that path otherwise print the default one.
rerun_resolveCommandScript() {
[ ! $# -eq 2 ] && {
[[ ! $# -eq 2 ]] && {
die 'wrong # args: should be: rerun_resolveCommandScript module command'
}
local module_dir=$1 command=$2
local command_dir=$module_dir/commands/${command}
local os=$(uname -s)
if [ -f "$command_dir/${os}.sh" ]
if [[ -f "$command_dir/${os}.sh" ]]
then echo $command_dir/${os}.sh;
else echo $command_dir/default.sh;
fi
Expand All @@ -142,11 +142,11 @@ rerun_resolveCommandScript() {
# Checks if handler script exists (default or otherwise).
# return 0 if exists, 1 otherwise
rerun_existsCommandScript() {
[ ! $# -eq 2 ] && {
[[ ! $# -eq 2 ]] && {
die 'wrong # args: should be: rerun_existsCommandScript module_dir command'
}
local module_dir=$1 command=$2
if [ -f "$(rerun_resolveCommandScript $module_dir $command)" ]
if [[ -f "$(rerun_resolveCommandScript $module_dir $command)" ]]
then return 0
else return 1
fi
Expand All @@ -155,20 +155,20 @@ rerun_existsCommandScript() {
# Check if module exists.
# It's a module if it has a `metadata` file in the subdirectory.
rerun_existsModule() {
[ ! $# -eq 1 ] && {
[[ ! $# -eq 1 ]] && {
die 'wrong # args: should be: rerun_existsModule module'
}
local module=$1

# Give precedence to finding the module in $RERUN_MODULES:
if [ -f "$RERUN_MODULES/$module/metadata" ]
if [[ -f "$RERUN_MODULES/$module/metadata" ]]
then
echo $RERUN_MODULES/$module
return 0
fi

# If running an installed version of Rerun check the system location for the module:
if [ "${RERUN_LOCATION}" = "/usr/bin" -a -f "/usr/lib/rerun/modules/$module/metadata" ]
if [[ "${RERUN_LOCATION}" = "/usr/bin" && -f "/usr/lib/rerun/modules/$module/metadata" ]]
then
echo /usr/lib/rerun/modules/$module
return 0
Expand All @@ -180,11 +180,11 @@ rerun_existsModule() {

# Extract the log data from the replay log file.
rerun_extractLog() {
[ ! $# -eq 1 ] && {
[[ ! $# -eq 1 ]] && {
die 'wrong # args: should be: rerun_extractLog file'
}
local file=$1
[ -f $file ] || {
[[ -f $file ]] || {
die "file does not exist: $file"
}
local SIZE=$(awk '/^__COMMAND_OUT_BELOW__/ {print NR + 1; exit 0; }' $file) || die "failed sizing log"
Expand All @@ -194,7 +194,7 @@ rerun_extractLog() {
# Lookup a metadata property value.
rerun_metadataLookup() {
local field=$1 file=$2
[ ! -r $file ] && { echo "file not found: $file" ; return 1 ; }
[[ ! -r $file ]] && { echo "file not found: $file" ; return 1 ; }
while read line
do
key=${line%%=*}
Expand Down Expand Up @@ -226,7 +226,7 @@ EOF

# given a command set options based on answer file
answerOptions() {
[ $# -lt 2 ] && {
[[ $# -lt 2 ]] && {
echo "usage: answerOptions: module command ?answers?" >&2
return 2
}
Expand All @@ -235,13 +235,13 @@ answerOptions() {
do
option=$(
. $metadata; # source the option metadata
[ -n "$LONG" ] && flag="$LONG" || flag="$NAME"
if [ "$ARGUMENTS" = "true" -a -r "$3" ]
[[ -n "$LONG" ]] && flag="$LONG" || flag="$NAME"
if [[ "$ARGUMENTS" = "true" && -r "$3" ]]
then
var=$(echo $NAME | tr "[a-z]" "[A-Z]")
arg=$(awk -F= "/^$var/ {print \$2}" $3)
fi
[ -n "$arg" ] && printf -- "--%s %s" "$flag" "$arg"
[[ -n "$arg" ]] && printf -- "--%s %s" "$flag" "$arg"
)
argline="$argline $option"
done
Expand All @@ -258,17 +258,21 @@ answerOptions() {
# modules located in /usr/lib/rerun/modules are appended to the modules in RERUN_MODULES.
RERUN_LOCATION="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"

# Check for the `RERUN_MODULES` environment variable
[ -n "$RERUN_MODULES" ] || {
if [ "$RERUN_LOCATION" = "/usr/bin" ]
# Check for the `RERUN_MODULES` environment variable.
# If it is not set, then default it to either the system
# install location or relative to the rerun executable.
# TODO: add a unit test for this.
if [[ -z "$RERUN_MODULES" ]]
then
if [[ "$RERUN_LOCATION" = "/usr/bin" ]]
then
# Linux FSH convention:
RERUN_MODULES="/usr/lib/rerun/modules"
else
# Set module directory relative to the `rerun` script:
RERUN_MODULES=${RERUN_LOCATION}/modules
fi
}
fi

# Clear MODULE and COMMAND in case they were incidentally
# declared in the environment.
Expand Down Expand Up @@ -314,7 +318,7 @@ while [ "$#" -gt 0 ]; do
--answers)
rerun_syntax_check "$#" "$1"
ANSWERS="$2"
[ ! -f $ANSWERS ] && rerun_syntax_error "answers file not found: $ANSWERS"
[[ ! -f $ANSWERS ]] && rerun_syntax_error "answers file not found: $ANSWERS"
shift
;;
# Ignore remaining arguments as they are for the module.
Expand All @@ -325,7 +329,7 @@ while [ "$#" -gt 0 ]; do
done

# Replay requires that a log directory is also set.
[ -n "$REPLAY" -a -z "$RERUN_LOGS" ] && {
[[ -n "$REPLAY" && -z "$RERUN_LOGS" ]] && {
rerun_syntax_error "usage error: --replay <file> also requires -L <dir>"
}

Expand All @@ -347,7 +351,7 @@ fi
shift;

# Ensure the modules directory path is defined and is a directory.
[ -n "$RERUN_MODULES" -a -d "$RERUN_MODULES" ] || {
[[ -n "$RERUN_MODULES" && -d "$RERUN_MODULES" ]] || {
die "RERUN_MODULES directory not found or does not exist: $RERUN_MODULES"
}

Expand All @@ -368,17 +372,23 @@ shift;
# module's name and description.
# Modules are read from the directory referenced
# using the `$RERUN_MODULES` environment variable.
if [ -z "$MODULE" -a -z "$COMMAND" ]
if [[ -z "$MODULE" && -z "$COMMAND" ]]
then
echo -e $gray"Available modules in \"$RERUN_MODULES\":"$_gray
for module in $RERUN_MODULES/*; do
module_dir=$(rerun_existsModule $(basename $module)) && {
module_name=$(basename $module)
module_desc=$(rerun_metadataLookup DESCRIPTION $module/metadata)
shopt -s nullglob # enable
for module in $RERUN_MODULES/*
do
module_dir=$(rerun_existsModule $(basename "$module")) && {
module_name=$(basename "$module")
module_desc=$(rerun_metadataLookup DESCRIPTION "$module/metadata")
echo "${PAD}${module_name}: ${module_desc}"
}
done

# When rerun is installed in the system location
# and rerun modules is different to the system location,
# then list the system installed ones separately.
# TODO: add a unit test for these assumptions
if [[ $RERUN_LOCATION = "/usr/bin" && $RERUN_MODULES != "/usr/lib/rerun/modules" ]]
then
echo
Expand All @@ -400,7 +410,7 @@ fi
# If a module name is specified, show the command set.
# For each command, show that command's option list in summary form
# displaying requirement, name, flags, defaults and description.
if [ -n "$MODULE" -a -z "$COMMAND" ]
if [[ -n "$MODULE" && -z "$COMMAND" ]]
then
module_dir=$(rerun_existsModule "$MODULE") || rerun_syntax_error "module not found: \"$MODULE\""
echo -e $gray"Available commands in module, \"$module_dir\":"$_gray
Expand All @@ -409,25 +419,25 @@ then
do
cmd_name=$(basename $(dirname $cmd))
cmd_metadata=$module_dir/commands/${cmd_name}/metadata
[ -f "$cmd_metadata" ] && cmd_desc=$(rerun_metadataLookup DESCRIPTION $cmd_metadata)
[[ -f "$cmd_metadata" ]] && cmd_desc=$(rerun_metadataLookup DESCRIPTION $cmd_metadata)
bold "${cmd_name}: ${cmd_desc}"
if [ -d "$module_dir/commands/${cmd_name}" ]
if [[ -d "$module_dir/commands/${cmd_name}" ]]
then
shopt -s nullglob # enable
optfiles=( $module_dir/commands/${cmd_name}/*.option )
for opt_metadata in ${optfiles[*]}; do
cmd_param=$(basename $(echo ${opt_metadata%%.option}))
( argstring= summary=
. $opt_metadata ; # Read the option's metadata.
[ -n "${SHORT}" ] && {
[[ -n "${SHORT}" ]] && {
argstring=$(printf ' --%s|-%s' "$NAME" "$SHORT")
} || {
argstring=$(printf " --%s" "$NAME" )
}
[ "$ARGUMENTS" == "true" ] && {
[[ "$ARGUMENTS" == "true" ]] && {
argstring=$(printf "%s <${ul}%s${_ul}>" "$argstring" "$DEFAULT")
}
[ "$REQUIRED" != "true" ] && {
[[ "$REQUIRED" != "true" ]] && {
summary=$(printf "[%s]: \"%s\"" "${argstring}" "$DESCRIPTION")
} || {
summary=$(printf "%s: \"%s\"" "${argstring}" "$DESCRIPTION")
Expand All @@ -450,8 +460,8 @@ fi
# its output is extracted and compared to a new execution.

# The module and command must be specified for execution mode.
[ -z "$MODULE" ] && rerun_syntax_error "module not specified"
[ -z "$COMMAND" ] && rerun_syntax_error "command not specified"
[[ -z "$MODULE" ]] && rerun_syntax_error "module not specified"
[[ -z "$COMMAND" ]] && rerun_syntax_error "command not specified"

RERUN_MODULE_DIR=$(rerun_existsModule "$MODULE") || rerun_syntax_error "module not found: \"$MODULE\""

Expand All @@ -462,7 +472,7 @@ RERUN_MODULE_DIR=$(rerun_existsModule "$MODULE") || rerun_syntax_error "module n
}

# Read answer file and set positional parameters from them.
if [ -n "$ANSWERS" -a -f "$ANSWERS" ]
if [[ -n "$ANSWERS" && -f "$ANSWERS" ]]
then
set -- $(answerOptions $MODULE $COMMAND $ANSWERS)
fi
Expand All @@ -474,15 +484,15 @@ export RERUN RERUN_MODULES RERUN_MODULE_DIR
COMMAND_SCRIPT=$(rerun_resolveCommandScript $RERUN_MODULE_DIR $COMMAND)

# If the `-L` flag is set, record execution.
if [ -z "$RERUN_LOGS" -o ! -d "$RERUN_LOGS" ]
if [[ -z "$RERUN_LOGS" || ! -d "$RERUN_LOGS" ]]
then
# __Normal execution mode__
#

# Execute the command script if it is executable
# otherwise run it using the same interpreter as rerun.
# Return the command script's exit code as the result.
if [ -x $COMMAND_SCRIPT ]
if [[ -x $COMMAND_SCRIPT ]]
then
$COMMAND_SCRIPT "$@"
RETVAL=$?
Expand Down Expand Up @@ -527,14 +537,14 @@ else
# and show the diff output if execution output
# doesn't match the replay file content.

if [ -n "$REPLAY" ]
if [[ -n "$REPLAY" ]]
then
rerun_extractLog $REPLAY > $RERUN_LOGS/rerun-$$.checklog
rerun_extractLog $LOG > $RERUN_LOGS/rerun-$$.log

$DIFF $RERUN_LOGS/rerun-$$.checklog $RERUN_LOGS/rerun-$$.log > $RERUN_LOGS/rerun-$$.diff
RETVAL=$?
[ $RETVAL -eq 1 ] && {
[[ $RETVAL -eq 1 ]] && {
echo -e ${bold}"[diff]"${_bold}
cat $RERUN_LOGS/rerun-$$.diff
}
Expand Down

0 comments on commit 343bb3c

Please sign in to comment.