Step help as own function within step definition script

fixes #1
This commit is contained in:
2019-04-05 12:06:47 +01:00
parent bfccac6b28
commit 17eaeee234
4 changed files with 107 additions and 120 deletions

View File

@@ -2,12 +2,13 @@
## Sequencer script is doing nothing on its own. It is included by a step definition
## script which uses the sequencer to provide sequencial operations with or without
## user interaction (see template.sh)
## user interaction (see stepTemplate.sh)
## Start of generic script part
QUIET=0
ERNO=0
MAX_STEP=255
TEMPLATE_NAME=stepTemplateExample.sh
function helpSequencer() {
@@ -17,7 +18,7 @@ function helpSequencer() {
echo " -q : Don't ask for permission to execute next step"
echo " If called without starting step number only this help is shown"
echo
echo " [Step Number(s) 1-255]"
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"
@@ -27,18 +28,25 @@ function helpSequencer() {
# endCheckEmpty [VariableName] [DESCRIPTION]
# DESCRIPTION : Optional text for error
function endCheckEmpty() {
local errorText=$1
eval 'local ref=$'$1
if [ ! -z "$2" ] ; then
errorText=$2
fi
if [ -z $ref ] ; then
echo -e "[Error] $errorText must not be empty.\nAborting installation."
exit 666
local errorText=$1
eval 'local ref=$'$1
if [ ! -z "$2" ] ; then
errorText=$2
fi
if [ -z $ref ] ; then
echo -e "[Error] $errorText must not be empty.\nAborting installation."
exit 666
fi
}
function existsFunction() {
local NOTFOUND=0
eval 'local ref=$'$1
declare -F $1 &>>/dev/null || NOTFOUND=1
return $NOTFOUND
}
function saveReturn() {
if [ $1 -ne 0 ] ; then
ERNO=$1
@@ -96,6 +104,10 @@ function execute() {
fi
echo -en "\n[STEP $1] "
existsFunction step_${1}_info
if [ $? -eq 0 ] ; then
step_${1}_info $1
fi
if [ $QUIET -ne 1 ] ; then
read -p "Start: y/n(default)? " answer
case $answer in
@@ -113,10 +125,10 @@ function execute() {
}
# continous <Starting Step Number>
# (max 255)
# (max $MAX_STEP)
# execute installation continously from given starting step
function continous() {
for ((i=$1; i<=255; i++)); do
for ((i=$1; i<=${MAX_STEP}; i++)); do
execute -q $i
local res=$?
if [ $res -ne 0 ] ; then
@@ -146,13 +158,10 @@ function createTemplate() {
fi
echo "#!/bin/bash" > $TEMPLATE_NAME
echo >> $TEMPLATE_NAME
echo "function step_1 {" >> $TEMPLATE_NAME
echo " echo \"My custom step one\"" >> $TEMPLATE_NAME
echo "}" >> $TEMPLATE_NAME
echo >> $TEMPLATE_NAME
echo "help() {" >> $TEMPLATE_NAME
echo " echo \" Step Documentation\"" >> $TEMPLATE_NAME
echo " echo \" 1: My custom step\"" >> $TEMPLATE_NAME
echo "# Step 1 is mandatory" >> $TEMPLATE_NAME
echo "step_1_info() { echo \"My custom step \$1\"; }" >> $TEMPLATE_NAME
echo "step_1() {" >> $TEMPLATE_NAME
echo " echo \"Doing something...\"" >> $TEMPLATE_NAME
echo "}" >> $TEMPLATE_NAME
echo >> $TEMPLATE_NAME
echo ". $0" >> $TEMPLATE_NAME
@@ -163,13 +172,12 @@ function createTemplate() {
# Always display sequencer help and, if available, definition script help
function displayHelp() {
local NOTFOUND=0
helpSequencer
# check if help function exists
declare -F help &>>/dev/null || NOTFOUND=1
if [ $NOTFOUND -eq 1 ] ; then
# check if step definition exists by looking for step_1()
existsFunction step_1
if [ $? -ne 0 ] ; then
echo -e "\n It seems ${0##*/} was called directly."
echo -e " Please create a step definition script first.\n"
read -p " Create a step definition template now? y/n(default)? " answer
@@ -188,7 +196,24 @@ function displayHelp() {
esac
exit 1;
else
help
echo " Step 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
fi
printf ' Step %3s : ' $i
# Display step help only if function exists
existsFunction step_${i}_info
if [ $? -eq 0 ] ; then
step_${i}_info $i
else
echo " - step_${i}_info() missing"
fi
done
fi
}