From a5e32f9a913338b84441703d8a825ebabeed61dd Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Mon, 12 Jul 2021 22:07:33 -0700 Subject: [PATCH] added commands for paste --- output.py | 14 ++++++++ setup.py | 2 +- srht.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 113 insertions(+), 2 deletions(-) diff --git a/output.py b/output.py index b2e106c..a63ab14 100644 --- a/output.py +++ b/output.py @@ -76,3 +76,17 @@ def show_repos(repos, verbose): print(f" {repo['description']}") print() +def show_pastes(pastes): + for paste in pastes: + print(f"{paste['sha']} ({paste['visibility']}) {paste['created']}") + +def show_paste(paste): + print(f"[{paste['sha']}]") + print() + print(f"visibility: {paste['visibility']}") + print(f" created: {paste['created']}") + print() + for blob in paste['blobs']: + print(f"[{blob['filename']}] {blob['id']}") + print(f"{blob['content']}") + print() diff --git a/setup.py b/setup.py index 49191ae..7b5f18d 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='sr.ht cli', - version='0.1.1', + version='0.2.0', py_modules=['srht','output'], install_requires=[ 'click', diff --git a/srht.py b/srht.py index 4e939ad..5ec42f7 100644 --- a/srht.py +++ b/srht.py @@ -23,6 +23,11 @@ TICKET_RESOLUTION = [ "duplicate", "not_our_bug" ] +PASTE_VISIBILITY = [ + "public", + "private", + "unlisted" +] access_token = os.environ["SOURCEHUT_CLI_ACCESS_TOKEN"] username = "~"+os.environ["USER"] @@ -31,7 +36,7 @@ base_url = "" @click.group() @click.option('--user') @click.option('--token') -@click.version_option(version='0.1.1') +@click.version_option(version='0.2.0') def cli(user, token): global access_token global username @@ -40,6 +45,98 @@ def cli(user, token): if user is not None: username = "~"+user +@cli.group() +def paste(): + '''sourcehut paste hosting service''' + global base_url + base_url = "https://paste.sr.ht/api" + +@paste.command("list") +def get_pastes(): + '''List pastes''' + url = base_url + f"/pastes" + headers = {'Authorization': "Bearer " + access_token} + r = requests.get(url, headers=headers) + if r.status_code == 200: + output.show_pastes(r.json()['results']) + else: + print(r.status_code) + print(r.text) + +@paste.command("show") +@click.argument('sha') +def show_paste(sha): + '''Show details of a paste''' + url = base_url + f"/pastes/{sha}" + headers = {'Authorization': "Bearer " + access_token} + r = requests.get(url, headers=headers) + if r.status_code == 200: + paste = r.json() + blobs = [] + for b in paste['files']: + burl = base_url + f"/blobs/{b['blob_id']}" + br = requests.get(burl, headers=headers) + if br.status_code == 200: + item = { + 'id': b['blob_id'], + 'filename': b['filename'], + 'content': br.json()['contents'] + } + blobs.append(item) + paste['blobs'] = blobs + + output.show_paste(paste) + else: + print(r.status_code) + print(r.text) + +@paste.command("blob") +@click.argument('sha') +def show_blob(sha): + '''Show paste blob''' + url = base_url + f"/blobs/{sha}" + headers = {'Authorization': "Bearer " + access_token} + r = requests.get(url, headers=headers) + if r.status_code == 200: + #output.show_pastes(r.json()['results']) + #print(json.dumps(r.json(), indent=4)) + print(r.json()['contents']) + else: + print(r.status_code) + print(r.text) + +@paste.command("submit") +@click.argument('visibility', type=click.Choice(PASTE_VISIBILITY)) +@click.option('--filename') +def submit_ticket(visibility, filename): + '''Submit a new paste''' + content = click.edit() + if content is None or len(content) < 1: + print("nothing saved, aborting...") + return + + url = base_url + f"/pastes" + headers = {'Authorization': "Bearer " + access_token} + payload = {'visibility': visibility} + blob = {'contents': content} + if filename is not None: + blob['filename'] = filename + payload['files'] = [blob] + + r = requests.post(url, headers=headers, json=payload) + print(r.status_code) + print(r.text) + +@paste.command("delete") +@click.argument('sha') +def delete_paste(sha): + '''Delete a paste''' + url = base_url + f"/pastes/{sha}" + headers = {'Authorization': "Bearer " + access_token} + r = requests.delete(url, headers=headers) + print(r.status_code) + print(r.text) + @cli.group() def git(): '''sourcehut git hosting service'''