From 9ea3dd2e9783b72dc78f5943f319b66bac226842 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Fri, 8 May 2020 15:06:28 +0200 Subject: Arrange new method for writing command wrappers --- .local/bin/signal | 38 +------------------ .local/etc/cmd-wrapper/signal/bins.txt | 1 + .local/etc/cmd-wrapper/signal/env.sh | 6 +++ .local/lib/cmd-wrapper/wrapper.sh | 68 ++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 37 deletions(-) mode change 100755 => 120000 .local/bin/signal create mode 100644 .local/etc/cmd-wrapper/signal/bins.txt create mode 100644 .local/etc/cmd-wrapper/signal/env.sh create mode 100755 .local/lib/cmd-wrapper/wrapper.sh (limited to '.local') diff --git a/.local/bin/signal b/.local/bin/signal deleted file mode 100755 index bf8d198..0000000 --- a/.local/bin/signal +++ /dev/null @@ -1,37 +0,0 @@ -#!/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 "$@" diff --git a/.local/bin/signal b/.local/bin/signal new file mode 120000 index 0000000..3dc73e2 --- /dev/null +++ b/.local/bin/signal @@ -0,0 +1 @@ +../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 "$@" -- cgit v1.1