commit:     dea9bf08dd93895101331a3d4e307199d6962891
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sat May 18 13:13:37 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat May 18 13:27:54 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=dea9bf08

Add an edo() function

Its implementation is similar to that of the edo eclass.

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

 functions.sh   | 25 +++++++++++++++++++++++++
 test-functions | 15 +++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/functions.sh b/functions.sh
index 2efcf1f..6301c40 100644
--- a/functions.sh
+++ b/functions.sh
@@ -86,6 +86,31 @@ ebegin()
        fi
 }
 
+#
+# Takes the positional parameters as the definition of a simple command then
+# prints the command as an informational message with einfo before executing 
it.
+# Should the command fail, a diagnostic message shall be printed and the shell
+# be made to exit by calling the die function.
+#
+edo() {
+       # Approximate the effect of ${param@Q} bash expansion.
+       genfun_cmd=$(
+               awk -v q=\' -f - "$@" <<-'EOF'
+                       BEGIN {
+                               argc = ARGC
+                               ARGC = 1
+                               for (i = 1; i < argc; i++) {
+                                       arg = ARGV[i]
+                                       gsub(q, q "\\" q q, arg)
+                                       printf("'%s' ", arg)
+                               }
+                       }
+               EOF
+       )
+       einfo "${genfun_cmd% }"
+       "$@" || die "Failed to run command: ${genfun_cmd% }"
+}
+
 #
 # Prints an indicator to convey the completion of a given process, provided 
that
 # EINFO_QUIET is false. It is expected that it be paired with an earlier call 
to

diff --git a/test-functions b/test-functions
index d46546a..9414136 100755
--- a/test-functions
+++ b/test-functions
@@ -110,6 +110,20 @@ test_ebegin() {
        return "$(( ok ? 0 : 1 ))"
 }
 
+test_edo() {
+       set -- \
+               eq  1  false \
+               eq  0  true
+
+       callback() {
+               shift
+               test_description="edo $1"
+               ( edo "$1" )
+       }
+
+       iterate_tests 3 "$@"
+}
+
 test_is_older_than() {
        set -- \
                ge  1  N/A           N/A \
@@ -459,6 +473,7 @@ test_is_int || rc=1
 test_is_visible || rc=1
 test_yesno || rc=1
 test_die || rc=1
+test_edo || rc=1
 
 cleanup_tmpdir
 

Reply via email to