added commands for paste

This commit is contained in:
Morgan McMillian 2021-07-12 22:07:33 -07:00
parent b3d2d6d2b0
commit a5e32f9a91
3 changed files with 113 additions and 2 deletions

View file

@ -76,3 +76,17 @@ def show_repos(repos, verbose):
print(f" {repo['description']}") print(f" {repo['description']}")
print() 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()

View file

@ -2,7 +2,7 @@ from setuptools import setup
setup( setup(
name='sr.ht cli', name='sr.ht cli',
version='0.1.1', version='0.2.0',
py_modules=['srht','output'], py_modules=['srht','output'],
install_requires=[ install_requires=[
'click', 'click',

99
srht.py
View file

@ -23,6 +23,11 @@ TICKET_RESOLUTION = [
"duplicate", "duplicate",
"not_our_bug" "not_our_bug"
] ]
PASTE_VISIBILITY = [
"public",
"private",
"unlisted"
]
access_token = os.environ["SOURCEHUT_CLI_ACCESS_TOKEN"] access_token = os.environ["SOURCEHUT_CLI_ACCESS_TOKEN"]
username = "~"+os.environ["USER"] username = "~"+os.environ["USER"]
@ -31,7 +36,7 @@ base_url = ""
@click.group() @click.group()
@click.option('--user') @click.option('--user')
@click.option('--token') @click.option('--token')
@click.version_option(version='0.1.1') @click.version_option(version='0.2.0')
def cli(user, token): def cli(user, token):
global access_token global access_token
global username global username
@ -40,6 +45,98 @@ def cli(user, token):
if user is not None: if user is not None:
username = "~"+user 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() @cli.group()
def git(): def git():
'''sourcehut git hosting service''' '''sourcehut git hosting service'''