From a77777987dde9ae5c08b6e471ddce5e389255b1a Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Sat, 24 Aug 2019 21:33:37 +0100 Subject: [PATCH 1/2] Add support for specifying a WHITELIST --- README.rst | 11 ++++++++++ auto-notify.plugin.zsh | 29 +++++++++++++++++++------ tests/test_is_auto_notify_ignored.zunit | 18 +++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index e4ec9ff..e5dad79 100644 --- a/README.rst +++ b/README.rst @@ -143,6 +143,17 @@ a new array. # redefine what is ignored by auto-notify export AUTO_NOTIFY_IGNORE=("docker" "man" "sleep") +**Using a Whitelist to ignore commands** + +If you wish to use a whitelist approach instead of the default blacklist approach used by ``AUTO_NOTIFY_IGNORE``, +you can do so by defining the environment variable ``AUTO_NOTIFY_WHITELIST`` with the elements you wish to +allow ``auto-notify`` to track and send notifications for. NOTE: If ``AUTO_NOTIFY_WHITELIST`` is defined, +then all the values in ``AUTO_NOTIFY_IGNORE`` are not used. + +:: + + export AUTO_NOTIFY_WHITELIST=("apt-get" "docker") + Temporarily Disabling Notifications ----------------------------------- diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index c44d04e..022d131 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -61,13 +61,28 @@ function _is_auto_notify_ignored() { # Remove leading whitespace target_command="$(echo "$target_command" | sed -e 's/^ *//')" - for ignore in $AUTO_NOTIFY_IGNORE; do - if [[ "$target_command" == "$ignore"* ]]; then - print "yes" - return - fi - done - print "no" + # If AUTO_NOTIFY_WHITELIST is defined, then auto-notify will ignore + # any item not defined in the white list + # Otherwise - the alternative (default) approach is used where the + # AUTO_NOTIFY_IGNORE blacklist is used to ignore commands + + if [[ -n "$AUTO_NOTIFY_WHITELIST" ]]; then + for allowed in $AUTO_NOTIFY_WHITELIST; do + if [[ "$target_command" == "$allowed"* ]]; then + print "no" + return + fi + done + print "yes" + else + for ignore in $AUTO_NOTIFY_IGNORE; do + if [[ "$target_command" == "$ignore"* ]]; then + print "yes" + return + fi + done + print "no" + fi } function _auto_notify_send() { diff --git a/tests/test_is_auto_notify_ignored.zunit b/tests/test_is_auto_notify_ignored.zunit index b678fa7..8397482 100644 --- a/tests/test_is_auto_notify_ignored.zunit +++ b/tests/test_is_auto_notify_ignored.zunit @@ -33,3 +33,21 @@ assert $state equals 0 assert "$output" same_as "yes" } + +@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST disallowed' { + AUTO_NOTIFY_WHITELIST="foobar" + + run _is_auto_notify_ignored "boom baz" + + assert $state equals 0 + assert "$output" same_as "yes" +} + +@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST allowed' { + AUTO_NOTIFY_WHITELIST="foobar" + + run _is_auto_notify_ignored "foobar baz" + + assert $state equals 0 + assert "$output" same_as "no" +} From cbd4e2bcac46d92da471dbec2f098f01fb5bdb5b Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Mon, 26 Aug 2019 12:25:43 +0100 Subject: [PATCH 2/2] Remove sudo prefix from command if detected --- auto-notify.plugin.zsh | 5 +++++ tests/test_is_auto_notify_ignored.zunit | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 022d131..58820d6 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -61,6 +61,11 @@ function _is_auto_notify_ignored() { # Remove leading whitespace target_command="$(echo "$target_command" | sed -e 's/^ *//')" + # Remove sudo prefix from command if detected + if [[ "$target_command" == "sudo "* ]]; then + target_command="${target_command/sudo /}" + fi + # If AUTO_NOTIFY_WHITELIST is defined, then auto-notify will ignore # any item not defined in the white list # Otherwise - the alternative (default) approach is used where the diff --git a/tests/test_is_auto_notify_ignored.zunit b/tests/test_is_auto_notify_ignored.zunit index 8397482..cb4f4fa 100644 --- a/tests/test_is_auto_notify_ignored.zunit +++ b/tests/test_is_auto_notify_ignored.zunit @@ -51,3 +51,12 @@ assert $state equals 0 assert "$output" same_as "no" } + +@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST allowed with sudo' { + AUTO_NOTIFY_WHITELIST="foobar" + + run _is_auto_notify_ignored "sudo foobar baz" + + assert $state equals 0 + assert "$output" same_as "no" +}