From 56f045bfa2207c04654fa8b2604e8e0118e63eff Mon Sep 17 00:00:00 2001 From: boyska Date: Tue, 7 Jan 2020 00:10:17 +0100 Subject: [PATCH] initial --- cli.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 7 ++++ 2 files changed, 107 insertions(+) create mode 100644 cli.py create mode 100644 requirements.txt diff --git a/cli.py b/cli.py new file mode 100644 index 0000000..c6e5fcc --- /dev/null +++ b/cli.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +import sys +from pprint import pprint +import requests +import argparse + +try: + from colors import color +except ImportError: + def color(text, *args, **kwargs): + return text + + +def req(args, location, data=None): + kwargs = {} + if data is not None: + kwargs['json'] = data + r = requests.post(args.endpoint + location, + auth=tuple(args.auth.split(":", 2)), + **kwargs + ) + return r + +def main_channel_list(args): + r = req(args, "/rsGxsChannels/getChannelsSummaries") + channels = r.json()['channels'] + for chan in channels: + print(color(chan['mGroupName'], style='bold', fg='green')) + print(' ' + color(chan['mGroupId'], style='underline')) + +def main_channel_show(args): + r = req( + args, "/rsGxsChannels/getChannelsInfo", + { + 'chanIds': [args.channel_id] + } + ) + data = r.json() + channels = data['channelsInfo'] + for chan in channels: + print(color(chan['mMeta']['mGroupName'], style='bold', fg='green')) + print(' ' + color(chan['mMeta']['mGroupId'], style='underline')) + print(' ' + chan['mDescription']) + print(color('Last Post:', style='bold') + ' \t%s' % chan['mMeta']['mLastPost']) + + +def main_channel_post(args): + chid = args.channel_id + r = req( + args, "/rsGxsChannels/createPostV2", + { + 'channelId': args.channel_id, + 'title': args.post_title, + 'mBody': args.post_body, + } + ) + ret = r.json() + if ret.get('retval', True) is False: + print(color('ERROR: could not create post', fg='red', style='bold')) + print(ret['errorMessage']) + pprint(ret) + sys.exit(1) + pprint(ret) + + +def get_parser(): + p = argparse.ArgumentParser() + p.add_argument("--endpoint", default="http://127.0.0.1:9092") + p.add_argument("-u", "--auth", dest="auth") + + p_sub = p.add_subparsers() + + ch = p_sub.add_parser("channel") + ch.add_argument("--channel-id") + ch_sub = ch.add_subparsers() + ch_list = ch_sub.add_parser("list") + ch_list.set_defaults(mainfunc=main_channel_list) + + ch_show = ch_sub.add_parser("show") + ch_show.set_defaults(mainfunc=main_channel_show) + + ch_post = ch_sub.add_parser("post") + ch_post.set_defaults(mainfunc=main_channel_post) + ch_post.add_argument('--post-title') + ch_post.add_argument('--post-body') + + return p + + +def main(): + p = get_parser() + args = p.parse_args() + if getattr(args, "mainfunc", None) is None: + print("Not a complete command") + sys.exit(2) + args.mainfunc(args) + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8914523 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +ansicolors==1.1.8 +certifi==2019.11.28 +chardet==3.0.4 +idna==2.8 +pkg-resources==0.0.0 +requests==2.22.0 +urllib3==1.25.7