chore: Merge config strings in multiple functions to improve code readability and maintainability

This commit is contained in:
Ing 2024-08-18 18:01:01 +08:00
parent 8931af4fe3
commit 6898e5b2fa
3 changed files with 41 additions and 42 deletions

View File

@ -157,6 +157,13 @@ jobs:
[ "${RESULT}" == "null" ] && echo "" || echo "${RESULT}" [ "${RESULT}" == "null" ] && echo "" || echo "${RESULT}"
} }
function mergeConfigStr() {
local JF=$(mktemp)
echo "${2}" | yq -p ${1} -o y > "${JF}"
yq eval-all --inplace '. as $item ireduce ({}; . * $item)' --inplace "${3}" "${JF}" 2>/dev/null
rm -f "${JF}"
}
REPO="${{ github.server_url }}/${{ github.repository }}" REPO="${{ github.server_url }}/${{ github.repository }}"
MODEL="${{ env.model }}" MODEL="${{ env.model }}"
VERSION="${{ env.version }}" VERSION="${{ env.version }}"
@ -200,10 +207,9 @@ jobs:
fi fi
if [ -n "${{ env.kernel }}" ]; then if [ -n "${{ env.kernel }}" ]; then
echo -en "set kernel" echo "set kernel"
USER_CONFIG_FILE="rr/ws/mnt/p1/user-config.yml" USER_CONFIG_FILE="rr/ws/mnt/p1/user-config.yml"
writeConfigKey "kernel" "${{ env.kernel }}" "${USER_CONFIG_FILE}" writeConfigKey "kernel" "${{ env.kernel }}" "${USER_CONFIG_FILE}"
echo ""
fi fi
sudo ./localbuild.sh config "${MODEL}" "${VERSION}" sudo ./localbuild.sh config "${MODEL}" "${VERSION}"
@ -213,29 +219,20 @@ jobs:
fi fi
if [ -n "${{ env.addons }}" ]; then if [ -n "${{ env.addons }}" ]; then
echo -en "set addons" echo "set addons: ${{ env.addons }}"
USER_CONFIG_FILE="rr/ws/mnt/p1/user-config.yml" USER_CONFIG_FILE="rr/ws/mnt/p1/user-config.yml"
writeConfigKey "addons" "{}" "${USER_CONFIG_FILE}" writeConfigKey "addons" "{}" "${USER_CONFIG_FILE}"
echo -en ': ' for A in $(echo "${{ env.addons }}" | sed 's/,/ /g'); do
for A in $(echo "${{ env.addons }}" | tr ',' ' '); do writeConfigKey "addons.\"${A}\"" "" "${USER_CONFIG_FILE}"
echo -en "${A} "
VALUE=""
writeConfigKey "addons.\"${A}\"" "${VALUE}" "${USER_CONFIG_FILE}"
done done
echo ""
fi fi
if [ ! "custom" = "${{ env.kernel }}" ] && [ -n "${{ env.modules }}" ]; then if [ ! "custom" = "${{ env.kernel }}" ] && [ -n "${{ env.modules }}" ]; then
echo -en "set modules" echo "set modules: ${{ env.modules }}"
USER_CONFIG_FILE="rr/ws/mnt/p1/user-config.yml" USER_CONFIG_FILE="rr/ws/mnt/p1/user-config.yml"
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
echo -en ': ' L="$(for I in $(echo "${{ env.modules }}" | sed 's/,/ /g'); do echo "modules.${I}:"; done)"
for M in $(echo "${{ env.modules }}" | tr ',' ' '); do mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
echo -en "${M} "
VALUE=""
writeConfigKey "modules.\"${M}\"" "${VALUE}" "${USER_CONFIG_FILE}"
done
echo ""
fi fi
sudo ./localbuild.sh build sudo ./localbuild.sh build

View File

@ -25,6 +25,17 @@ function readConfigKey() {
[ "${RESULT}" == "null" ] && echo "" || echo "${RESULT}" [ "${RESULT}" == "null" ] && echo "" || echo "${RESULT}"
} }
# Write to yaml config file
# 1 - format
# 2 - string
# 3 - Path of yaml config file
function mergeConfigStr() {
local JF=$(mktemp)
echo "${2}" | yq -p ${1} -o y > "${JF}"
yq eval-all --inplace '. as $item ireduce ({}; . * $item)' --inplace "${3}" "${JF}" 2>/dev/null
rm -f "${JF}"
}
############################################################################### ###############################################################################
# Write to yaml config file if key not exists # Write to yaml config file if key not exists
# 1 - Path of Key # 1 - Path of Key

View File

@ -346,9 +346,8 @@ function productversMenu() {
done <<<$(readConfigMap "addons" "${USER_CONFIG_FILE}") done <<<$(readConfigMap "addons" "${USER_CONFIG_FILE}")
# Rewrite modules # Rewrite modules
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
while read ID DESC; do L="$(echo "$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")" | awk '{print "modules."$1":"}')"
writeConfigKey "modules.\"${ID}\"" "" "${USER_CONFIG_FILE}" mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
done <<<$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")
# Remove old files # Remove old files
rm -f "${ORI_ZIMAGE_FILE}" "${ORI_RDGZ_FILE}" "${MOD_ZIMAGE_FILE}" "${MOD_RDGZ_FILE}" >/dev/null 2>&1 || true rm -f "${ORI_ZIMAGE_FILE}" "${ORI_RDGZ_FILE}" "${MOD_ZIMAGE_FILE}" "${MOD_RDGZ_FILE}" >/dev/null 2>&1 || true
rm -f "${PART1_PATH}/grub_cksum.syno" "${PART1_PATH}/GRUB_VER" "${PART2_PATH}/"* >/dev/null 2>&1 || true rm -f "${PART1_PATH}/grub_cksum.syno" "${PART1_PATH}/GRUB_VER" "${PART2_PATH}/"* >/dev/null 2>&1 || true
@ -424,9 +423,8 @@ function setConfigFromDSM() {
done <<<$(readConfigMap "addons" "${USER_CONFIG_FILE}") done <<<$(readConfigMap "addons" "${USER_CONFIG_FILE}")
# Rebuild modules # Rebuild modules
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
while read ID DESC; do L="$(echo "$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")" | awk '{print "modules."$1":"}')"
writeConfigKey "modules.\"${ID}\"" "" "${USER_CONFIG_FILE}" mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
done <<<$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")
touch ${PART1_PATH}/.build touch ${PART1_PATH}/.build
return 0 return 0
} }
@ -702,19 +700,16 @@ function moduleMenu() {
RET=$? RET=$?
case ${RET} in case ${RET} in
0) # ok-button 0) # ok-button
resp=$(cat ${TMP_PATH}/resp)
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
for ID in ${resp}; do L="$(for I in $(cat ${TMP_PATH}/resp 2>/dev/null); do echo "modules.${I}:"; done)"
writeConfigKey "modules.\"${ID}\"" "" "${USER_CONFIG_FILE}" mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
done
touch ${PART1_PATH}/.build touch ${PART1_PATH}/.build
break break
;; ;;
3) # extra-button 3) # extra-button
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
while read ID DESC; do L="$(echo "${ALLMODULES}" | awk '{print "modules."$1":"}')"
writeConfigKey "modules.\"${ID}\"" "" "${USER_CONFIG_FILE}" mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
done <<<${ALLMODULES}
touch ${PART1_PATH}/.build touch ${PART1_PATH}/.build
;; ;;
2) # help-button 2) # help-button
@ -3012,9 +3007,8 @@ function updateRR() {
KPRE="$(readConfigKey "platforms.${PLATFORM}.productvers.\"${PRODUCTVER}\".kpre" "${WORK_PATH}/platforms.yml")" KPRE="$(readConfigKey "platforms.${PLATFORM}.productvers.\"${PRODUCTVER}\".kpre" "${WORK_PATH}/platforms.yml")"
if [ -n "${PLATFORM}" -a -n "${KVER}" ]; then if [ -n "${PLATFORM}" -a -n "${KVER}" ]; then
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
while read ID DESC; do L="$(echo "$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")" | awk '{print "modules."$1":"}')"
writeConfigKey "modules.\"${ID}\"" "" "${USER_CONFIG_FILE}" mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
done <<<$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")
fi fi
fi fi
fi fi
@ -3149,9 +3143,8 @@ function updateModules() {
KPRE="$(readConfigKey "platforms.${PLATFORM}.productvers.\"${PRODUCTVER}\".kpre" "${WORK_PATH}/platforms.yml")" KPRE="$(readConfigKey "platforms.${PLATFORM}.productvers.\"${PRODUCTVER}\".kpre" "${WORK_PATH}/platforms.yml")"
if [ -n "${PLATFORM}" -a -n "${KVER}" ]; then if [ -n "${PLATFORM}" -a -n "${KVER}" ]; then
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
while read ID DESC; do L="$(echo "$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")" | awk '{print "modules."$1":"}')"
writeConfigKey "modules.\"${ID}\"" "" "${USER_CONFIG_FILE}" mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
done <<<$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")
fi fi
fi fi
rm -rf "${TMP_PATH}/update" rm -rf "${TMP_PATH}/update"
@ -3271,9 +3264,8 @@ function updateCKs() {
KPRE="$(readConfigKey "platforms.${PLATFORM}.productvers.\"${PRODUCTVER}\".kpre" "${WORK_PATH}/platforms.yml")" KPRE="$(readConfigKey "platforms.${PLATFORM}.productvers.\"${PRODUCTVER}\".kpre" "${WORK_PATH}/platforms.yml")"
if [ -n "${PLATFORM}" -a -n "${KVER}" ]; then if [ -n "${PLATFORM}" -a -n "${KVER}" ]; then
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
while read ID DESC; do L="$(echo "$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")" | awk '{print "modules."$1":"}')"
writeConfigKey "modules.\"${ID}\"" "" "${USER_CONFIG_FILE}" mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
done <<<$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")
fi fi
fi fi
rm -rf "${TMP_PATH}/update" rm -rf "${TMP_PATH}/update"
@ -3552,9 +3544,8 @@ else
fi fi
if [ -n "${PLATFORM}" -a -n "${KVER}" ]; then if [ -n "${PLATFORM}" -a -n "${KVER}" ]; then
writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}" writeConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
while read ID DESC; do L="$(echo "$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")" | awk '{print "modules."$1":"}')"
writeConfigKey "modules.\"${ID}\"" "" "${USER_CONFIG_FILE}" mergeConfigStr p "${L}" "${USER_CONFIG_FILE}"
done <<<$(getAllModules "${PLATFORM}" "$([ -n "${KPRE}" ] && echo "${KPRE}-")${KVER}")
fi fi
touch ${PART1_PATH}/.build touch ${PART1_PATH}/.build