From 8dcd03ebe1cc91325b1632e9c4e712cd5e1414aa Mon Sep 17 00:00:00 2001 From: levasseur Date: Mon, 25 Oct 2021 15:13:45 +0200 Subject: [PATCH] filefct.sh: mutualised file selection mechanisme --- lib/filefct.sh | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/filefct.sh b/lib/filefct.sh index de3331e..8c0e905 100644 --- a/lib/filefct.sh +++ b/lib/filefct.sh @@ -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