From fc3deea9eefef9517d63816e6d9899ce78f31346 Mon Sep 17 00:00:00 2001 From: l-n-s Date: Thu, 24 May 2018 04:15:14 -0400 Subject: [PATCH] add config files --- cli | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/cli b/cli index 180b133..18cbef5 100755 --- a/cli +++ b/cli @@ -5,6 +5,18 @@ import pbincli.actions from pbincli.api import PrivateBin from pbincli.utils import PBinCLIException +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 def main(): parser = argparse.ArgumentParser() @@ -49,10 +61,19 @@ def main(): # parse arguments args = parser.parse_args() - server = os.getenv("PRIVATEBIN_SERVER", "https://paste.i2pd.xyz/") - proxy = os.getenv("PRIVATEBIN_PROXY") + CONFIG = {"server": "https://paste.i2pd.xyz/", + "proxy": None} + + for p in CONFIG_PATHS: + is os.path.exists(p): + 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) - api_client = PrivateBin(server, proxy=proxy) + api_client = PrivateBin(CONFIG["server"], proxy=CONFIG["proxy"]) if hasattr(args, "func"): try: