aboutsummaryrefslogtreecommitdiff
path: root/.local/lib
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2020-05-08 15:06:28 +0200
committerPatrick Spek <p.spek@tyil.nl>2021-08-14 11:59:36 +0200
commit9ea3dd2e9783b72dc78f5943f319b66bac226842 (patch)
tree9623628ac008dd94767e3ca9100ea57aa071e348 /.local/lib
parent276262410880fcb6c27a3a0e5f9308a5a1cb7bd3 (diff)
Arrange new method for writing command wrappers
Diffstat (limited to '.local/lib')
-rwxr-xr-x.local/lib/cmd-wrapper/wrapper.sh68
1 files changed, 68 insertions, 0 deletions
diff --git a/.local/lib/cmd-wrapper/wrapper.sh b/.local/lib/cmd-wrapper/wrapper.sh
new file mode 100755
index 0000000..de74794
--- /dev/null
+++ b/.local/lib/cmd-wrapper/wrapper.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
+# details.
+
+readonly wrapper_name="${0##*/}"
+readonly wrapper_data="$HOME/.local/etc/cmd-wrapper/${0##*/}"
+
+main() {
+ # Ensure there's a text file containing a list of potential binaries
+ if [ ! -f "$wrapper_data/bins.txt" ]
+ then
+ printf "No wrapper bins for %s (%s)\n" \
+ "$wrapper_name" \
+ "$wrapper_data/bins.txt" \
+ >&2
+
+ exit 1
+ fi
+
+ # Source a custom environment if one exists
+ if [ -f "$wrapper_data/env.sh" ]
+ then
+ #shellcheck disable=1090
+ . "$wrapper_data/env.sh"
+ fi
+
+ # Loop through all potential binaries
+ while read -r bin
+ do
+ # Check if this particular bin is available
+ [ -x "$bin" ] || continue
+
+ # Run the wrap_before command if specified
+ if command -V "wrap_before" 2>/dev/null | head -n 1 | grep -Eq "function$"
+ then
+ wrap_before
+ fi
+
+ # shellcheck disable=SC2093
+ wrap_cmd "$bin" "$@"
+ done < "$wrapper_data/bins.txt"
+
+ # No fitting binary found, show error
+ printf "None of the underlying executables for %s exist:\n" \
+ "$wrapper_name" >&2
+
+ while read -r bin
+ do
+ printf "\t%s\n" "$bin" >&2
+ done < "$wrapper_data/bins.txt"
+
+ exit 1
+}
+
+# The actual running of the binary can be overriden from an env.sh, if desired.
+wrap_cmd() {
+ exec "$@"
+}
+
+main "$@"