phi/src/phicli

50 lines
1.3 KiB
Plaintext
Raw Normal View History

2020-10-10 18:45:16 +02:00
#!/usr/bin/env python3
from pprint import pformat as pp
2020-10-10 21:14:21 +02:00
import sys
import argparse
2020-10-10 18:45:16 +02:00
import phi.ldap.client
from phi.config import get_config
from phi.logging import setup_logging, get_logger
2020-10-10 23:27:08 +02:00
from phi.ldap.user import get_user_by_uid
2020-10-10 18:45:16 +02:00
log = get_logger(__name__)
if __name__ == '__main__':
2020-10-10 21:14:21 +02:00
parser = argparse.ArgumentParser()
2020-10-10 18:45:16 +02:00
2020-10-10 23:27:08 +02:00
parser.add_argument(
'--config', metavar='config.yml', help='custom configuration file')
subparses = parser.add_subparsers(title='actions', dest='action')
2020-10-10 21:14:21 +02:00
2020-10-10 23:27:08 +02:00
parser_showuser = subparses.add_parser(
'showuser', help='display user fields')
parser_showuser.add_argument(dest='uid', help='user identifier')
namespace = parser.parse_args(sys.argv[1:])
config_file = namespace.config
2020-10-10 18:45:16 +02:00
2020-10-10 21:14:21 +02:00
config_file, config = get_config(config_file)
setup_logging(config.get('logging', {}))
2020-10-10 23:27:08 +02:00
log.info("Using configuration at '{}':\n{}"
.format(config_file, pp(config)))
2020-10-10 18:45:16 +02:00
2020-10-10 23:27:08 +02:00
# TODO: check fields in config
2020-10-10 18:45:16 +02:00
client = phi.ldap.client.Client(**config['ldap'])
2020-10-10 23:27:08 +02:00
log.info('Opening LDAP client')
2020-10-10 18:45:16 +02:00
client.open()
2020-10-10 23:27:08 +02:00
log.info('Arguments: {}'.format(pp(namespace.__dict__)))
if namespace.action == 'showuser':
uid = namespace.uid
user = get_user_by_uid(client, uid)
print('\nuid: {}\n{}\n\n'.format(uid, pp(user)))
2020-10-10 18:45:16 +02:00
log.info('Closing LDAP client')
client.close()