added conf_dump and updated doc
This commit is contained in:
@@ -149,4 +149,111 @@ export -f conf_save
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Display the profile configuration file, with optional section and key filters.
|
||||
# The same file resolution as conf_save is used: $HOME/.profile.conf when
|
||||
# present, otherwise $PROFILE_CONF or $MYPATH/profile.conf.
|
||||
#
|
||||
# Usage: conf_dump [options] [pattern]
|
||||
# -s, --section NAME : Only display the given section
|
||||
# pattern : Only display keys whose name contains this substring
|
||||
conf_dump()
|
||||
{
|
||||
local section="" key_pattern=""
|
||||
|
||||
local PARSED
|
||||
PARSED=$(getopt -o hs: --long help,section: -n 'conf_dump' -- "$@")
|
||||
# shellcheck disable=SC2181
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"conf_dump --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "conf_dump: Display the profile configuration file, with optional filters.\n\n"
|
||||
printf "Usage: conf_dump [options] [pattern]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
printf "\t-s, --section NAME\tOnly display the given section\n\n"
|
||||
printf "Arguments:\n"
|
||||
printf "\tpattern\tOnly display keys whose name contains this substring\n"
|
||||
return 0
|
||||
;;
|
||||
-s|--section)
|
||||
section="$2"
|
||||
shift 2
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ $# -gt 0 ]] && key_pattern="$1"
|
||||
|
||||
local conf_file
|
||||
if [[ -f "$HOME/.profile.conf" ]]; then
|
||||
conf_file="$HOME/.profile.conf"
|
||||
else
|
||||
conf_file="${PROFILE_CONF:-${MYPATH}/profile.conf}"
|
||||
fi
|
||||
|
||||
if [[ ! -f "$conf_file" ]]; then
|
||||
disp E "conf_dump: configuration file not found: ${conf_file}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Colours are passed via ENVIRON to avoid awk -v escape interpretation.
|
||||
_CONF_DUMP_SEC="${Blue:-}" \
|
||||
_CONF_DUMP_KEY="${BIWhite:-}" \
|
||||
_CONF_DUMP_RST="${RESETCOL:-}" \
|
||||
awk -v sec_filter="$section" -v key_filter="$key_pattern" '
|
||||
BEGIN {
|
||||
c_sec = ENVIRON["_CONF_DUMP_SEC"]
|
||||
c_key = ENVIRON["_CONF_DUMP_KEY"]
|
||||
c_rst = ENVIRON["_CONF_DUMP_RST"]
|
||||
in_target = 0
|
||||
current_sec = ""
|
||||
hdr_printed = 0
|
||||
found = 0
|
||||
}
|
||||
|
||||
{
|
||||
sub(/\r$/, "")
|
||||
|
||||
if ($0 ~ /^\[[^]]+\][[:space:]]*$/) {
|
||||
current_sec = $0
|
||||
sub(/^\[/, "", current_sec)
|
||||
sub(/\][[:space:]]*$/, "", current_sec)
|
||||
in_target = (sec_filter == "" || current_sec == sec_filter)
|
||||
hdr_printed = 0
|
||||
next
|
||||
}
|
||||
|
||||
if (!in_target) next
|
||||
if ($0 !~ /^[[:space:]]*[A-Za-z_][A-Za-z0-9_]*[[:space:]]*=/) next
|
||||
|
||||
key = $0; sub(/[[:space:]]*=.*$/, "", key); sub(/^[[:space:]]*/, "", key)
|
||||
val = $0; sub(/^[^=]*=/, "", val)
|
||||
|
||||
if (key_filter != "" && index(key, key_filter) == 0) next
|
||||
|
||||
if (!hdr_printed) {
|
||||
if (found) print ""
|
||||
print c_sec "[" current_sec "]" c_rst
|
||||
hdr_printed = 1
|
||||
found = 1
|
||||
}
|
||||
print " " c_key key c_rst "=" val
|
||||
}
|
||||
' "$conf_file"
|
||||
}
|
||||
export -f conf_dump
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
Reference in New Issue
Block a user