Introducing text alias for steps
This commit is contained in:
2019-04-08 14:44:31 +01:00
parent baef4e8d56
commit e6313d2c32
4 changed files with 139 additions and 71 deletions

View File

@@ -6,7 +6,7 @@
## Version information
VERSION_REV=1
VERSION_REV=2
VERSION_MAJOR=0
VERSION_MINOR=0
@@ -15,31 +15,32 @@ VERSION_MINOR=0
QUIET=0
ERNO=0
MAX_STEP=255
ALIAS=
TEMPLATE_NAME=seqTemplateExample.sh
VERSION_STRING="${VERSION_REV}.${VERSION_MAJOR}.${VERSION_MINOR}"
function helpSequencer() {
echo "Usage: ${0##*/} [Options] [Step Number(s)]"
helpSequencer() {
echo "Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS]"
echo
echo " [Options]"
echo " [OPTIONS]"
echo " --help, -h : Display help"
echo " --quiet, -q : Don't ask for permission to execute steps"
echo " If called without starting step number, only this help is shown"
echo " --version,-v : Display version of sequencer and revision of sequence"
echo
echo " [Step Number(s) 1-${MAX_STEP}]"
echo " Single step number : starting point of process"
echo " Multiple step numbers : execute only given steps"
echo " execute only one step with using special step 0"
echo " ( e.g. only execute step 4: $0 4 0 )"
echo " [STEP NUMBER(s) 1-${MAX_STEP} or ALIAS]"
echo " Single STEP or ALIAS : starting point of process"
echo " Multiple STEPS or ALIAS : execute only given steps"
echo " execute only one step with using special step 0"
echo " ( e.g. only execute step 4: $0 4 0 )"
}
# endCheckEmpty [VariableName] [DESCRIPTION]
# DESCRIPTION : Optional text for error
function endCheckEmpty() {
endCheckEmpty() {
local errorText=$1
eval 'local ref=$'$1
if [ ! -z "$2" ] ; then
errorText=$2
fi
@@ -49,14 +50,13 @@ function endCheckEmpty() {
fi
}
function existsFunction() {
existsFunction() {
local NOTFOUND=0
eval 'local ref=$'$1
declare -F $1 &>>/dev/null || NOTFOUND=1
return $NOTFOUND
}
function saveReturn() {
saveReturn() {
if [ $1 -ne 0 ] ; then
ERNO=$1
fi
@@ -64,7 +64,7 @@ function saveReturn() {
# endReturn [-f]
# -f : force exit with $ERNO without user input
function endReturn() {
endReturn() {
if [[ ( $ERNO -ne 0 && $QUIET -ne 0 ) || ( $ERNO -ne 0 && ! -z $1 && $1 == "-f" ) ]] ; then
echo
echo -e "[Error] Return value $ERNO detected.\nAborting installation."
@@ -76,14 +76,14 @@ function endReturn() {
read -p "End installation: y(default)/n? " answer
case $answer in
[nN])
echo
echo Continuing installation...
;;
echo
echo Continuing installation...
;;
*)
echo
echo Installation aborted
exit $ERNO;
;;
echo
echo Installation aborted
exit $ERNO;
;;
esac
fi
}
@@ -91,10 +91,10 @@ function endReturn() {
# execute [-q] <Step Number>
# -q: don't stop and don't report step functions which cannot be found
# execute given step_<Step Number> function
function execute() {
execute() {
local NOTFOUND=0
local NOREPORT=0
if [ $1 == "-q" ] ; then
NOREPORT=1
shift
@@ -121,23 +121,48 @@ function execute() {
read -p "Start: y/n(default)? " answer
case $answer in
[yY])
step_$1 $1
;;
step_$1 $1
;;
*)
echo Aborting installation at step $1
exit 1;
;;
echo Aborting installation at step $1
exit 1;
;;
esac
else
step_$1 $1
fi
}
# checkStep <Step Number or Alias>
# return 0 - for invalid step
# Check sanitiy of step number or
# Check if alias exists
checkStep() {
if (( $1 < 1 || $1 > $MAX_STEP )) ; then
eval 'local ref=$alias_'$1
if [ -z $ref ] || [ "$ref" == "$1" ] ; then
return 0
else
return $ref
fi
else
return $1
fi
}
# continous <Starting Step Number>
# (max $MAX_STEP)
# execute installation continously from given starting step
function continous() {
for ((i=$1; i<=${MAX_STEP}; i++)); do
continous() {
local step=0
checkStep $1
step=$?
if [[ $step == 0 ]] ; then
return 1
fi
for ((i=$step; i<=${MAX_STEP}; i++)); do
execute -q $i
local res=$?
if [ $res -ne 0 ] ; then
@@ -149,19 +174,22 @@ function continous() {
# selection <Step Number List (separated by space)>
# execute given step list
# e.g.: selection -q 1 4 12
function selection() {
selection() {
local step=0
for i in $@ ; do
# stop on step 0
if [ $i -eq 0 ] ; then
break
else
execute $i
fi
checkStep $i
step=$?
# stop on step 0
if [ $step -eq 0 ] ; then
break
else
execute $step
fi
done
}
# Creating a minimal step definition template
function createTemplate() {
createTemplate() {
if [ -f $TEMPLATE_NAME ] ; then
return 1
fi
@@ -169,19 +197,39 @@ function createTemplate() {
echo >> $TEMPLATE_NAME
echo "# Step 1 is mandatory" >> $TEMPLATE_NAME
echo "step_1_info() { echo \"My custom step \$1\"; }" >> $TEMPLATE_NAME
echo "step_1_alias() { ALIAS=\"begin\"; }" >> $TEMPLATE_NAME
echo "step_1() {" >> $TEMPLATE_NAME
echo " echo \"Doing something...\"" >> $TEMPLATE_NAME
echo "}" >> $TEMPLATE_NAME
echo >> $TEMPLATE_NAME
echo "VERSION_SEQREV=2" >> $TEMPLATE_NAME
echo ". $0" >> $TEMPLATE_NAME
chmod +x $TEMPLATE_NAME
return 0
}
# Parse alias functions "step_[STEP NUBER]_alias" to create
# back reference variable of schema:
# alias_[ALIAS]=[STEP NUMBER]
parseAlias() {
for ((i=1; i<=${MAX_STEP}; i++)); do
# Check for alias definition
existsFunction step_${i}_alias
if [ $? -ne 0 ] ; then
continue
fi
# Function writes global ALIAS variable
step_${i}_alias
eval 'alias_'$ALIAS'='$i
done
}
# Always display sequencer help and, if available, sequence help
function displayHelp() {
displayHelp() {
helpSequencer
# check if step definition exists by looking for step_1()
@@ -192,29 +240,39 @@ function displayHelp() {
read -p " Create a template now? y/n(default)? " answer
case $answer in
[yY])
createTemplate
if [ $? -eq 0 ] ; then
echo -e "\n $TEMPLATE_NAME created."
else
echo -e "\n $TEMPLATE_NAME exists...Nothing to do!"
fi
;;
createTemplate
if [ $? -eq 0 ] ; then
echo -e "\n $TEMPLATE_NAME created."
else
echo -e "\n $TEMPLATE_NAME exists...Nothing to do!"
fi
;;
*)
echo -e "\n Nothing to do!"
;;
echo -e "\n Nothing to do!"
;;
esac
exit 1;
else
echo " Step documentation:"
echo " Step (= alias) documentation:"
for ((i=1; i<=${MAX_STEP}; i++)); do
# Display step reference in help if step function exists
existsFunction step_${i}
if [ $? -ne 0 ] ; then
continue
continue
fi
printf ' Step %3s : ' $i
printf ' Step %3s ' $i
# Display alias if exists
existsFunction step_${i}_alias
if [ $? -eq 0 ] ; then
step_${i}_alias
echo " = $ALIAS"
printf ' : '
else
echo -n " : "
fi
# Display step help only if info function exists
existsFunction step_${i}_info
if [ $? -eq 0 ] ; then
@@ -227,7 +285,7 @@ function displayHelp() {
}
# showVersion
function showVersion() {
showVersion() {
echo "Sequencer ${VERSION_STRING}"
echo -n "Seq Revision "
if [ ! -z "${VERSION_SEQREV}" ] ; then
@@ -281,7 +339,9 @@ main() {
exit 1;
fi
fi
parseAlias
# check if more than one step is given and select execution mode
if [ ! -z $2 ] ; then
selection $@