cfg/bin/git-loadkey
Knut Ahlers 59afe8a585
Update loadkey to use new yaml config
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-06-24 14:57:27 +02:00

52 lines
1.1 KiB
Python
Executable file

#!/usr/bin/env python3
# Usage: git loadkey
#
# Configuration format:
# [
# {
# "match": ".*",
# "keyname": "...",
# }
# ]
import sys
import re
import subprocess
import os.path
import yaml
CONFIG = os.path.expanduser('~/.config/git-committerconfig.yaml')
def main():
try:
repos = [x.split()[1] for x in subprocess.check_output(
['git', 'remote', '-v'], universal_newlines=True).split('\n') if len(x.strip()) > 0]
except:
print('Could not find origin, not loading key.')
return 0
if os.path.isfile(CONFIG):
combinations = yaml.safe_load(open(CONFIG).read())
else:
print('Config ({}) found.'.format(CONFIG))
return 1
required_keys = []
for repo in sorted(set(repos)):
for combi in combinations:
if re.search(combi['match'], repo):
required_keys.append(combi['keyname'])
break
for key in sorted(set(required_keys)):
print('Loading key "{}"...'.format(key))
subprocess.check_call(['vault-sshadd', key])
return 0
if __name__ == "__main__":
exit(main())