From 20e7c554fc1af81218d702625639512ab85707ab Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 8 Aug 2017 11:56:36 +0200 Subject: [PATCH] Implement auto-loading of SSH keys Signed-off-by: Knut Ahlers --- bin/git-loadkey | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ bin/git-pot | 5 ++++- bin/git-ps | 3 +++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100755 bin/git-loadkey diff --git a/bin/git-loadkey b/bin/git-loadkey new file mode 100755 index 0000000..d206a4e --- /dev/null +++ b/bin/git-loadkey @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +# Usage: git loadkey +# +# Configuration format: +# [ +# { +# "match": ".*", +# "keyname": "...", +# } +# ] + +import sys +import re +import subprocess +import os.path +import json + +CONFIG = os.path.expanduser('~/.config/git-setmail.json') + + +def main(): + try: + repos = [x.split()[1] for x in subprocess.check_output(['git', 'remote', '-v']).split('\n') if len(x.strip()) > 0] + except: + print 'Could not find origin, not loading key.' + return 0 + + if os.path.isfile(CONFIG): + combinations = json.loads(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()) diff --git a/bin/git-pot b/bin/git-pot index 4398585..b5c4f65 100755 --- a/bin/git-pot +++ b/bin/git-pot @@ -12,10 +12,13 @@ function step { ### Determine what to push -step "Collect refs to push to origin" +step "Getting current branch" CURRENT_BRANCH=$(git branch --list | awk '/^\*/{print $2}') +step "Loading required key to push" +git loadkey + step "Execute pre-push auto-hook" git autohook pre-push diff --git a/bin/git-ps b/bin/git-ps index a52e21c..dc2c7be 100755 --- a/bin/git-ps +++ b/bin/git-ps @@ -10,6 +10,9 @@ function step { echo -e ${STEP_COLOR}[$(date +%H:%M:%S)] $1${NO_COLOR} } +step "Loading required keys to pull" +git loadkey + step "Fetching data from remote..." for remote in $(git remote -v | awk '{print $1}' | sort | uniq); do step "+++ Remote: '${remote}'"