From c346053ebb9b0df4e41a262fc58ca13543d6b6da Mon Sep 17 00:00:00 2001 From: Midgard Date: Mon, 27 Sep 2021 00:22:54 +0200 Subject: [PATCH] Add rm and edit commands --- mmcli.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/mmcli.py b/mmcli.py index 75589d3..91f08e1 100755 --- a/mmcli.py +++ b/mmcli.py @@ -39,6 +39,11 @@ def http_to_ws(url): return "ws" + url[4:] +def warn_if_tty(you_can_type="the message objects", write_message_to=sys.stderr): + if sys.stdin.isatty(): + print(f"Reading from tty. (You can type {you_can_type} below. Or maybe you meant to redirect something to stdin.)", file=write_message_to) + + def get_posts_for_channel(self, channel_id: str, progress=lambda x: None, after=None, since=None, **kwargs) -> Iterable[Dict]: """ @raises ApiException: Passed on from lower layers. @@ -299,8 +304,7 @@ def send(mm_api: mattermost.MMApi, cmdline_args): team, channel = resolve_team_channel(mm_api, cmdline_args.channel) if cmdline_args.channel is not None else (None, None) if read_stdin: - if sys.stdin.isatty(): - print("Reading from tty. (You can type the message objects below. Or maybe you meant to redirect something to stdin.)", file=sys.stderr) + warn_if_tty() for line in sys.stdin: msg = json.loads(line) @@ -324,6 +328,20 @@ def send(mm_api: mattermost.MMApi, cmdline_args): print(sent) +def rm(mm_api: mattermost.MMApi, cmdline_args): + mm_api.delete_post(cmdline_args.msgid) + + +def edit(mm_api: mattermost.MMApi, cmdline_args): + if cmdline_args.message is None: + warn_if_tty(you_can_type="the new message text") + new_text = sys.stdin.read() + else: + new_text = cmdline_args.message + + mm_api.patch_post(cmdline_args.msgid, message=new_text) + + def status(mm_api: mattermost.MMApi, cmdline_args): if not cmdline_args.status: raise ValueError("No status selected") @@ -391,6 +409,8 @@ ACTIONS = { "cat": {"function": cat}, "ls": {"function": ls}, "send": {"function": send}, + "rm": {"function": rm}, + "edit": {"function": edit}, "status": {"function": status}, "customstatus": {"function": customstatus}, } @@ -471,6 +491,16 @@ The input format accepted on stdin is one JSON object per line. The possible fie parser_send.add_argument( "--attach", nargs="+", help="filename of file to attach") + parser_rm = subparsers.add_parser("rm", help="delete message(s)") + parser_rm.add_argument("msgid", help="ID of message to delete") + + parser_edit = subparsers.add_parser( + "edit", help="edit message(s)", + epilog="The input accepted on stdin will be used as-is as the new text.") + parser_edit.add_argument("msgid", help="ID of message to edit") + parser_edit.add_argument( + "--message", help="message; if not provided, message will be expected on stdin") + parser_status = subparsers.add_parser("status", help="update user status") parser_status.add_argument("--online", dest="status", action="store_const", const="online", help="Set status to online") parser_status.add_argument("--away", dest="status", action="store_const", const="away", help="Set status to away")