From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 1D6EC1581D3 for ; Wed, 22 May 2024 01:12:28 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id C6DC3E2A33; Wed, 22 May 2024 01:12:26 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id A79ECE2A33 for ; Wed, 22 May 2024 01:12:26 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 9874933BEA5 for ; Wed, 22 May 2024 01:12:25 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id BA2651AF2 for ; Wed, 22 May 2024 01:12:23 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1716191393.8a8dc3b63c8c639117c12a22960b03102dc00942.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 8a8dc3b63c8c639117c12a22960b03102dc00942 X-VCS-Branch: master Date: Wed, 22 May 2024 01:12:23 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: e717fb36-d76e-4477-909b-4d7d2c2fba96 X-Archives-Hash: 90196ae29aa8f2423e99898664d3f3ba commit: 8a8dc3b63c8c639117c12a22960b03102dc00942 Author: Kerin Millar plushkava net> AuthorDate: Sun May 19 17:41:23 2024 +0000 Commit: Sam James gentoo 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 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 +# . 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 -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