Handle prepending for custom env conf, fix bugs in external conf

master
Quentin Duchemin 2023-10-28 23:13:37 +02:00
parent ea22f2a027
commit 5f304686f7
1 changed files with 28 additions and 14 deletions

View File

@ -19,8 +19,10 @@ POLYBAR_CONFIG="polybar"
ARCH_PACKAGES="bootstrap" ARCH_PACKAGES="bootstrap"
MERGE_DIRS="${I3_CONFIG} ${POLYBAR_CONFIG} ${ARCH_PACKAGES}" MERGE_DIRS="${I3_CONFIG} ${POLYBAR_CONFIG} ${ARCH_PACKAGES}"
changed_files="$(git --git-dir=$HOME/.cfg/ --work-tree=$HOME diff-tree -r --name-status --no-commit-id HEAD^1 HEAD)"
# Check if a file has been modified and deleted since last commit, if so perform an action # Check if a file has been modified and deleted since last commit, if so perform an action
# $1 : relative path starting from the folder with "dest" # $1 : absolute path of the file
# $2 : command to launch if file has been created of modifed # $2 : command to launch if file has been created of modifed
# $3 : command to launch if file has been deleted # $3 : command to launch if file has been deleted
function check_copy() { function check_copy() {
@ -29,15 +31,15 @@ function check_copy() {
echo -e "* Running \"$2\"" && eval "$2" echo -e "* Running \"$2\"" && eval "$2"
return return
fi fi
echo "$changed_files" | grep --quiet "${1#$HOME/}" changed_file=`grep "${1#$HOME/}" <<< "${changed_files}"`
if [ $? == "0" ]; then if [ ! -z "${changed_file}" ]; then
echo "$changed_files" | cut -f 1 | grep --quiet "D" if echo "$changed_file" | grep -q -E "^D"; then
if [ $? == "0" ]; then
echo "* $1 has been removed" echo "* $1 has been removed"
echo -e "\t* Running \"$3\"" && eval "$3" echo -e "\t* Running \"$3\"" && eval "$3"
else fi
echo "* Changes detected in $1" if echo "$changed_file" | grep -q -E "^(A|M)"; then
echo -e "* Running \"$2\"" && eval "$2" echo "* Changes detected in $(basename $1)"
echo -e "\t* Running \"$2\"" && eval "$2"
fi fi
else else
echo "$1 not modified, no need to copy" echo "$1 not modified, no need to copy"
@ -49,6 +51,7 @@ function check_copy() {
# a common base for multiple computer, and per-computer stuff. # a common base for multiple computer, and per-computer stuff.
# $1 : path of the folder # $1 : path of the folder
# $2 : optional current environment # $2 : optional current environment
# $3 : if defined, prepend $2 instead of appending
function merge_config() { function merge_config() {
echo -e "\n=== Generate ${m} config file ===" echo -e "\n=== Generate ${m} config file ==="
rm -f ${CONFIG}/${m}/${FINAL_CONFIG} rm -f ${CONFIG}/${m}/${FINAL_CONFIG}
@ -58,8 +61,17 @@ function merge_config() {
# If there is an override folder, merge given env # If there is an override folder, merge given env
if [ ! -z ${2} ] && [ -d ${CONFIG}/${m}/${MERGE_CONFIG_FOLDER} ]; then if [ ! -z ${2} ] && [ -d ${CONFIG}/${m}/${MERGE_CONFIG_FOLDER} ]; then
for f in `find ${CONFIG}/${m}/config.d -type f -path "*${2}"`; do for f in `find ${CONFIG}/${m}/config.d -type f -path "*${2}"`; do
echo "Found ${f}, add to regular config file" final_path=${CONFIG}/${m}/${FINAL_CONFIG}
cat "${f}" >> ${CONFIG}/${m}/${FINAL_CONFIG} if [ ! -z $3 ]; then
echo "Found ${f}, append to regular config file"
cat "${f}" >> ${final_path}
else
echo "Found ${f}, prepend to regular config file"
cat ${f} > /tmp/config
echo -e '\n' >> /tmp/config
cat ${final_path} >> /tmp/config
mv /tmp/config ${final_path}
fi
done done
fi fi
} }
@ -75,10 +87,12 @@ CONFIG=${XDG_CONFIG_HOME:-$HOME/.config}
ENV=${1} ENV=${1}
changed_files="$(git --git-dir=$HOME/.cfg/ --work-tree=$HOME diff-tree -r --name-status --no-commit-id HEAD^1 HEAD)"
for m in ${MERGE_DIRS}; do for m in ${MERGE_DIRS}; do
if [ -f ${m}/above ]; then
merge_config $m ${ENV} true
else
merge_config $m ${ENV} merge_config $m ${ENV}
fi
done done
if [ ! -z "$INIT" ]; then if [ ! -z "$INIT" ]; then
@ -147,7 +161,7 @@ for f in `find ${CONFIG} -type f -name ${DEST_FILENAME}`; do
real_dest=${DEST}/${subfolder} real_dest=${DEST}/${subfolder}
sudo mkdir -p ${real_dest} sudo mkdir -p ${real_dest}
absolute_source=`realpath "${DIR}/${config_file}"` absolute_source=`realpath "${DIR}/${config_file}"`
check_copy "${filename}" "sudo cp ${absolute_source} ${real_dest}" "sudo rm ${real_dest}/${filename}" check_copy "${absolute_source}" "sudo cp ${absolute_source} ${real_dest}" "sudo rm ${real_dest}/${filename}"
done done
done done