Skip to content

Instantly share code, notes, and snippets.

@everpeace
Created May 15, 2012 14:16
Show Gist options
  • Save everpeace/2702101 to your computer and use it in GitHub Desktop.
Save everpeace/2702101 to your computer and use it in GitHub Desktop.
GIT_SSH manager for github.
#!/bin/bash
if [ -z $GITHUB_KEYS_DIR ]; then
GITHUB_KEYS_DIR="$HOME/github-keys"
fi
if [ -z $GITHUB_KEY_FILE_NAME ]; then
GITHUB_KEY_FILE_NAME="id_rsa"
fi
usage(){
cat << EOS
gitsshm: GIT_SSH Manager.
Usage:
source gitsshm [Action]
Action:
-h|help - show this help.
-u|setup - add public keys for github to ssh-agent.
-r|reset - reset GIT_SSH environment variables and delete public keys for github from ssh-agent.
-l|list - list available user names.
-L|listkeys - list all keys registered in ssh-agent.
-c|current - print current GIT_SSH setting.
-s|switch [username] - setup GIT_SSH environment for given username.
[username] - shortcut for switch.
Author:
Shingo Omura
github : http://everpeace.github.com
twitter: http://twitter.com/everpeace
License:
MIT License.
EOS
}
list_users(){
$LS -1 $GITHUB_KEYS_DIR
echo ''
print_current_git_ssh
}
check_git_ssh(){
if echo $GIT_SSH |grep -q "$GITHUB_KEYS_DIR/.gitsshm/"
then
echo 'detected GIT_SSH is maintained by gitsshm.'
else
print_current_git_ssh
echo 'WARNING: GIT_SSH may not be maintained by gitsshm. GIT_SSH will be overwritten.'
fi
}
setup(){
setup="setup"
check_git_ssh ${setup}
echo "mkdir $GITHUB_KEYS_DIR/.gitsshm"
mkdir -p $GITHUB_KEYS_DIR/.gitsshm
echo "all keys in $GITHUB_KEYS_DIR will be added to ssh_agent"
for u in `$LS -1 $GITHUB_KEYS_DIR`
do
add_to_ssh_agent ${u}
echo "exec $SSH -i $GITHUB_KEYS_DIR/${u}/$GITHUB_KEY_FILE_NAME \"\$@\"" > $GITHUB_KEYS_DIR/.gitsshm/${u}-ssh.sh
echo "create $GITHUB_KEYS_DIR/.gitsshm/${u}-ssh.sh"
chmod u+x $GITHUB_KEYS_DIR/.gitsshm/${u}-ssh.sh
done
}
reset(){
check_git_ssh
for u in `$LS -1 $GITHUB_KEYS_DIR`
do
delete_from_ssh_agent ${u}
done
echo "remove dir $GITHUB_KEYS_DIR/.gitsshm"
rm -rf $GITHUB_KEYS_DIR/.gitsshm
unset GIT_SSH
print_current_git_ssh
}
delete_from_ssh_agent(){
$SSH_ADD -d $GITHUB_KEYS_DIR/$1/$GITHUB_KEY_FILE_NAME
}
add_to_ssh_agent(){
$SSH_ADD $GITHUB_KEYS_DIR/$1/$GITHUB_KEY_FILE_NAME
}
print_current_git_ssh(){
if [ -z "$GIT_SSH" ]; then
echo 'GIT_SSH is not set now.'
else
echo "current GIT_SSH is $GIT_SSH"
fi
}
set_git_ssh(){
export GIT_SSH=$GITHUB_KEYS_DIR/.gitsshm/$1-ssh.sh
}
switch(){
check_git_ssh
username=$1
if [ -z "${username}" ]; then
echo 'error: Username must be given.'
usage
else
for u in `$LS -1 $GITHUB_KEYS_DIR`
do
if [ ${u} = $1 ]; then
set_git_ssh $1
print_current_git_ssh
return
fi
done
echo "ERROR: $1 is not available. GIT_SSH will be not set. Try -l action."
print_current_git_ssh
fi
}
SSH_ADD=`which ssh-add`
SSH=`which ssh`
LS=`which ls`
action="$1"
if [ -z "${action}" ]; then
usage
else
case "${action}" in
-h|help) usage;;
-u|setup) if [ $# == 1 ]; then
setup
echo ''
echo 'Now you can switch GIT_SSH by -s action for available usernames:'
list_users
else
usage
fi ;;
-r|reset) if [ $# == 1 ]; then
reset
else
usage
fi ;;
-l|list) if [ $# == 1 ]; then
echo 'available usernames:'
list_users
else
usage
fi ;;
-L|listkeys) $SSH_ADD -l;;
-c|check) print_current_git_ssh;;
-s|switch) switch $2;;
-*) usage;;
*) switch $1;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment