summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-08-26 09:47:25 -0300
committerEuAndreh <eu@euandre.org>2024-08-26 09:47:25 -0300
commit0078d30d0c7b3ab0f61eb96928f96fe668a19b8b (patch)
tree5bb11f49a581fa647000eed5edde92a7024245dc
parentMakefile: Use correct $(sources) in "install" target (diff)
downloadeut-0078d30d0c7b3ab0f61eb96928f96fe668a19b8b.tar.gz
eut-0078d30d0c7b3ab0f61eb96928f96fe668a19b8b.tar.xz
src/untill: Copy in untill(1) as is
-rw-r--r--deps.mk1
-rwxr-xr-xsrc/untill94
2 files changed, 95 insertions, 0 deletions
diff --git a/deps.mk b/deps.mk
index 0a6adda..6e48ecf 100644
--- a/deps.mk
+++ b/deps.mk
@@ -13,5 +13,6 @@ sources.sh = \
src/statusf \
src/tempname \
src/timestamp \
+ src/untill \
src/uuid \
diff --git a/src/untill b/src/untill
new file mode 100755
index 0000000..9290cfe
--- /dev/null
+++ b/src/untill
@@ -0,0 +1,94 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ until [-m MAX] [-n SECONDS] COMMAND...
+ until -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+
+ Options:
+ -n SECONDS the amount of seconds to sleep between
+ attempts (default: 5)
+ -m MAX the maximum number of attempts (default: unlimited)
+ -h, --help show this message
+
+ COMMAND the shell command to be executed
+
+
+ Runs COMMAND until it eventually succeeds, trying atmost MAX
+ times. Sleep SECONDS between attempts.
+
+
+ Examples:
+
+ Try flaky build until it succeeds:
+
+ $ until guix home build home.scm
+
+
+ Try atmost 10 timex to build until a new version is successfull,
+ waiting 30 seconds between attempts:
+
+ $ until -m 10 -n 30 -- x git pull AND make
+ EOF
+}
+
+
+for flag in "$@"; do
+ case "$flag" in
+ (--)
+ break
+ ;;
+ (--help)
+ usage
+ help
+ exit
+ ;;
+ (*)
+ ;;
+ esac
+done
+
+_SECONDS=5
+while getopts 'm:n:h' flag; do
+ case "$flag" in
+ (m)
+ MAX="$OPTARG"
+ ;;
+ (n)
+ _SECONDS="$OPTARG"
+ ;;
+ (h)
+ usage
+ help
+ exit
+ ;;
+ (*)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+
+ATTEMPT=1
+while true; do
+ printf 'Attempt %s.\n' "$ATTEMPT" >&2
+ ATTEMPT=$((ATTEMPT + 1))
+ if "$@"; then
+ break
+ fi
+ if [ -n "${MAX:-}" ] && [ "$ATTEMPT" -gt "$MAX" ]; then
+ printf 'Maximum number of attempts reached: %s.\n' "$MAX" >&2
+ exit 1
+ fi
+ sleep "$_SECONDS"
+done