Skip to content

Commit

Permalink
fix configFile.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyGALLAND authored Sep 19, 2024
1 parent c64e914 commit 63c1039
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions files/board/arpl/overlayfs/opt/arpl/include/configFile.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

###############################################################################
# Delete a key in config file
# 1 - Path of Key
# 2 - Path of yaml config file
function deleteConfigKey() {
yq eval 'del(.'${1}')' --inplace "${2}"
yq eval 'del(.'${1}')' --inplace "${2}" 2>/dev/null
}

###############################################################################
Expand All @@ -13,8 +12,7 @@ function deleteConfigKey() {
# 2 - Value
# 3 - Path of yaml config file
function writeConfigKey() {
[ "${2}" = "{}" ] && yq eval '.'${1}' = {}' --inplace "${3}" || \
yq eval '.'${1}' = "'${2}'"' --inplace "${3}"
[ "${2}" = "{}" ] && yq eval '.'${1}' = {}' --inplace "${3}" 2>/dev/null || yq eval '.'${1}' = "'"${2}"'"' --inplace "${3}" 2>/dev/null
}

###############################################################################
Expand All @@ -23,8 +21,28 @@ function writeConfigKey() {
# 2 - Path of yaml config file
# Return Value
function readConfigKey() {
RESULT=`yq eval '.'${1}' | explode(.)' "${2}"`
[ "${RESULT}" == "null" ] && echo "" || echo ${RESULT}
RESULT=$(yq eval '.'${1}' | explode(.)' "${2}" 2>/dev/null)
[ "${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
# 1 - Path of Key
# 2 - Value
# 3 - Path of yaml config file
function initConfigKey() {
[ -z "$(readConfigKey "${1}" "${3}")" ] && writeConfigKey "${1}" "${2}" "${3}" || true
}

###############################################################################
Expand All @@ -33,7 +51,7 @@ function readConfigKey() {
# 2 - Path of yaml config file
# Returns map of values
function readConfigMap() {
yq eval '.'${1}' | explode(.) | to_entries | map([.key, .value] | join(": ")) | .[]' "${2}"
yq eval '.'${1}' | explode(.) | to_entries | map([.key, .value] | join(": ")) | .[]' "${2}" 2>/dev/null
}

###############################################################################
Expand All @@ -42,7 +60,7 @@ function readConfigMap() {
# 2 - Path of yaml config file
# Returns array/map of values
function readConfigArray() {
yq eval '.'${1}'[]' "${2}"
yq eval '.'${1}'[]' "${2}" 2>/dev/null
}

###############################################################################
Expand All @@ -51,5 +69,13 @@ function readConfigArray() {
# 2 - Path of yaml config file
# Returns array of values
function readConfigEntriesArray() {
yq eval '.'${1}' | explode(.) | to_entries | map([.key])[] | .[]' "${2}"
yq eval '.'${1}' | explode(.) | to_entries | map([.key])[] | .[]' "${2}" 2>/dev/null
}

###############################################################################
# Check yaml config file
# 1 - Path of yaml config file
# Returns error information
function checkConfigFile() {
yq eval "${1}" 2>&1
}

0 comments on commit 63c1039

Please sign in to comment.