Revert "[fix/enh] Rewrite of yunopaste CLI tool (#1667)"

This reverts commit 1927875924.
This commit is contained in:
Alexandre Aubin 2023-07-10 19:29:34 +02:00
parent 1927875924
commit dfc51ed7c5

View file

@ -1,34 +1,77 @@
#!/usr/bin/env python3 #!/bin/bash
import sys set -e
import requests set -u
import json
SERVER_URL = "https://paste.yunohost.org" PASTE_URL="https://paste.yunohost.org"
TIMEOUT = 3
def create_snippet(data): _die() {
try: printf "Error: %s\n" "$*"
url = SERVER_URL + "/documents" exit 1
response = requests.post(url, data=data.encode('utf-8'), timeout=TIMEOUT) }
response.raise_for_status()
dockey = json.loads(response.text)['key']
return SERVER_URL + "/raw/" + dockey
except requests.exceptions.RequestException as e:
print("\033[31mError: {}\033[0m".format(e))
sys.exit(1)
check_dependencies() {
curl -V > /dev/null 2>&1 || _die "This script requires curl."
}
def main(): paste_data() {
output = sys.stdin.read() json=$(curl -X POST -s -d "$1" "${PASTE_URL}/documents")
[[ -z "$json" ]] && _die "Unable to post the data to the server."
if not output: key=$(echo "$json" \
print("\033[31mError: No input received from stdin.\033[0m") | python3 -c 'import json,sys;o=json.load(sys.stdin);print(o["key"])' \
sys.exit(1) 2>/dev/null)
[[ -z "$key" ]] && _die "Unable to parse the server response."
url = create_snippet(output) echo "${PASTE_URL}/${key}"
}
print("\033[32mURL: {}\033[0m".format(url)) usage() {
printf "Usage: ${0} [OPTION]...
if __name__ == "__main__": Read from input stream and paste the data to the YunoHost
main() Haste server.
For example, to paste the output of the YunoHost diagnosis, you
can simply execute the following:
yunohost diagnosis show | ${0}
It will return the URL where you can access the pasted data.
Options:
-h, --help show this help message and exit
"
}
main() {
# parse options
while (( ${#} )); do
case "${1}" in
--help|-h)
usage
exit 0
;;
*)
echo "Unknown parameter detected: ${1}" >&2
echo >&2
usage >&2
exit 1
;;
esac
shift 1
done
# check input stream
read -t 0 || {
echo -e "Invalid usage: No input is provided.\n" >&2
usage
exit 1
}
paste_data "$(cat)"
}
check_dependencies
main "${@}"