Extended addConf with file support for SOURCE

This commit is contained in:
2019-12-12 23:49:16 +01:00
parent 790ba88d7d
commit 9a303b634f

View File

@@ -74,8 +74,8 @@ helpApi() {
echo " - Shell commands cd, read, ... won't work because COMMANDLINE is started in a new shell." 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 " - All apostrophes need to be esacped since the command line is given as string."
echo echo
echo " addConf <OPTIONS> <CONFIGTEXT> <CONFIGFILE>" echo " addConf <OPTIONS> [SOURCE TYPE] <SOURCE> <DESTINATION FILE>"
echo " Trying to write or append to a file." echo " Trying to write or append text or a file (<SOURCE>) 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 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 " If -s fails or -m, \"$(realpath "$MISSING_CONF")\" is created with the conflicts"
echo " to be resolved by the user." echo " to be resolved by the user."
@@ -84,8 +84,10 @@ helpApi() {
echo " -a : append to existing file" echo " -a : append to existing file"
echo " -s : skip if CONFIGFILE exists (no backup and entry in missing conf)" 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 " -m : only add content to missing conf and warn user"
echo " <CONFIGTEXT>" echo " [SOURCE TYPE]"
echo " Text to be created or added to <CONFIGFILE>" echo " -f : <SOURCE> is a file"
echo " <SOURCE>"
echo " Text or file (-f) to create or added to <DESTINATION FILE>"
echo " <CONFIGFILE>" echo " <CONFIGFILE>"
echo " Target file to be created or modified." echo " Target file to be created or modified."
echo echo
@@ -229,48 +231,67 @@ endReturn() {
fi fi
} }
# addConf <CONF_MODE> <CONFIGTEXT> <CONFIGFILE> # addConf <CONF_MODE> [FILE_MODE] <SOURCE> <DESTINATION_FILE>
# trying to write a file # trying to write a file
# if exists, one attempt is made to create bck file of it # 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 # if all fails, a log file is created with the conflicts to be resolved by the user
addConf() { addConf() {
local confMode="" local confMode=""
case "$1" in local transferCmd="echo"
-c) # create a new file
confMode="-c" for arg in $@ ; do
;; case "$1" in
-a) # append to existing file -c) # create a new file
confMode="-a" confMode="-c"
;; shift
-s) # skip if CONFIGFILE exists ;;
confMode="-s" -a) # append to existing file
;; confMode="-a"
-m) # only add content to missing conf and warn user shift
confMode="-m" ;;
;; -s) # skip if CONFIGFILE exists
*) # default confMode="-s"
echoerr " [E] Parameter 1 (-a|-c|-m|-s) missing for addConf()" shift
exit 0; ;;
;; -m) # only add content to missing conf and warn user
esac 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 if [ "$DRY" -ne 0 ] ; then
echo " [I] Writing $3...dry-run" echo " [I] Writing $dest ...dry-run"
return 0; return 0;
fi fi
echo -n " [I] Writing $3..." echo -n " [I] Writing $dest ..."
if [ $confMode != "-m" ] ; then if [ $confMode != "-m" ] ; then
# try writing config directly # try writing config directly if it doesn't exist
if [ ! -f "$3" ] ; then if [ ! -f "$dest" ] ; then
case "$confMode" in $transferCmd "$source" > "$dest"
-c|-s)
echo "$2" > "$3"
;;
-a)
echo "$2" >> "$3"
;;
esac
echo "ok" echo "ok"
return 0 return 0
fi fi
@@ -280,13 +301,13 @@ addConf() {
echo "skipping (exists)" echo "skipping (exists)"
else else
# try backup existing config # 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 if [ ! -f "$addConfBackup" ] ; then
cp -ar "$3" "$addConfBackup" cp -ar "$dest" "$addConfBackup"
if [ $confMode == "-c" ] ; then if [ $confMode == "-c" ] ; then
echo "$2" > "$3" $transferCmd "$source" > "$dest"
else else
echo "$2" >> "$3" $transferCmd "$source" >> "$dest"
fi fi
echo -e "ok \n [I] Existing config saved to ${addConfBackup}" echo -e "ok \n [I] Existing config saved to ${addConfBackup}"
return 0 return 0
@@ -305,11 +326,17 @@ addConf() {
echo -n "### " >> "$MISSING_CONF" echo -n "### " >> "$MISSING_CONF"
date >> "$MISSING_CONF" date >> "$MISSING_CONF"
fi 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" 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 return 1
} }