From 6c9a5c95b6750b1185682e1f672ae99d3090201a Mon Sep 17 00:00:00 2001 From: r4sas Date: Fri, 4 Jun 2021 20:13:26 +0000 Subject: [PATCH] add verbosity flag (-v), closes #32 Signed-off-by: r4sas --- pbincli/actions.py | 32 +++++++++++++++++++++----------- pbincli/api.py | 4 ++++ pbincli/cli.py | 9 ++++++--- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/pbincli/actions.py b/pbincli/actions.py index 00ae496..5968a03 100644 --- a/pbincli/actions.py +++ b/pbincli/actions.py @@ -3,7 +3,7 @@ from pbincli.utils import PBinCLIError import signal def signal_handler(sig, frame): - print('Keyboard interrupt received, terminating...') + print('Keyboard interrupt received, terminating…') exit(0) signal.signal(signal.SIGINT, signal_handler) @@ -18,18 +18,24 @@ def send(args, api_client, settings=None): if args.text: text = args.text elif args.stdin: + print("Reading text from stdin…") text = args.stdin.read() elif not args.file: PBinCLIError("Nothing to send!") else: text = "" + print("Preparing paste…") paste = Paste(args.debug) + if args.verbose: print("Used server: {}".format(api_client.getServer())) + # get from server supported paste format version and update object + if args.verbose: print("Getting supported paste format version from server…") version = api_client.getVersion() paste.setVersion(version) + if args.verbose: print("Filling paste with data…") # set compression type, works only on v2 pastes if version == 2: paste.setCompression(args.compression) @@ -45,6 +51,7 @@ def send(args, api_client, settings=None): if args.file: paste.setAttachment(args.file) + if args.verbose: print("Encrypting paste…") paste.encrypt( formatter = args.format, burnafterreading = args.burn, @@ -53,13 +60,12 @@ def send(args, api_client, settings=None): request = paste.getJSON() - if args.debug: - print("Passphrase:\t{}".format(paste.getHash())) - print("Request:\t{}".format(request)) + if args.debug: print("Passphrase:\t{}\nRequest:\t{}".format(paste.getHash(), request)) # If we use dry option, exit now if args.dry: exit(0) + print("Uploading paste…") result = api_client.post(request) if args.debug: print("Response:\t{}\n".format(result)) @@ -76,12 +82,12 @@ def send(args, api_client, settings=None): result['id'], passphrase)) elif result['status']: # return code is other then zero - PBinCLIError("Something went wrong...\nError:\t\t{}".format(result['message'])) + PBinCLIError("Something went wrong…\nError:\t\t{}".format(result['message'])) else: # or here no status field in response or it is empty - PBinCLIError("Something went wrong...\nError: Empty response.") + PBinCLIError("Something went wrong…\nError: Empty response.") if args.short: - print("\nQuerying URL shortening service...") + print("\nQuerying URL shortening service…") shortener.getlink("{}?{}#{}".format( settings['server'], result['id'], @@ -99,6 +105,7 @@ def get(args, api_client, settings=None): if not (pasteid and passphrase): PBinCLIError("Incorrect request") + if args.verbose: print("Used server: {}".format(api_client.getServer())) if args.debug: print("PasteID:\t{}\nPassphrase:\t{}".format(pasteid, passphrase)) paste = Paste(args.debug) @@ -107,13 +114,14 @@ def get(args, api_client, settings=None): paste.setPassword(args.password) if args.debug: print("Password:\t{}".format(args.password)) + if args.verbose: print("Requesting paste from server…") result = api_client.get(pasteid) if args.debug: print("Response:\t{}\n".format(result)) # Paste was received. Checking received status code if not result['status']: # return code is zero - print("Paste received!") + print("Paste received! Decoding…") version = result['v'] if 'v' in result else 1 paste.setVersion(version) @@ -150,13 +158,13 @@ def get(args, api_client, settings=None): f.close() if version == 1 and 'meta' in result and 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']: - print("Burn afrer reading flag found. Deleting paste...") + print("Burn afrer reading flag found. Deleting paste…") api_client.delete(json_encode({'pasteid':pasteid,'deletetoken':'burnafterreading'})) elif result['status']: # return code is other then zero - PBinCLIError("Something went wrong...\nError:\t\t{}".format(result['message'])) + PBinCLIError("Something went wrong…\nError:\t\t{}".format(result['message'])) else: # or here no status field in response or it is empty - PBinCLIError("Something went wrong...\nError: Empty response.") + PBinCLIError("Something went wrong…\nError: Empty response.") def delete(args, api_client, settings=None): @@ -165,6 +173,8 @@ def delete(args, api_client, settings=None): pasteid = args.paste token = args.token + if args.verbose: print("Used server: {}".format(api_client.getServer())) if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token)) + print("Requesting paste deletion…") api_client.delete(json_encode({'pasteid':pasteid,'deletetoken':token})) diff --git a/pbincli/api.py b/pbincli/api.py index fe76d77..92841a5 100644 --- a/pbincli/api.py +++ b/pbincli/api.py @@ -78,6 +78,10 @@ class PrivateBin: '@value' in jsonldSchema['@context']['v']) \ else 1 + def getServer(self): + return self.server + + class Shortener: """Some parts of this class was taken from python-yourls (https://github.com/tflink/python-yourls/) library diff --git a/pbincli/cli.py b/pbincli/cli.py index 835a422..e31b2a5 100755 --- a/pbincli/cli.py +++ b/pbincli/cli.py @@ -56,7 +56,8 @@ def main(): send_parser.add_argument("--no-insecure-warning", default=False, action="store_true", help="suppress InsecureRequestWarning (only with --no-check-certificate)") ## - send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug") + send_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") + send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug output") send_parser.add_argument("--dry", default=False, action="store_true", help="invoke dry run") send_parser.add_argument("stdin", help="input paste text from stdin", nargs="?", type=argparse.FileType("r"), default=sys.stdin) send_parser.set_defaults(func=pbincli.actions.send) @@ -72,7 +73,8 @@ def main(): get_parser.add_argument("--no-insecure-warning", default=False, action="store_true", help="suppress InsecureRequestWarning (only with --no-check-certificate)") ## - get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug") + get_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") + get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug output") get_parser.set_defaults(func=pbincli.actions.get) # a delete command @@ -86,7 +88,8 @@ def main(): delete_parser.add_argument("--no-insecure-warning", default=False, action="store_true", help="suppress InsecureRequestWarning (only with --no-check-certificate)") ## - delete_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug") + delete_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") + delete_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug output") delete_parser.set_defaults(func=pbincli.actions.delete) # parse arguments