public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-dev] New eclass: eapi9-pipestatus.eclass
@ 2024-11-24 12:24 Ulrich Müller
  2024-11-24 12:58 ` Michał Górny
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Ulrich Müller @ 2024-11-24 12:24 UTC (permalink / raw
  To: gentoo-dev


[-- Attachment #1.1: Type: text/plain, Size: 970 bytes --]

This implements a pipestatus command, as discussed in bug 566342 [1]
and on IRC.

Subject to approval by the council, the command would be added in
EAPI 9. Its definition in the Package Manager Specification would be
along the lines of:

╓────
║ Tests the shell's pipe status array, i.e. the exit status of the
║ command(s) in the most recently executed foreground pipeline.
║ Returns shell true (0) if all elements are zero, or the last
║ non-zero element otherwise. If called with -v as the first argument,
║ also outputs the pipe status array as a space-separated list.
╙────

The "assert" command could then be banned (either in the same EAPI or
in the following EAPI), typically to be replaced by "pipestatus || die".

I would commit the eclass after the next council meeting, and obviously
only under the condition that the council will pre-approve the command
for EAPI 9.

[1] https://bugs.gentoo.org/566342


[-- Attachment #1.2: eapi9-pipestatus.eclass --]
[-- Type: text/plain, Size: 1556 bytes --]

# Copyright 2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: eapi9-pipestatus.eclass
# @MAINTAINER:
# Ulrich Müller <ulm@gentoo.org>
# @AUTHOR:
# Ulrich Müller <ulm@gentoo.org>
# @SUPPORTED_EAPIS: 7 8
# @BLURB: test the PIPESTATUS array
# @DESCRIPTION:
# A stand-alone implementation of a possible future pipestatus command
# (which would be aimed for EAPI 9).  It is meant as a replacement for
# "assert".  In its simplest form it can be called like this:
#
# @CODE
# foo | bar
# pipestatus || die
# @CODE
#
# With the -v option, the command will also echo the pipe status array,
# so it can be assigned to a variable like in the following example:
#
# @CODE
# local status
# foo | bar
# status=$(pipestatus -v) || die "foo | bar failed, status ${status}"
# @CODE

case ${EAPI} in
	7|8) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

# @FUNCTION: pipestatus
# @USAGE: [-v]
# @RETURN: last non-zero element of PIPESTATUS, or zero if all are zero
# @DESCRIPTION:
# Test the PIPESTATUS array, i.e. the exit status of the command(s)
# in the most recently executed foreground pipeline.  If called with
# option -v, also output the PIPESTATUS array.
pipestatus() {
	local -a status=( "${PIPESTATUS[@]}" )
	local s ret=0

	[[ $# -gt 0 && ${1} != -v || $# -gt 1 ]] \
		&& die "${FUNCNAME}: bad arguments: $@"

	[[ ${1} == -v ]] && echo "${status[@]}"

	for s in "${status[@]}"; do
		[[ ${s} -ne 0 ]] && ret=${s}
	done

	return "${ret}"
}

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 507 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-11-27 12:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-24 12:24 [gentoo-dev] New eclass: eapi9-pipestatus.eclass Ulrich Müller
2024-11-24 12:58 ` Michał Górny
2024-11-24 14:12   ` Ulrich Müller
2024-11-26  6:52 ` [gentoo-dev] New eclass v2: eapi9-pipestatus.eclass Ulrich Müller
2024-11-27 10:41 ` [gentoo-dev] New eclass: eapi9-pipestatus.eclass Florian Schmaus
2024-11-27 11:52   ` Michał Górny
2024-11-27 12:32   ` Ulrich Müller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox