diff --git a/src/phi/config.py b/src/phi/config.py index 259863d..c0d41ca 100644 --- a/src/phi/config.py +++ b/src/phi/config.py @@ -13,12 +13,16 @@ CONFIG_FILES = [os.path.join(p, CONFIG_FILE) for p in CONFIG_PATHS] -def get_config(): +def get_config(custom_config=None): """Return the path of the found configuration file and its content :returns: (path, config) :rtype: (str, dict) """ + if custom_config: + global CONFIG_FILES + CONFIG_FILES = [custom_config] + for f in CONFIG_FILES: try: with open(f, 'r') as c: @@ -31,5 +35,10 @@ def get_config(): # in any of CONFIG_PATHS. pass else: - raise FileNotFoundError("Could not find {} in any of {}." - .format(CONFIG_FILE, ', '.join(CONFIG_PATHS))) + if custom_config: + raise FileNotFoundError('Config file {} not found.' + .format(custom_config)) + else: + raise FileNotFoundError("Could not find {} in any of {}." + .format(CONFIG_FILE, + ', '.join(CONFIG_PATHS))) diff --git a/src/phicli b/src/phicli index 43a266b..6c8d30e 100755 --- a/src/phicli +++ b/src/phicli @@ -1,5 +1,7 @@ #!/usr/bin/env python3 from pprint import pformat as pp +import sys +import argparse import phi.ldap.client from phi.config import get_config @@ -9,13 +11,17 @@ log = get_logger(__name__) if __name__ == '__main__': - config_file, config = get_config() + parser = argparse.ArgumentParser() + parser.add_argument('--config', metavar='config.yml', + type=str, help='custom configuration file') + + ns = parser.parse_args(sys.argv[1:]) + config_file = ns.config + + config_file, config = get_config(config_file) setup_logging(config.get('logging', {})) - - # log.info("Found configuration at '{}':\n{}" - # .format(config_file, pp(config))) - log.info("Found configuration at '{}':\n".format(config_file)) + log.info("Using configuration at '{}':\n".format(config_file)) log.info('Opening LDAP client') client = phi.ldap.client.Client(**config['ldap'])