created var management lib, embryo removed from filefct lib

This commit is contained in:
2025-10-16 15:36:35 +02:00
parent 1132d20796
commit f7bdab1bdb
2 changed files with 192 additions and 133 deletions

View File

@@ -25,35 +25,35 @@ export COMM_REPO_PATH=${COMM_REPO_PATH:-"$MYPATH/repo/common"}
backup_dist() backup_dist()
{ {
if [[ $# -lt 1 ]]; then if [[ $# -lt 1 ]]; then
prnt E "backup_dist(): At least one argument is required." prnt E "backup_dist(): At least one argument is required."
exit 11 exit 11
fi fi
local file= local file=
for file in $@; do for file in $@; do
local tmstmp=$(stdtime) local tmstmp=$(stdtime)
if [[ -L ${file} ]]; then if [[ -L ${file} ]]; then
# With symbolik links we call again backup_dist to treat target # With symbolik links we call again backup_dist to treat target
prnt I "Following the symbolic link $file to do a proper backup..." prnt I "Following the symbolic link $file to do a proper backup..."
backup_dist $(readlink -f "${file}") backup_dist $(readlink -f "${file}")
elif [[ -f ${file} ]]; then elif [[ -f ${file} ]]; then
prnt I "Creating a backup of ${file} on $tmstmp..." prnt I "Creating a backup of ${file} on $tmstmp..."
cp -av $file ${file}.dist.${tmstmp} cp -av $file ${file}.dist.${tmstmp}
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "backup_dist(): Failed copying file." prnt E "backup_dist(): Failed copying file."
die 12 die 12
fi fi
elif [[ -d ${file} ]]; then elif [[ -d ${file} ]]; then
prnt I "Creating a backup of the directory ${file} on $tmstmp..." prnt I "Creating a backup of the directory ${file} on $tmstmp..."
cp -av $file ${file}.dist.${tmstmp} cp -av $file ${file}.dist.${tmstmp}
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "backup_dist(): Failed copying directory recursively." prnt E "backup_dist(): Failed copying directory recursively."
die 12 die 12
fi fi
else else
prnt W "backup_dist(): $file don't exists, nothing to do." prnt W "backup_dist(): $file don't exists, nothing to do."
fi fi
unset tmstmp unset tmstmp
done done
unset file unset file
} }
@@ -74,20 +74,20 @@ select_file()
{ {
local infile=$1 local infile=$1
if [[ -f $RLMHST_REPO_PATH/$infile ]]; then if [[ -f $RLMHST_REPO_PATH/$infile ]]; then
local source="$RLMHST_REPO_PATH/$infile" local source="$RLMHST_REPO_PATH/$infile"
elif [[ -f $RLMGRP_REPO_PATH/$infile ]]; then elif [[ -f $RLMGRP_REPO_PATH/$infile ]]; then
local source="$RLMGRP_REPO_PATH/$infile" local source="$RLMGRP_REPO_PATH/$infile"
elif [[ -f $HOST_REPO_PATH/$infile ]]; then elif [[ -f $HOST_REPO_PATH/$infile ]]; then
local source="$HOST_REPO_PATH/$infile" local source="$HOST_REPO_PATH/$infile"
elif [[ -f $GROUP_REPO_PATH/$infile ]]; then elif [[ -f $GROUP_REPO_PATH/$infile ]]; then
local source="$GROUP_REPO_PATH/$infile" local source="$GROUP_REPO_PATH/$infile"
elif [[ -f $REALM_REPO_PATH/$infile ]]; then elif [[ -f $REALM_REPO_PATH/$infile ]]; then
local source="$REALM_REPO_PATH/$infile" local source="$REALM_REPO_PATH/$infile"
elif [[ -f $COMM_REPO_PATH/$infile ]]; then elif [[ -f $COMM_REPO_PATH/$infile ]]; then
local source="$COMM_REPO_PATH/$infile" local source="$COMM_REPO_PATH/$infile"
else else
# Not found in repository, we expect full name # Not found in repository, we expect full name
local source="$infile" local source="$infile"
fi fi
unset infile unset infile
echo $source echo $source
@@ -103,20 +103,20 @@ select_directory()
{ {
local indir=$1 local indir=$1
if [[ -d $RLMHST_REPO_PATH/$indir ]]; then if [[ -d $RLMHST_REPO_PATH/$indir ]]; then
local source="$RLMHST_REPO_PATH/$indir" local source="$RLMHST_REPO_PATH/$indir"
elif [[ -d $RLMGRP_REPO_PATH/$indir ]]; then elif [[ -d $RLMGRP_REPO_PATH/$indir ]]; then
local source="$RLMGRP_REPO_PATH/$indir" local source="$RLMGRP_REPO_PATH/$indir"
elif [[ -d $HOST_REPO_PATH/$indir ]]; then elif [[ -d $HOST_REPO_PATH/$indir ]]; then
local source="$HOST_REPO_PATH/$indir" local source="$HOST_REPO_PATH/$indir"
elif [[ -d $GROUP_REPO_PATH/$indir ]]; then elif [[ -d $GROUP_REPO_PATH/$indir ]]; then
local source="$GROUP_REPO_PATH/$indir" local source="$GROUP_REPO_PATH/$indir"
elif [[ -d $REALM_REPO_PATH/$indir ]]; then elif [[ -d $REALM_REPO_PATH/$indir ]]; then
local source="$REALM_REPO_PATH/$indir" local source="$REALM_REPO_PATH/$indir"
elif [[ -d $COMM_REPO_PATH/$indir ]]; then elif [[ -d $COMM_REPO_PATH/$indir ]]; then
local source="$COMM_REPO_PATH/$indir" local source="$COMM_REPO_PATH/$indir"
else else
# Not found in repository, we expect full name # Not found in repository, we expect full name
local source="$indir" local source="$indir"
fi fi
unset indir unset indir
echo $source echo $source
@@ -135,41 +135,41 @@ install_file()
local i=0 local i=0
if [[ $# -lt 2 ]]; then if [[ $# -lt 2 ]]; then
prnt E "install_file(): At least two arguments are required." prnt E "install_file(): At least two arguments are required."
die 11 die 11
fi fi
if [[ -n $(echo $@ | grep "\*\|\?") ]]; then if [[ -n $(echo $@ | grep "\*\|\?") ]]; then
prnt E "install_file(): Wildcards are not authorized." prnt E "install_file(): Wildcards are not authorized."
die 7 die 7
fi fi
local arg= local arg=
for arg in $@; do for arg in $@; do
filelist="$filelist $(select_file $arg)" filelist="$filelist $(select_file $arg)"
# We always replace until the last argument being the target # We always replace until the last argument being the target
target="$arg" target="$arg"
done done
unset arg unset arg
if [[ ! $target == /* ]]; then if [[ ! $target == /* ]]; then
prnt E "install_file(): Target must be on the root filesystem and full path must be provided." prnt E "install_file(): Target must be on the root filesystem and full path must be provided."
die 13 die 13
fi fi
unset target unset target
if [[ -d $(dirname $i) ]]; then if [[ -d $(dirname $i) ]]; then
prnt I "Creating required target directory $(dirname $i)..." prnt I "Creating required target directory $(dirname $i)..."
mkdir -pv $(dirname $i) mkdir -pv $(dirname $i)
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "install_file(): Can't create target directory!" prnt E "install_file(): Can't create target directory!"
die 12 die 12
fi fi
fi fi
prnt I "Copying files ${filelist} to target directory $(dirname $i)..." prnt I "Copying files ${filelist} to target directory $(dirname $i)..."
cp -av $filelist cp -av $filelist
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "install_file(): Couldn't copy some required files!" prnt E "install_file(): Couldn't copy some required files!"
die 12 die 12
fi fi
} }
export -f install_file export -f install_file
@@ -181,26 +181,26 @@ export -f install_file
append_file() append_file()
{ {
if [[ $# -ne 2 ]]; then if [[ $# -ne 2 ]]; then
prnt E "append_file(): Two arguments are required, source and destination." prnt E "append_file(): Two arguments are required, source and destination."
die 11 die 11
fi fi
local srcfile=$(select_file $1) local srcfile=$(select_file $1)
local dstfile=$2 local dstfile=$2
if [[ ! $dstfile == /* ]]; then if [[ ! $dstfile == /* ]]; then
prnt E "append_file(): Target must be on the root filesystem and full path must be provided." prnt E "append_file(): Target must be on the root filesystem and full path must be provided."
die 13 die 13
fi fi
if [[ -e $dstfile ]]; then if [[ -e $dstfile ]]; then
prnt E "append_file(): Target file must exist (use touch first to create it if required)." prnt E "append_file(): Target file must exist (use touch first to create it if required)."
die 13 die 13
fi fi
prnt I "Adding content to file $dstfile..." prnt I "Adding content to file $dstfile..."
cat $srcfile >> $dstfile cat $srcfile >> $dstfile
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "append_file(): Couldn't append a file!" prnt E "append_file(): Couldn't append a file!"
die 12 die 12
fi fi
} }
export -f append_file export -f append_file
@@ -214,16 +214,16 @@ is_dir_empty()
dir=$1 dir=$1
if [[ -f $dir ]]; then if [[ -f $dir ]]; then
prnt E "is_dir_empty(): The given parameter is not a directory." prnt E "is_dir_empty(): The given parameter is not a directory."
die 15 die 15
fi fi
if [[ ! -d $dir ]]; then if [[ ! -d $dir ]]; then
return 0 return 0
fi fi
nbfiles=$(ls -a1 $dir | grep -Evc '^.$|^..$') nbfiles=$(ls -a1 $dir | grep -Evc '^.$|^..$')
if [[ $nbfiles -eq 0 ]]; then if [[ $nbfiles -eq 0 ]]; then
return 0 return 0
fi fi
return 1 return 1
} }
@@ -231,64 +231,17 @@ export -f is_dir_empty
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# copy and patch a file replacing all @var@ by the corresponding value in
# the environment or the variable list given in parameter
patch_file()
{
local srcfile=$(select_file $1) && shift
local dstfile=$1 && shift
local workfile=${dstfile}.work
if [[ ! -s $srcfile ]]; then
prnt E "patch_file(): Source file is empty, is not a file or don't exists!"
die 10
fi
# Create a sub-process, to avoid bash environment pollution
(
local varlist='' pattern=''
if [[ $# -eq 0 ]] ; then
pattern="-e s/<\(.*\)>/\$\1\$\1/g"
else
local var=
for var in $* ; do
if ! declare -p $var >/dev/null 2>&1 ; then
local $var=$(eval echo \$$var)
fi
pattern="$pattern -e s/@$var@/\$$var/g"
varlist=$varlist\$$var
done
fi
# sed replace <VAR> with \$$VAR and envsubst do the replace by value
sed $pattern $srcfile | envsubst ${varlist:+"$varlist"} > "$workfile"
)
local -a rights=( $(stat --printf="%a %u %g" "$srcfile") )
unset srcfile
mv "$workfile" "$dstfile"
chmod ${rights[0]} "$dstfile"
chown ${rights[1]}:${rights[2]} "$dstfile"
unset rights dstfile
}
export -f patch_file
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Put a small header in a file showing it have been automatically modified # Put a small header in a file showing it have been automatically modified
tag_file() tag_file()
{ {
for f in $@; do for f in $@; do
local text="# File automatically modified by init.sh on $(stdtime)." local text="# File automatically modified by init.sh on $(stdtime)."
if [[ -e $f ]]; then if [[ -e $f ]]; then
sed -i "1s/^/$text\n/" $f sed -i "1s/^/$text\n/" $f
else else
echo $text > $f echo $text | sed "s/modified/generated/" > $f
sed -i -e "s/modified/generated/" $f fi
fi
done done
} }
export -f tag_file export -f tag_file
@@ -300,10 +253,10 @@ export -f tag_file
file_exists() file_exists()
{ {
for f in $@; do for f in $@; do
if [[ ! -f $(select_file $f) ]]; then if [[ ! -f $(select_file $f) ]]; then
echo $f echo $f
return 1 return 1
fi fi
done done
return 0 return 0
} }
@@ -318,8 +271,8 @@ file_must_exists()
prnt I "Checking $@ files existance..." prnt I "Checking $@ files existance..."
local mf=$(file_exists $@) local mf=$(file_exists $@)
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "file_must_exists(): The $mf file is missing, can't continue." prnt E "file_must_exists(): The $mf file is missing, can't continue."
die 10 die 10
fi fi
unset mf unset mf
} }
@@ -332,10 +285,10 @@ export -f file_must_exists
directory_exists() directory_exists()
{ {
for d in $@; do for d in $@; do
if [[ ! -d $(select_directory $d) ]]; then if [[ ! -d $(select_directory $d) ]]; then
echo $d echo $d
return 1 return 1
fi fi
done done
return 0 return 0
} }
@@ -350,8 +303,8 @@ directory_must_exists()
prnt I "Checking $@ directories existance..." prnt I "Checking $@ directories existance..."
local md=$(directory_exists $@) local md=$(directory_exists $@)
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "directory_must_exists(): The $md directory is missing, can't continue." prnt E "directory_must_exists(): The $md directory is missing, can't continue."
die 10 die 10
fi fi
unset md unset md
} }

106
lib/vars.sh Normal file
View File

@@ -0,0 +1,106 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Variables substitution function
# This file is part of the init.sh project
# Copyright (c) 2019-2024 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Replace @VAR@ in a text file by the corresponding $VAR value
# The --delimiter or -d option allow to use something else than @
setvar() {
local delimiter="@"
local vars=()
local file
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--delimiter|-d)
shift
delimiter="${1:-@}"
;;
-*)
prnt E "setvar(): Unknown option: $1"
die
;;
*)
if [[ -f $1 && $# -eq 1 ]]; then
file="$1"
else
vars+=("$1")
fi
;;
esac
shift
done
if [[ -z $file ]]; then
prnt E "Usage: setvar [--delimiter D] VAR1 [VAR2 ...] <file>"
die 1
fi
if [[ ${#vars[@]} -eq 0 ]]; then
prnt E "No variable name(s) provided."
die 1
fi
local var val escaped pattern
for var in "${vars[@]}"; do
val="${!var}"
if [[ -z $val ]]; then
prnt W "Variable '$var' is unset or empty; skipped."
continue
fi
# Échapper les caractères spéciaux pour sed
escaped=$(printf '%s' "$val" | sed -e 's/[\/&]/\\&/g')
pattern="${delimiter}${var}${delimiter}"
prnt I "Replacing $pattern with $val in $file"
sed -i -e "s|$pattern|$escaped|g" "$file"
done
}
export -f setvar
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Replace @VAR@ in a text file by the corresponding values available in the
# environment. The --delimiter or -d option allow to use something else than @
setvars_from_env() {
local file delimiter="@"
while [[ $# -gt 0 ]]; do
case "$1" in
-d|--delimiter)
shift
delimiter="${1:-@}"
;;
*)
file="$1"
;;
esac
shift
done
[[ -f $file ]] || {
prnt E "File not found: $file"
die 1
}
local vars
vars=$(grep -o "${delimiter}[A-Z0-9_]\+${delimiter}" "$file" | sort -u | tr -d "$delimiter")
[[ -z $vars ]] && return 0
setvar --delimiter "$delimiter" $vars "$file"
}
export -f setvars_from_env
# ------------------------------------------------------------------------------
# EOF