Add ls command to list channels

This commit is contained in:
Midgard 2021-06-30 13:34:58 +02:00
parent 82b7704cf3
commit 0dba9e63ac
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -238,6 +238,60 @@ def cat(mm_api: mattermost.MMApi, cmdline_args):
print_initial_messages()
def ls(mm_api: mattermost.MMApi, cmdline_args):
# TODO Doesn't work for channel creation and deletion yet
# In a list to allow overwriting from within print_initial_channels without using global
backlog = [ [] ]
backlog_lock = threading.Lock()
team = resolve_team(mm_api, cmdline_args.team)
events = {"channel_converted", "channel_created", "channel_deleted", "channel_updated"}
def print_initial_channels():
for channel in mm_api.get_team_channels(team["id"]):
print(str_for_chan(lambda x: x, channel, cmdline_args))
with backlog_lock:
for channel in backlog[0]:
print(str_for_chan(lambda x: x, channel, cmdline_args))
backlog[0] = None
if cmdline_args.follow:
def simple_websocket_callback(_mmws, event_data):
if event_data.get("event") in events:
channel = json.loads(event_data["data"]["channel"])
if channel["team_id"] != team["id"]:
return
print(str_for_chan(lambda x: x, channel, cmdline_args))
def initial_websocket_callback(mmws: MMws, event_data):
if event_data.get("event") in events:
channel = json.loads(event_data["data"]["channel"])
if channel["team_id"] != team["id"]:
return
with backlog_lock:
if backlog[0] is not None:
backlog[0].append(channel)
return
else:
mmws.ws_handler = simple_websocket_callback
simple_websocket_callback(mmws, event_data)
ws_url = http_to_ws(mm_api._url) + "/v4/websocket"
mmws = MMws(initial_websocket_callback, mm_api.access_token, ws_url)
thread = threading.Thread(target=print_initial_channels)
thread.setDaemon(True)
thread.start()
mmws.run_websocket()
else:
print_initial_channels()
def send(mm_api: mattermost.MMApi, cmdline_args):
read_stdin = cmdline_args.message is None or cmdline_args.channel is None
@ -288,9 +342,26 @@ def str_for_post(attribute, post, cmdline_args):
assert False
def str_for_chan(attribute, channel, cmdline_args):
obj = {
k: v
for k, v in map(attribute, channel.items())
}
if cmdline_args.format == "json":
return json.dumps(obj)
if cmdline_args.format == "tsv":
# TODO
header = tsv_escape(obj.get("header", ""))
purpose = tsv_escape(obj.get("purpose", ""))
return f"{obj['id']}\t{obj['name']}\t{obj.get('display_name')}\t{obj.get('create_at')}\t{obj.get('delete_at')}\t{purpose}\t{header}"
assert False
ACTIONS = {
"login": {"function": login, "accesstoken_required": False},
"cat": {"function": cat},
"ls": {"function": ls},
"send": {"function": send},
}
@ -346,6 +417,10 @@ Hint: JSON output can be filtered on the command line with jq(1).
parser_cat.add_argument("--since", help="all after timestamp")
parser_cat.add_argument("-f", "--follow", action="store_true", help="keep running, printing new posts as they come in")
parser_ls = subparsers.add_parser("ls", help="list channels")
parser_ls.add_argument("team", help="URL name of team")
parser_ls.add_argument("-f", "--follow", action="store_true", help="keep running, printing changes to channels as they come in")
parser_send = subparsers.add_parser("send", help="send message(s)")
parser_send.add_argument(
"--channel", help="URL names of team and channel: '<team>/<channel>'; if not provided, "