cfg/bin/git-loadkey

53 lines
1.1 KiB
Plaintext
Raw Normal View History

2017-12-14 14:02:20 +00:00
#!/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:
2017-12-14 14:02:20 +00:00
print('Could not find origin, not loading key.')
return 0
if os.path.isfile(CONFIG):
combinations = yaml.safe_load(open(CONFIG).read())
else:
2017-12-14 14:02:20 +00:00
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)):
2017-12-14 14:02:20 +00:00
print('Loading key "{}"...'.format(key))
subprocess.check_call(['vault-sshadd', key])
return 0
if __name__ == "__main__":
exit(main())