diff options
author | Patrick Spek <p.spek@tyil.nl> | 2020-05-08 15:06:28 +0200 |
---|---|---|
committer | Patrick Spek <p.spek@tyil.nl> | 2021-08-14 11:59:36 +0200 |
commit | 9ea3dd2e9783b72dc78f5943f319b66bac226842 (patch) | |
tree | 9623628ac008dd94767e3ca9100ea57aa071e348 /.local | |
parent | 276262410880fcb6c27a3a0e5f9308a5a1cb7bd3 (diff) |
Arrange new method for writing command wrappers
Diffstat (limited to '.local')
l---------[-rwxr-xr-x] | .local/bin/signal | 38 | ||||
-rw-r--r-- | .local/etc/cmd-wrapper/signal/bins.txt | 1 | ||||
-rw-r--r-- | .local/etc/cmd-wrapper/signal/env.sh | 6 | ||||
-rwxr-xr-x | .local/lib/cmd-wrapper/wrapper.sh | 68 |
4 files changed, 76 insertions, 37 deletions
diff --git a/.local/bin/signal b/.local/bin/signal index bf8d198..3dc73e2 100755..120000 --- a/.local/bin/signal +++ b/.local/bin/signal @@ -1,37 +1 @@ -#!/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 BINS=" - /usr/bin/signal-desktop -" - -main() -{ - for bin in $BINS - do - [ -x "$bin" ] || continue - - export TMPDIR="${XDG_CACHE_HOME:-$HOME/.local/tmp}" - mkdir -p -- "/run/user/$UID/doc/by-app/org.signal.Signal" - - exec "$bin" "$@" - done - - printf "No underlying executable found for %s:\n" "${0##*/}" >&2 - - for bin in $BINS - do - printf "\t%s\n" "$bin" >&2 - done -} - -main "$@" +../lib/cmd-wrapper/wrapper.sh
\ No newline at end of file diff --git a/.local/etc/cmd-wrapper/signal/bins.txt b/.local/etc/cmd-wrapper/signal/bins.txt new file mode 100644 index 0000000..a5262d1 --- /dev/null +++ b/.local/etc/cmd-wrapper/signal/bins.txt @@ -0,0 +1 @@ +/usr/bin/signal-desktop diff --git a/.local/etc/cmd-wrapper/signal/env.sh b/.local/etc/cmd-wrapper/signal/env.sh new file mode 100644 index 0000000..8d3399d --- /dev/null +++ b/.local/etc/cmd-wrapper/signal/env.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +wrap_before() { + export TMPDIR="${XDG_CACHE_HOME:-$HOME/.local/tmp}" + mkdir -p -- "/run/user/$UID/doc/by-app/org.signal.Signal" +} 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 "$@" |