filefct.sh: mutualised file selection mechanisme

This commit is contained in:
levasseur
2021-10-25 15:13:45 +02:00
parent 476aa67daa
commit 8dcd03ebe1

View File

@@ -58,6 +58,25 @@ backupdist()
export -f backupdist
# ------------------------------------------------------------------------------
# Select source file according to our priority mechanisme
select_file()
{
local infile=$1
if [[ -f $MYPATH/repo/hosts/$HOSTNAME/$infile ]]; then
local source="$MYPATH/repo/hosts/$HOSTNAME/$infile"
elif [[ -f $MYPATH/repo/common/$infile ]]; then
local source="$MYPATH/repo/common/$infile"
else
# Not found in repository, we expect full name
local source="$infile"
fi
unset infile
echo $source
unset source
}
# ------------------------------------------------------------------------------
# Install file to the host (specific first then general)
# Todo: implement wildcard support
@@ -77,14 +96,7 @@ installfile()
local arg=
for arg in $@; do
if [[ -f $MYPATH/repo/hosts/$HOSTNAME/$arg ]]; then
filelist="$filelist $MYPATH/repo/hosts/$HOSTNAME/$arg"
elif [[ -f $MYPATH/repo/common/$arg ]]; then
filelist="$filelist $MYPATH/repo/common/$arg"
else
# Not found in repository, we expect full name
filelist="$filelist $arg"
fi
filelist="$filelist $(select_file $arg)"
done
unset arg
@@ -121,29 +133,19 @@ export -f installfile
# Add the content of a file at the end of an other
appendfile()
{
local infile=$1
if [[ -f $MYPATH/repo/hosts/$HOSTNAME/$infile ]]; then
local source="$MYPATH/repo/hosts/$HOSTNAME/$infile"
elif [[ -f $MYPATH/repo/common/$infile ]]; then
local source="$MYPATH/repo/common/$infile"
else
# Not found in repository, we expect full name
local source="$infile"
fi
unset infile
local file=$2
if [[ -e $file ]]; then
local srcfile=$(select_file $1)
local dstfile=$2
if [[ -e $dstfile ]]; then
prnt E "appendfile(): Target must be on the root filesystem and full path must be provided."
die 13
fi
if [[ ! $file == /* ]]; then
if [[ ! $dstfile == /* ]]; then
prnt E "appendfile(): Target file must exist."
die 13
fi
prnt I "Ajout de contenu au fichier $file..."
cat $ource >> $file
prnt I "Ajout de contenu au fichier $dstfile..."
cat $srcfile >> $dstfile
if [[ $? -ne 0 ]]; then
prnt E "appendfile(): Couldn't append a file!"
die 12