lacre.admin: Implement identity import, fix identity list
- Let the user specify a directory, using the one from configuration by default. - If user requested identity list without a specific email, list all. Drop support for '-a' option.
This commit is contained in:
parent
0fe5e6b3dc
commit
b44bd7b150
|
@ -8,6 +8,7 @@ import sys
|
|||
import argparse
|
||||
import logging
|
||||
|
||||
import GnuPG
|
||||
import lacre
|
||||
import lacre.config as conf
|
||||
|
||||
|
@ -40,31 +41,53 @@ def sub_identities(args):
|
|||
identities = repo.IdentityRepository(conn)
|
||||
|
||||
all_identities = identities.freeze_identities()
|
||||
if args.all:
|
||||
for id_ in all_identities:
|
||||
print('-', all_identities[id_], id_)
|
||||
elif args.email:
|
||||
if args.email:
|
||||
all_rev = all_identities.emails()
|
||||
print('-', args.email, all_rev[args.email])
|
||||
else:
|
||||
exit(lacre.EX_UNAVAILABLE)
|
||||
for id_ in all_identities:
|
||||
print('-', all_identities[id_], id_)
|
||||
|
||||
|
||||
def sub_import(args):
|
||||
"""Sub-command to import all identities known to GnuPG into Lacre database."""
|
||||
LOG.debug('Importing identities...')
|
||||
source_dir = args.homedir or conf.get_item('gpg', 'keyhome')
|
||||
|
||||
public = GnuPG.public_keys(source_dir)
|
||||
|
||||
conn = repo.connect(conf.get_item('database', 'url'))
|
||||
identities = repo.IdentityRepository(conn)
|
||||
|
||||
total = 0
|
||||
for (fingerprint, email) in public.items():
|
||||
LOG.debug('Importing %s - %s', email, fingerprint)
|
||||
identities.register_or_update(email, fingerprint)
|
||||
total += 1
|
||||
|
||||
LOG.debug('Imported %d identities', total)
|
||||
print(f'Imported {total} identities')
|
||||
|
||||
|
||||
def main():
|
||||
conf.validate_config(additional=conf.SCRIPT_REQUIRED)
|
||||
|
||||
general_conf = conf.config_source()
|
||||
log_conf = conf.get_item('logging', 'config')
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='lacre.admin',
|
||||
description='Lacre Admin\'s best friend'
|
||||
description='Lacre Admin\'s best friend',
|
||||
epilog=f'Config read from {general_conf}. For diagnostic info, see {log_conf}'
|
||||
)
|
||||
|
||||
sub_commands = parser.add_subparsers(help='Sub-commands', required=True)
|
||||
|
||||
# cmd_import = sub_commands.add_parser('import',
|
||||
# help='Load identities from GnuPG directory to Lacre database'
|
||||
# )
|
||||
# cmd_import.add_argument('-d', '--homedir', help='Specify GnuPG directory')
|
||||
# cmd_import.set_defaults(operation=sub_import)
|
||||
cmd_import = sub_commands.add_parser('import',
|
||||
help='Load identities from GnuPG directory to Lacre database'
|
||||
)
|
||||
cmd_import.add_argument('-d', '--homedir', help='specify GnuPG directory (default: use configured dir.)')
|
||||
cmd_import.set_defaults(operation=sub_import)
|
||||
|
||||
cmd_queue = sub_commands.add_parser('queue',
|
||||
help='Inspect key queue',
|
||||
|
@ -76,11 +99,11 @@ def main():
|
|||
help='Inspect identity database',
|
||||
aliases=['id']
|
||||
)
|
||||
cmd_identities.add_argument('-e', '--email', help='Check single email')
|
||||
cmd_identities.add_argument('-a', '--all', help='List all emails', action='store_true')
|
||||
cmd_identities.add_argument('-e', '--email', help='look up a single email')
|
||||
cmd_identities.set_defaults(operation=sub_identities)
|
||||
|
||||
user_request = parser.parse_args()
|
||||
|
||||
user_request.operation(user_request)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue