Extended addConf with file support for SOURCE
This commit is contained in:
@@ -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 <OPTIONS> <CONFIGTEXT> <CONFIGFILE>"
|
||||
echo " Trying to write or append to a file."
|
||||
echo " addConf <OPTIONS> [SOURCE TYPE] <SOURCE> <DESTINATION 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 -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 " <CONFIGTEXT>"
|
||||
echo " Text to be created or added to <CONFIGFILE>"
|
||||
echo " [SOURCE TYPE]"
|
||||
echo " -f : <SOURCE> is a file"
|
||||
echo " <SOURCE>"
|
||||
echo " Text or file (-f) to create or added to <DESTINATION FILE>"
|
||||
echo " <CONFIGFILE>"
|
||||
echo " Target file to be created or modified."
|
||||
echo
|
||||
@@ -229,48 +231,67 @@ endReturn() {
|
||||
fi
|
||||
}
|
||||
|
||||
# addConf <CONF_MODE> <CONFIGTEXT> <CONFIGFILE>
|
||||
# addConf <CONF_MODE> [FILE_MODE] <SOURCE> <DESTINATION_FILE>
|
||||
# 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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user