commit:     94ee6e4ec34ece5759c6398cf6d33cf66c5e2687
Author:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
AuthorDate: Sun Nov 24 18:25:26 2024 +0000
Commit:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
CommitDate: Mon Dec  9 18:48:10 2024 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=94ee6e4e

eapi9-pipestatus.eclass: New eclass

This implements the pipestatus command, as proposed for EAPI 9:

| Checks 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.

Bug: https://bugs.gentoo.org/566342
Signed-off-by: Ulrich Müller <ulm <AT> gentoo.org>

 eclass/eapi9-pipestatus.eclass | 60 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/eclass/eapi9-pipestatus.eclass b/eclass/eapi9-pipestatus.eclass
new file mode 100644
index 000000000000..f92afe42ef14
--- /dev/null
+++ b/eclass/eapi9-pipestatus.eclass
@@ -0,0 +1,60 @@
+# Copyright 2024 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+# @ECLASS: eapi9-pipestatus.eclass
+# @MAINTAINER:
+# Ulrich Müller <[email protected]>
+# @AUTHOR:
+# Ulrich Müller <[email protected]>
+# @SUPPORTED_EAPIS: 7 8
+# @BLURB: check 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
+#
+# Caveat: "pipestatus" must be the next command following the pipeline.
+# In particular, the "local" declaration must be before the pipeline,
+# otherwise it would reset the status.
+
+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:
+# Check 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 status=( "${PIPESTATUS[@]}" )
+       local s ret=0 verbose=""
+
+       [[ ${1} == -v ]] && { verbose=1; shift; }
+       [[ $# -ne 0 ]] && die "usage: ${FUNCNAME} [-v]"
+
+       for s in "${status[@]}"; do
+               [[ ${s} -ne 0 ]] && ret=${s}
+       done
+
+       [[ ${verbose} ]] && echo "${status[@]}"
+
+       return "${ret}"
+}

Reply via email to