From 917983987132a3a4c04a8aa1108d7ededbd21152 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Mon, 4 Jan 2021 21:01:09 -0500 Subject: [PATCH] Massively improve `upl` * Add argparsing * Private bin support * Functionalization --- upl | 62 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/upl b/upl index 3f494f5..c1b6051 100755 --- a/upl +++ b/upl @@ -1,20 +1,54 @@ #!/usr/bin/env python3 -import os.path -from shutil import copyfile -import sys +import argparse +from os import path, makedirs +from pathlib import Path +from shutil import copyfile -endp = "https://s3.us-east-2.wasabisys.com/armaan-public/" -wabin = "/home/armaa/.local/mnt/public/" -inp = sys.argv[1] +def parse_args(): + parser = argparse.ArgumentParser( + description="Copy file to Wasabi-mounted directory") + parser.add_argument("input", metavar="inp", type=str, nargs="+", + help="input file") + parser.add_argument("output", metavar="outp", type=str, nargs="*", + help="output path") + parser.add_argument("-p", "--private", + action="store_true", + help="Copy to private directory") + parser.add_argument("-m", "--mkdir", + action="store_true", + help="Create parent directories if necesary") + return parser.parse_args() -if len(sys.argv) >= 3: - out = sys.argv[2] -else: - out = inp +def outp(args): + if not args.output: + out = path.basename(args.input[0]) + else: + out = args.output[0] + return out -if not out: - out = os.path.basename(inp) +def main(args): + out = outp(args) + inp = args.input[0] -copyfile(inp, wabin + out) -print(endp+out) + if args.private: + s3path = priv_path + s3bin = priv_bin + else: + s3path = pub_path + s3bin = pub_bin + + fpath = s3path + out + if not path.exists(fpath): + makedirs(Path(fpath).parent) + + copyfile(inp, fpath) + print(endp + s3bin + "/" + out) + +if __name__ == "__main__": + endp = "https://s3.us-east-2.wasabisys.com/" + pub_path = "/home/armaa/.local/mnt/public/" + pub_bin = "armaan-public" + priv_path = "/home/armaa/.local/mnt/private/" + priv_bin = "armaan-private" + main(parse_args()) -- 2.39.2