PBinCLI/pbincli/cli.py

87 lines
4.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2017-03-02 11:23:05 +00:00
import os, sys, argparse
2018-05-23 08:14:35 +00:00
2017-02-18 18:00:40 +00:00
import pbincli.actions
2018-05-23 08:14:35 +00:00
from pbincli.api import PrivateBin
2017-02-18 18:00:40 +00:00
from pbincli.utils import PBinCLIException
2018-05-24 08:15:14 +00:00
CONFIG_PATHS = [os.path.join(".", "pbincli.conf", ),
os.path.join(os.getenv("HOME") or "~", ".config", "pbincli", "pbincli.conf") ]
def read_config(filename):
"""Read config variables from a file"""
settings = {}
with open(filename) as f:
for l in f.readlines():
key, value = l.strip().split("=")
settings[key.strip()] = value.strip()
return settings
2017-03-02 10:09:52 +00:00
2017-02-18 18:00:40 +00:00
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title="actions", help="List of commands")
2017-02-18 18:27:24 +00:00
2017-02-18 18:00:40 +00:00
# a send command
send_parser = subparsers.add_parser("send", description="Send data to PrivateBin instance")
send_parser.add_argument("-t", "--text", help="text in quotes. Ignored if used stdin. If not used, forcefully used stdin")
send_parser.add_argument("-f", "--file", help="example: image.jpg or full path to file")
send_parser.add_argument("-p", "--password", help="password for encrypting paste")
send_parser.add_argument("-E", "--expire", default="1day", action="store",
choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="paste lifetime (default: 1day)")
send_parser.add_argument("-B", "--burn", default=False, action="store_true", help="burn sent paste after reading")
send_parser.add_argument("-D", "--discus", default=False, action="store_true", help="open discussion for sent paste")
send_parser.add_argument("-F", "--format", default="plaintext", action="store",
choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of text (default: plaintext)")
send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="don't send text in paste")
send_parser.add_argument("-c", "--compression", default="zlib", action="store",
choices=["zlib", "none"], help="set compression for paste (default: zlib). Note: works only on v2 paste format")
2017-02-20 21:14:56 +00:00
send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
2018-02-12 14:28:18 +00:00
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)
2017-02-18 18:00:40 +00:00
send_parser.set_defaults(func=pbincli.actions.send)
2017-02-18 18:27:24 +00:00
# a get command
get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance")
2017-02-20 12:13:42 +00:00
get_parser.add_argument("pasteinfo", help="example: aabb#cccddd")
2017-02-20 21:14:56 +00:00
get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
2017-02-20 22:27:32 +00:00
get_parser.add_argument("-p", "--password", help="password for decrypting paste")
2017-02-20 12:13:42 +00:00
get_parser.set_defaults(func=pbincli.actions.get)
# a delete command
delete_parser = subparsers.add_parser("delete", description="Delete paste from PrivateBin instance using token")
delete_parser.add_argument("-p", "--paste", required=True, help="paste id")
delete_parser.add_argument("-t", "--token", required=True, help="paste deletion token")
delete_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
delete_parser.set_defaults(func=pbincli.actions.delete)
2017-02-18 18:00:40 +00:00
# parse arguments
args = parser.parse_args()
2018-05-23 08:14:35 +00:00
2018-05-24 08:15:14 +00:00
CONFIG = {"server": "https://paste.i2pd.xyz/",
"proxy": None}
for p in CONFIG_PATHS:
2018-05-31 17:09:48 +00:00
if os.path.exists(p):
2018-05-24 08:15:14 +00:00
CONFIG.update(read_config(p))
break
for key in CONFIG.keys():
var = "PRIVATEBIN_{}".format(key.upper())
if var in os.environ: CONFIG[key] = os.getenv(var)
2018-05-23 08:14:35 +00:00
2018-05-24 08:15:14 +00:00
api_client = PrivateBin(CONFIG["server"], proxy=CONFIG["proxy"])
2018-05-23 08:14:35 +00:00
2017-02-18 18:00:40 +00:00
if hasattr(args, "func"):
try:
2018-05-23 08:14:35 +00:00
args.func(args, api_client)
2017-02-18 18:00:40 +00:00
except PBinCLIException as pe:
print("PBinCLI error: {}".format(pe))
sys.exit(1)
else:
parser.print_help()
2017-03-02 10:09:52 +00:00
2017-02-18 18:00:40 +00:00
if __name__ == "__main__":
main()