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 " - 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=""
|
||||||
|
local transferCmd="echo"
|
||||||
|
|
||||||
|
for arg in $@ ; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-c) # create a new file
|
-c) # create a new file
|
||||||
confMode="-c"
|
confMode="-c"
|
||||||
|
shift
|
||||||
;;
|
;;
|
||||||
-a) # append to existing file
|
-a) # append to existing file
|
||||||
confMode="-a"
|
confMode="-a"
|
||||||
|
shift
|
||||||
;;
|
;;
|
||||||
-s) # skip if CONFIGFILE exists
|
-s) # skip if CONFIGFILE exists
|
||||||
confMode="-s"
|
confMode="-s"
|
||||||
|
shift
|
||||||
;;
|
;;
|
||||||
-m) # only add content to missing conf and warn user
|
-m) # only add content to missing conf and warn user
|
||||||
confMode="-m"
|
confMode="-m"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-f) # choose if source is a file or text
|
||||||
|
transferCmd="cat"
|
||||||
|
shift
|
||||||
;;
|
;;
|
||||||
*) # default
|
*) # default
|
||||||
|
if [ "$confMode" == "" ] ; then
|
||||||
echoerr " [E] Parameter 1 (-a|-c|-m|-s) missing for addConf()"
|
echoerr " [E] Parameter 1 (-a|-c|-m|-s) missing for addConf()"
|
||||||
exit 0;
|
exit 0;
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user