aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2022-08-13 09:42:48 -0300
committerEuAndreh <eu@euandre.org>2022-08-13 09:42:48 -0300
commitb0deea1e8b9bbefe3772cc8e1eedded2b5f85811 (patch)
tree2a115ab04e0b8cfd4f4854f4eeb58c9ef4e4a264 /bin
parentbin/n-times: Add working utility (diff)
downloaddotfiles-b0deea1e8b9bbefe3772cc8e1eedded2b5f85811.tar.gz
dotfiles-b0deea1e8b9bbefe3772cc8e1eedded2b5f85811.tar.xz
bin/untill: Add working utility
Diffstat (limited to 'bin')
-rwxr-xr-xbin/untill83
1 files changed, 83 insertions, 0 deletions
diff --git a/bin/untill b/bin/untill
new file mode 100755
index 0000000..403717f
--- /dev/null
+++ b/bin/untill
@@ -0,0 +1,83 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ until [-n SECONDS] -- COMMAND...
+ until -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+ Options:
+ -n SECONDS the amount of seconds to sleep between
+ attempts (default: 60)
+ -h, --help show this message
+
+
+ Runs COMMAND until it eventually succeeds. Sleep SECONDS
+ between attempts.
+
+
+ Examples:
+
+ Try flaky build until it succeeds:
+
+ $ until guix home build home.scm
+
+
+ Try to build until a new version is successfull,
+ waiting 30 seconds between attempts:
+
+ $ until -n 30 -- x git pull AND make
+ EOF
+}
+
+
+for flag in "$@"; do
+ case "$flag" in
+ --)
+ break
+ ;;
+ --help)
+ usage
+ help
+ exit
+ ;;
+ *)
+ ;;
+ esac
+done
+
+_SECONDS=60
+while getopts 'n:h' flag; do
+ case "$flag" in
+ 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
+ sleep "$_SECONDS"
+done