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

* rewrite python

* Modify to pipe

* alexAubin review

* Fix "output" var not existing ...

* yunopaste: anonymize_output is too harsh and not yunopaste's job + print_usage ain't called ...

* yunopaste: return link to the raw version, less confusing than haste's ui ...

---------

Co-authored-by: Alexandre Aubin <alex.aubin@mailoo.org>
This commit is contained in:
André Théo LAURET 2023-07-10 21:28:22 +04:00 committed by GitHub
parent 89ffe624f6
commit 1927875924
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,77 +1,34 @@
#!/bin/bash #!/usr/bin/env python3
set -e import sys
set -u import requests
import json
PASTE_URL="https://paste.yunohost.org" SERVER_URL = "https://paste.yunohost.org"
TIMEOUT = 3
_die() { def create_snippet(data):
printf "Error: %s\n" "$*" try:
exit 1 url = SERVER_URL + "/documents"
} 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."
}
paste_data() { def main():
json=$(curl -X POST -s -d "$1" "${PASTE_URL}/documents") output = sys.stdin.read()
[[ -z "$json" ]] && _die "Unable to post the data to the server."
key=$(echo "$json" \ if not output:
| python3 -c 'import json,sys;o=json.load(sys.stdin);print(o["key"])' \ print("\033[31mError: No input received from stdin.\033[0m")
2>/dev/null) sys.exit(1)
[[ -z "$key" ]] && _die "Unable to parse the server response."
echo "${PASTE_URL}/${key}" url = create_snippet(output)
}
usage() { print("\033[32mURL: {}\033[0m".format(url))
printf "Usage: ${0} [OPTION]...
Read from input stream and paste the data to the YunoHost if __name__ == "__main__":
Haste server. main()
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 "${@}"