From 95a8a950b55426292a99e537244a174c547f414f Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 19 Nov 2022 15:14:47 +0100 Subject: [PATCH] feat(zsh): add script for paste.sr.ht Signed-off-by: Matej Focko --- playbooks/roles/shell/tasks/zsh.yml | 11 ++++++ .../roles/shell/templates/scripts/srht.py | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 playbooks/roles/shell/templates/scripts/srht.py diff --git a/playbooks/roles/shell/tasks/zsh.yml b/playbooks/roles/shell/tasks/zsh.yml index 9c96c95..f0eca1e 100644 --- a/playbooks/roles/shell/tasks/zsh.yml +++ b/playbooks/roles/shell/tasks/zsh.yml @@ -43,3 +43,14 @@ template: src: templates/scripts/toolbox_name.sh dest: ~/.local/bin/toolbox_name.sh + +- name: Install script for paste.sr.ht + template: + src: templates/scripts/srht.py + dest: ~/.local/bin/srht.py + +- name: Symlink paste.sr.ht + ansible.builtin.file: + src: ./srht.py + dest: ~/.local/bin/srht + state: link diff --git a/playbooks/roles/shell/templates/scripts/srht.py b/playbooks/roles/shell/templates/scripts/srht.py new file mode 100755 index 0000000..125f2f1 --- /dev/null +++ b/playbooks/roles/shell/templates/scripts/srht.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import os + +import click +import requests + + +@click.command() +@click.option("--public", "visibility", flag_value="public") +@click.option("--unlisted", "visibility", flag_value="unlisted", default=True) +@click.option("--private", "visibility", flag_value="private") +@click.argument("src", type=click.File("r"), nargs=-1) +def paste(visibility, src): + request = { + "visibility": visibility, + "files": [ + {"filename": s.name.split("/")[-1], "contents": s.read()} + for s in src + ], + } + + response = requests.post( + "https://paste.sr.ht/api/pastes", + json=request, + headers={"Authorization": f"token {os.getenv('SRHT_LEGACY')}"}, + ).json() + + if "sha" in response: + click.secho( + f"Pasted: https://paste.sr.ht/~mfocko/{response['sha']}", + fg="green", + ) + else: + click.secho(response, fg="red") + + +if __name__ == "__main__": + paste()