commit:     8a8dc3b63c8c639117c12a22960b03102dc00942
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sun May 19 17:41:23 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Mon May 20 07:49:53 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=8a8dc3b6

Add an eqatag() function

This differs from the isolated-functions implementation in that it will
render a line of compact JSON then use the logger(1) utility to log it.
It requires jq in order to do so. Should jq be unavailable, a warning
will be displayed, though no more than once.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Bug: https://bugs.gentoo.org/878505

 functions.sh | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/functions.sh b/functions.sh
index f2e5bb5..94d0f98 100644
--- a/functions.sh
+++ b/functions.sh
@@ -172,6 +172,78 @@ eoutdent()
        _esetdent "$(( ${#genfun_indent} - $1 ))"
 }
 
+#
+# This is based on the eqatag function defined by isolated-functions.sh in
+# portage. If the first parameter is the -v option, it shall be disregarded.
+# Discounting said option, at least one parameter is required, which shall be
+# taken as a tag name. Thereafter, zero or more parameters shall be accepted in
+# the form of "key=val", followed by zero or more parameters beginning with a
+# <slash>. An object shall be composed in which the tag is the value of a "tag"
+# key, the key/value pairs the value of a "data" key, and the <slash>-prefixed
+# parameters the value of a "files" key. The resulting object shall be rendered
+# as JSON by jq(1) before being logged by the logger(1) utility.
+#
+eqatag() {
+       local arg argc json positional tag
+
+       case ${genfun_has_jq} in
+               0)
+                       return 1
+                       ;;
+               1)
+                       ;;
+               *)
+                       if command -v jq >/dev/null; then
+                               genfun_has_jq=1
+                       else
+                               ewarn "The eqatag() function requires that jq 
be installed"
+                               genfun_has_jq=0
+                               return 1
+                       fi
+       esac
+       # Acknowledge the -v option for isolated-functions API compatibility.
+       if [ "$1" = "-v" ]; then
+               shift
+       fi
+       if [ "$#" -eq 0 ]; then
+               die "eqatag: no tag specified"
+       fi
+       tag=$1
+       shift
+       argc=$#
+       positional=0
+       for arg; do
+               case ${arg} in
+                       [!=/]*=?*)
+                               if [ "${positional}" -eq 1 ]; then
+                                       die "eqatag: invalid argument in 
positional context -- ${arg}"
+                               fi
+                               set -- "$@" --arg "${arg%%=*}" "${arg#*=}"
+                               ;;
+                       /*)
+                               if [ "${positional}" -eq 0 ]; then
+                                       set -- "$@" --args --
+                                       positional=1
+                               fi
+                               set -- "$@" "${arg}"
+                               ;;
+                       *)
+                               die "eqatag: invalid argument -- ${arg}"
+               esac
+       done
+       shift "${argc}"
+       json=$(
+               jq -cn '{
+                       eqatag: {
+                               tag:   $ARGS.named["=tag"],
+                               data:  $ARGS.named | with_entries(select(.key | 
startswith("=") | not)),
+                               files: $ARGS.positional
+                       }
+               }' --arg "=tag" "${tag}" "$@"
+       ) \
+       && logger -p user.debug -t "${0##*/}" -- "${json}"
+}
+
 #
 # Prints a QA warning message, provided that EINFO_QUIET is false. If printed,
 # the message shall also be conveyed to the esyslog function. For now, this is

Reply via email to