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