From 9a303b634f0224a2330f40f56b614c584fcfafcb Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Thu, 12 Dec 2019 23:49:16 +0100 Subject: [PATCH] Extended addConf with file support for SOURCE --- sequencer/sequencer.sh | 111 +++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 42 deletions(-) diff --git a/sequencer/sequencer.sh b/sequencer/sequencer.sh index 40cf10a..fcf8e14 100755 --- a/sequencer/sequencer.sh +++ b/sequencer/sequencer.sh @@ -74,8 +74,8 @@ 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 " addConf " - echo " Trying to write or append to a file." + 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." echo " If -s fails or -m, \"$(realpath "$MISSING_CONF")\" is created with the conflicts" echo " to be resolved by the user." @@ -84,8 +84,10 @@ helpApi() { echo " -a : append to existing file" echo " -s : skip if CONFIGFILE exists (no backup and entry in missing conf)" echo " -m : only add content to missing conf and warn user" - echo " " - echo " Text to be created or added to " + echo " [SOURCE TYPE]" + echo " -f : is a file" + echo " " + echo " Text or file (-f) to create or added to " echo " " echo " Target file to be created or modified." echo @@ -229,48 +231,67 @@ endReturn() { fi } -# addConf +# addConf [FILE_MODE] # trying to write a file # if exists, one attempt is made to create bck file of it # if all fails, a log file is created with the conflicts to be resolved by the user addConf() { local confMode="" - case "$1" in - -c) # create a new file - confMode="-c" - ;; - -a) # append to existing file - confMode="-a" - ;; - -s) # skip if CONFIGFILE exists - confMode="-s" - ;; - -m) # only add content to missing conf and warn user - confMode="-m" - ;; - *) # default - echoerr " [E] Parameter 1 (-a|-c|-m|-s) missing for addConf()" - exit 0; - ;; - esac + local transferCmd="echo" + + for arg in $@ ; do + case "$1" in + -c) # create a new file + confMode="-c" + shift + ;; + -a) # append to existing file + confMode="-a" + shift + ;; + -s) # skip if CONFIGFILE exists + confMode="-s" + shift + ;; + -m) # only add content to missing conf and warn user + confMode="-m" + shift + ;; + -f) # choose if source is a file or text + transferCmd="cat" + shift + ;; + *) # default + if [ "$confMode" == "" ] ; then + echoerr " [E] Parameter 1 (-a|-c|-m|-s) missing for addConf()" + exit 0; + fi + ;; + esac + done + + local source="$1" + local dest="$2" + + if [ "$transferCmd" == "cat" ] && [ ! -f "$source" ] ; then + echoerr " [E] Source: \"$source\" does not exist" + return 1; + fi + if [ "$dest" == "" ] ; then + echoerr " [E] Destination empty" + return 1; + fi if [ "$DRY" -ne 0 ] ; then - echo " [I] Writing $3...dry-run" + echo " [I] Writing $dest ...dry-run" return 0; fi - echo -n " [I] Writing $3..." + echo -n " [I] Writing $dest ..." if [ $confMode != "-m" ] ; then - # try writing config directly - if [ ! -f "$3" ] ; then - case "$confMode" in - -c|-s) - echo "$2" > "$3" - ;; - -a) - echo "$2" >> "$3" - ;; - esac + # try writing config directly if it doesn't exist + if [ ! -f "$dest" ] ; then + $transferCmd "$source" > "$dest" echo "ok" return 0 fi @@ -280,13 +301,13 @@ addConf() { echo "skipping (exists)" else # try backup existing config - local addConfBackup="${3}_`date +%Y%m%d-%H%M%S`.bck" + local addConfBackup="${dest}_`date +%Y%m%d-%H%M%S`.bck" if [ ! -f "$addConfBackup" ] ; then - cp -ar "$3" "$addConfBackup" + cp -ar "$dest" "$addConfBackup" if [ $confMode == "-c" ] ; then - echo "$2" > "$3" + $transferCmd "$source" > "$dest" else - echo "$2" >> "$3" + $transferCmd "$source" >> "$dest" fi echo -e "ok \n [I] Existing config saved to ${addConfBackup}" return 0 @@ -305,11 +326,17 @@ addConf() { echo -n "### " >> "$MISSING_CONF" date >> "$MISSING_CONF" fi - echo "#--- $3 ---" >> "$MISSING_CONF" - echo "$2" >> "$MISSING_CONF" + + local helpText="needs to be added manually" + if [ "$confMode" == "-s" ] ; then + helpText="not overwritten" + fi + + echo "#--- \"$dest\" $helpText (Option: $confMode) ---" >> "$MISSING_CONF" + $transferCmd "$source" >> "$MISSING_CONF" echo >> "$MISSING_CONF" - echoerr " [W] Check $(realpath "$MISSING_CONF") for configuration conflicts ($3)" + echoerr " [W] Check $(realpath "$MISSING_CONF") for configuration conflicts ($dest)" return 1 }