diff --git a/src/de/p1st/exec_notify/execNotify.py b/src/de/p1st/exec_notify/execNotify.py index 228a203..6b38ebd 100755 --- a/src/de/p1st/exec_notify/execNotify.py +++ b/src/de/p1st/exec_notify/execNotify.py @@ -4,7 +4,7 @@ from sys import argv import socket from typing import List -from de.p1st.exec_notify.lib import exec, config, mail +from de.p1st.exec_notify.lib import exec, config, mail, util def main(): @@ -22,19 +22,25 @@ def executeCommand(command: List) -> int: :return: exit code of executed command """ - keys = ['command', 'exit code', 'stderr', 'stdout'] exitCode, stdout, stderr = exec.execute(command) - values = [str(command), str(exitCode), stderr, stdout] - BODY = '' - for key, value in zip(keys, values): - BODY += f'=== {key} ===\n{value}\n' + prefix='┃ ' + BODY = f'┏{"━"*19}\n' \ + f'┣╸Command:\n' \ + f'{util.appendLinePrefix(prefix, str(command))}\n' \ + f'┣╸Exit Code:\n' \ + f'{util.appendLinePrefix(prefix, str(exitCode))}\n' \ + f'┣╸stderr:\n' \ + f'{util.appendLinePrefix(prefix, stderr)}\n' \ + f'┣╸stdout:\n' \ + f'{util.appendLinePrefix(prefix, stdout)}\n' \ + f'┗{"━"*19}' print(BODY) if exitCode != 0: hostname = socket.gethostname() - SUBJECT = '{hostname} | {str(command)}' + SUBJECT = f'{hostname} | {str(command)}' mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY) return exitCode diff --git a/src/de/p1st/exec_notify/lib/mail.py b/src/de/p1st/exec_notify/lib/mail.py index 5193a93..bd181e0 100644 --- a/src/de/p1st/exec_notify/lib/mail.py +++ b/src/de/p1st/exec_notify/lib/mail.py @@ -12,7 +12,8 @@ from de.p1st.exec_notify.lib import config def sendMailOrWriteToFile(SUBJECT: str, BODY: str, informAboutLocalMail: bool = True): if informAboutLocalMail and _localMailExists(): SUBJECT=f'{SUBJECT} | UNREAD LOCAL MAIL' - BODY=f'[!] Note [!]\nThere is local mail inside [file][maildir] that was not delivered previously! ' \ + BODY=f'[!] Note [!]\n' \ + f'There is local mail inside [file][maildir] that was not delivered previously! ' \ f'Please read and then delete it to get rid of this warning.\n\n\n' \ f'{BODY}' diff --git a/src/de/p1st/exec_notify/lib/util.py b/src/de/p1st/exec_notify/lib/util.py index 3ea65a1..a235ec4 100644 --- a/src/de/p1st/exec_notify/lib/util.py +++ b/src/de/p1st/exec_notify/lib/util.py @@ -22,3 +22,12 @@ def readFirstLine(file: Path) -> str: """ with open(file, "r") as f: return f.readline() + + +def appendLinePrefix(prefix: str, s: str) -> str: + if s is None or len(s) == 0: + return prefix + result = ''.join([prefix + line for line in s.splitlines(keepends=True)]) + if result.endswith(os.linesep): + result+=prefix + return result diff --git a/tests/util_test.py b/tests/util_test.py new file mode 100644 index 0000000..cb60708 --- /dev/null +++ b/tests/util_test.py @@ -0,0 +1,30 @@ +import unittest + +import de.p1st.exec_notify.lib.util as util + + +class MyTestCase(unittest.TestCase): + def test1_appendLinePrefix(self): + s = "This is a sentence.\nAnd this a second line." + + expected = "PREFIX This is a sentence.\nPREFIX And this a second line." + actual = util.appendLinePrefix("PREFIX ", s) + self.assertEqual(expected, actual) + + def test2_appendLinePrefix(self): + s = "\n" + + expected = "PREFIX \nPREFIX " + actual = util.appendLinePrefix("PREFIX ", s) + self.assertEqual(expected, actual) + + def test3_appendLinePrefix(self): + s = "" + + expected = "PREFIX " + actual = util.appendLinePrefix("PREFIX ", s) + self.assertEqual(expected, actual) + + +if __name__ == '__main__': + unittest.main()