From 4b4df3132addee441cb7b87538c65cfbb849fecb Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Mon, 13 Jan 2020 11:48:15 +0100 Subject: [PATCH] New api function to manage seq configurations (create/use) Seq template is generated by sequencer.sh; deleting static template --- README.md | 4 +- seqTemplate.sh | 53 ----------------------- sequencer/sequencer.sh | 98 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 94 insertions(+), 61 deletions(-) delete mode 100755 seqTemplate.sh diff --git a/README.md b/README.md index de0cd98..78072ff 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,6 @@ Contains sequences (seqs) for different tools, servers or occasions. Sequencer include working on sh and bash. [...] - . ./sequencer.sh + . /usr/local/bin/sequencer.sh -see [seqTemplate.sh](https://winklerfamilie.eu/git/efelon/shell_sequencer/src/branch/master/seqTemplate.sh) \ No newline at end of file +see seqTemplateExample.sh (which can be generated when calling sequencer.sh directly) diff --git a/seqTemplate.sh b/seqTemplate.sh deleted file mode 100755 index bdc2654..0000000 --- a/seqTemplate.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash - -## -## Sequencer interface: -## - step_[1 - 255]_info [STEP NUMBER] -## functions providing short header for help and user interaction -## - step_[1 - 255]_alias -## (optional) provide a string as alias for this step number -## - step_[1 - 255] [STEP NUMBER] -## performing custom shell operations -## - -step_1_info() { echo "Step $1 header"; } -step_1_alias() { ALIAS="begin"; } -step_1() { - exe cat '.no f"le' - saveReturn $? -} - -step_2_info() { echo "Step $1 header"; } -step_2() { - echo -e "Zwei" - endReturn - echo zwo -} - -step_3_info() { echo "Step $1 header"; } -step_3() { - echo drei -} - -step_10_info() { echo "Step $1 header"; } -step_10() { - echo zehn -} - -step_11_info() { echo "Step $1 header"; } -step_11_alias() { ALIAS="elf"; } -step_11() { - echo elf -} - -# Sequence Revision -VERSION_SEQREV=3 - -# Workaround when called from different directory -# Not needed when path to sequencer is absolut -# -# It is recommended to copy or link sequencer.sh to -# /usr/local/bin -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )" -# Path to local sequencer.sh script -. ${DIR}/./sequencer/sequencer.sh diff --git a/sequencer/sequencer.sh b/sequencer/sequencer.sh index 8ba6bb6..7d1b1c9 100755 --- a/sequencer/sequencer.sh +++ b/sequencer/sequencer.sh @@ -7,8 +7,8 @@ ## Version information -VERSION_REV=9 -VERSION_MAJOR=2 +VERSION_REV=10 +VERSION_MAJOR=0 VERSION_MINOR=0 ## Start of generic script part @@ -21,6 +21,9 @@ ERNO=0 STEP_ARGS= MAX_STEP=512 ALIAS= +SEQ_CONFIG_NAME=".seqs" +SEQ_CONFIG_HOME="$HOME/$SEQ_CONFIG_NAME" +SEQ_CONFIG_FILE= TEMPLATE_NAME=seqTemplateExample.sh MISSING_CONF=missingConf.log VERSION_STRING="${VERSION_REV}.${VERSION_MAJOR}.${VERSION_MINOR}" @@ -74,6 +77,11 @@ helpApi() { echo " \$DRY" echo " 0 : default" echo " 1 (-d) : Commands shall only be printed but not executed" + echo " \$SEQ_CONFIG_HOME" + echo " Path to user specific seq configuration directory" + echo " \$SEQ_CONFIG_FILE" + echo " Path to user specific seq configuration file" + echo " Will be empty if unused" echo echo "sequencer.sh build-in functions:" echo @@ -88,6 +96,16 @@ helpApi() { echo " - Shell commands cd, read, ... won't work because COMMANDLINE is started in a new shell." echo " - All apostrophes need to be esacped since the command line is given as string." echo + echo " initSeqConfig [OPTION] [TEMPLATE]" + echo " Create a configuration file in $SEQ_CONFIG_HOME/ and source it if already existent." + echo " [OPTION]" + echo " -t : Source config also if created from template" + echo " Returns" + echo " 0 : Sourced configuration or" + echo " (-t) : Created and sourced configuration from template" + echo " 1 : Created configuration from template but not sourced" + echo " 2 : or created empty configuration" + echo echo " addConf [SOURCE TYPE] " echo " Trying to write or append text or a file () to a destination file." echo " If the CONFIGFILE exists, a backup (name_%Y%m%d-%H%M%S.bck) is saved at the same location." @@ -254,6 +272,68 @@ endReturn() { fi } +# initSeqConfig [OPTION] [TEMPLATE] +# Create a configuration file in the users' home. +# Source it if already existent +# [OPTION] +# -t : Source config also if created from template +# Return +# 0 : Sourced configuration or +# (-t) : Created and sourced configuration from template +# 1 : Created configuration from template but not sourced +# 2 : or created empty configuration +initSeqConfig() { + local sourceAlways=0 + for arg in "$@" ; do + case "$1" in + -t) + sourceAlways=1 + shift + ;; + esac + done + + local configLoc="$SEQ_CONFIG_HOME/$1" + local configTemplate="$2" + + # Create config subdir in users home + if [ ! -e "$SEQ_CONFIG_HOME/" ] ; then + echo -n " [I] Creating $(realpath $SEQ_CONFIG_HOME)..." + exe mkdir -p "$SEQ_CONFIG_HOME" && echo "Ok" || echo "Nok" + fi + + if [ -s "$configLoc" ] ; then + echo " [I] Using configuration file: $configLoc" + SEQ_CONFIG_FILE="$configLoc" + . "$configLoc" + return 0 + fi + + # Config does not exist, check for template + if [ -s "$configTemplate" ] ; then + exe cp -ar "$configTemplate" "$configLoc" + endReturn -o $? "Failed to create configuration" + + if [ $sourceAlways -eq 0 ] ; then + echoerr " [W] Seq configuration created from template" + echoerr " Please modify "$configLoc" to your needs first" + return 1 + else + echo " [W] Using seq configuration from template $configTemplate" + SEQ_CONFIG_FILE="$configLoc" + . "$configLoc" + return 0 + fi + else + echo " [W] Seq configuration template empty" + fi + + # Create empty config file + echo " [W] Created empty configuration file $configLoc" + exe touch "$configLoc" + return 2 +} + # addConf [FILE_MODE] # trying to write a file # if exists, one attempt is made to create bck file of it @@ -520,7 +600,7 @@ selection() { done } -# Creating a minimal step definition template +# Creating a minimal seq (step definition) template createTemplate() { if [ -f $TEMPLATE_NAME ] ; then return 1 @@ -532,13 +612,19 @@ createTemplate() { echo "# Get script working directory" >> $TEMPLATE_NAME echo "# (when called from a different directory)" >> $TEMPLATE_NAME echo "WDIR=\"\$( cd \"\$( dirname \"\${BASH_SOURCE[0]}\" )\" >>/dev/null 2>&1 && pwd )\"" >> $TEMPLATE_NAME - echo "CONFIG_FILE=\"\$WDIR/\${toolName}.cfg\"" >> $TEMPLATE_NAME - echo "CONFIG_FILE_DEFAULT=\"\${CONFIG_FILE}.example\"" >> $TEMPLATE_NAME + echo "CONFIG=0" >> $TEMPLATE_NAME + echo "CONFIG_FILE_NAME=\"\${toolName}.cfg\"" >> $TEMPLATE_NAME + echo "CONFIG_FILE_TEMPLATE=\"\$WDIR/\${CONFIG_FILE_NAME}.example\"" >> $TEMPLATE_NAME echo >> $TEMPLATE_NAME echo "step_config() {" >> $TEMPLATE_NAME echo " echo \"Called once before executing steps.\"" >> $TEMPLATE_NAME - echo " echo \"e.g. to source a config file:\"" >> $TEMPLATE_NAME + echo " ## e.g. to source a config file manually:" >> $TEMPLATE_NAME echo " #. \"\$CONFIG_FILE\"" >> $TEMPLATE_NAME + echo " ## or to use sequencer api:" >> $TEMPLATE_NAME + echo " #initSeqConfig \"\$CONFIG_FILE_NAME\" \"\$CONFIG_FILE_TEMPLATE\"" >> $TEMPLATE_NAME + echo " #if [ \$CONFIG -ne 0 ] ; then" >> $TEMPLATE_NAME + echo " # CONFIG=1" >> $TEMPLATE_NAME + echo " #fi" >> $TEMPLATE_NAME echo "}" >> $TEMPLATE_NAME echo >> $TEMPLATE_NAME echo "step_1_info() { echo \"My custom step\"; }" >> $TEMPLATE_NAME