Skip to content

Commit

Permalink
mq-send-test: provide feedback for interactive terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixSchwarz committed Oct 18, 2024
1 parent 5f43806 commit aa8207f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
34 changes: 31 additions & 3 deletions schwarz/mailqueue/cli/send_test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

import sys


try:
import colorama
has_colorama = True
except ImportError:
has_colorama = False
import docopt

from ..app_helpers import guess_config_path
Expand All @@ -28,15 +34,37 @@ def send_test_message_main(argv=sys.argv, return_rc_code=False):
--verbose -v more verbose program output
"""
arguments = docopt.docopt(send_test_message_main.__doc__, argv=argv[1:])
verbose = arguments['--verbose']
quiet = arguments['--quiet']
recipient = arguments['--to']

config_path = guess_config_path(arguments['--config'])
cli_options = {
'verbose': arguments['--verbose'],
'recipient': arguments['--to'],
'verbose': verbose,
'recipient': recipient,
'sender': arguments['--from'],
'quiet' : arguments['--quiet'],
'quiet': quiet,
}
was_sent = send_test_message(config_path, cli_options)
exit_code = 0 if was_sent else 100
if verbose or (not quiet and sys.stdout.isatty()):
msg = _status_message(was_sent, recipient)
print(msg)
if return_rc_code:
return exit_code
sys.exit(exit_code)

def _status_message(was_sent, recipient) -> str:
if has_colorama and sys.stdout.isatty():
_cf_green = colorama.Fore.GREEN
_cf_lightred = colorama.Fore.LIGHTRED_EX
_cf_reset = colorama.Style.RESET_ALL
else:
_cf_green = ''
_cf_lightred = ''
_cf_reset = ''

if was_sent:
return f'{_cf_green}OK{_cf_reset}: message sent succesfully to {recipient}.'
else:
return f'{_cf_lightred}FAIL{_cf_reset}: message sending failed!'
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ install_requires =
plugins =
PuzzlePluginSystem >= 0.6.0 # >= 0.6.0: .terminate_all_activated_plugins()

colors =
colorama

# "testutils" provides helpers to simplify testing (also usable by 3rd party code)
testutils =
# >= 0.8: SMTPCommandParser
Expand Down

0 comments on commit aa8207f

Please sign in to comment.