Permalink
Browse files

add `pigeon` script as an edge-triggered `canary`

  • Loading branch information...
sunaku committed Feb 28, 2018
1 parent 3d6fc04 commit 9dc207165b75cb6675d2c4c8456c4c8b832f07b5
Showing with 43 additions and 3 deletions.
  1. +3 −3 bin/canary
  2. +40 −0 bin/pigeon
View
@@ -3,8 +3,8 @@
# Usage: canary COMMAND [ARGUMENTS...]
# Usage: canary SHELL_EVAL_EXPRESSION
#
-# Silences (both stdout and stderr) the given command only if it succeeds
-# (exits with status 0) and then propagates its exit status to the caller.
+# Silences the given command (both stdout and stderr) if its exit status
+# is zero (a success) and then propagates that status back to the caller.
#
# Like a canary in a coal mine, this program exists to vocalize failures.
# It also prints the given command and its failing exit status to stderr.
@@ -24,7 +24,7 @@ output=$(
status=$?
if [ $status -ne 0 ]; then
- echo "canary: command exited with nonzero exit status: $status" >&2
+ echo "canary: command '$*' exited with nonzero status: $status" >&2
echo "$output" # propagate output of failed command back to caller
fi
View
@@ -0,0 +1,40 @@
+#!/bin/sh
+#
+# Usage: pigeon COMMAND [ARGUMENTS...]
+# Usage: pigeon SHELL_EVAL_EXPRESSION
+#
+# Silences the given command (both stdout and stderr) if its exit status
+# is different this time (compared to the last time it was run) and then
+# propagates that status back to the caller. Memory of previous statuses
+# is kept in files in the $PIGEON_STATUS_DIR directory, which defaults to
+# $XDG_DATA_HOME/pigeon/status or ~/.local/share/pigeon/status otherwise.
+#
+# Like a carrier pigeon, this program exists to convey changes in status.
+# It also prints the given command and its changed exit status to stderr.
+#
+# Written in 2018 by Suraj N. Kurapati <https://github.com/sunaku>
+
+: ${PIGEON_STATUS_DIR:=${XDG_DATA_HOME:-$HOME/.local/share}/pigeon/status}
+mkdir -p "$PIGEON_STATUS_DIR" || exit $?
+storage="$PIGEON_STATUS_DIR/$(echo "$*" | sha1sum | cut -f 1 -d ' ')"
+previous=$(test -s "$storage" && cat "$storage" || echo undefined)
+
+output=$(
+ # capture stderr alongside stdout
+ exec 2>&1
+
+ # SHELL_EVAL_EXPRESSION was given
+ test $# -eq 1 && set -- eval "$@"
+
+ # execute the given command line
+ "$@"
+)
+status=$?
+
+if [ $status != $previous ]; then
+ echo "pigeon: command '$*' exit status changed from $previous to $status" >&2
+ echo $status > "$storage" # store exit status for next time
+ echo "$output" # propagate output of command back to caller
+fi
+
+exit $status

0 comments on commit 9dc2071

Please sign in to comment.