created var management lib, embryo removed from filefct lib
This commit is contained in:
@@ -231,52 +231,6 @@ 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()
|
||||||
@@ -286,8 +240,7 @@ tag_file()
|
|||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
106
lib/vars.sh
Normal file
106
lib/vars.sh
Normal 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
|
||||||
Reference in New Issue
Block a user