Created
September 13, 2024 12:40
-
-
Save secemp9/2a66dcdc7af0986b56cdfefc6c2a4fa1 to your computer and use it in GitHub Desktop.
git env for ssh keys and github accounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| import subprocess | |
| import argparse | |
| import json | |
| import shutil | |
| CONFIG_FILE = os.path.expanduser('~/.github_account_manager.json') | |
| SSH_CONFIG_FILE = os.path.expanduser('~/.ssh/config') | |
| def load_config(): | |
| if os.path.exists(CONFIG_FILE): | |
| with open(CONFIG_FILE, 'r') as f: | |
| return json.load(f) | |
| return {'accounts': {}, 'current': None} | |
| def save_config(config): | |
| with open(CONFIG_FILE, 'w') as f: | |
| json.dump(config, f, indent=2) | |
| def update_ssh_config(config): | |
| with open(SSH_CONFIG_FILE, 'w') as f: | |
| for account, details in config['accounts'].items(): | |
| f.write(f"# GitHub account: {account}\n") | |
| f.write(f"Host github-{account}\n") | |
| f.write(f" HostName github.com\n") | |
| f.write(f" User git\n") | |
| f.write(f" IdentityFile {details['ssh_key']}\n") | |
| f.write(f" IdentitiesOnly yes\n\n") | |
| def add_account(args): | |
| config = load_config() | |
| config['accounts'][args.name] = { | |
| 'ssh_key': os.path.expanduser(args.ssh_key), | |
| 'email': args.email | |
| } | |
| save_config(config) | |
| update_ssh_config(config) | |
| print(f"Added account: {args.name}") | |
| def remove_account(args): | |
| config = load_config() | |
| if args.name in config['accounts']: | |
| del config['accounts'][args.name] | |
| if config['current'] == args.name: | |
| config['current'] = None | |
| save_config(config) | |
| update_ssh_config(config) | |
| print(f"Removed account: {args.name}") | |
| else: | |
| print(f"Account not found: {args.name}") | |
| def list_accounts(args): | |
| config = load_config() | |
| if config['accounts']: | |
| print("Configured accounts:") | |
| for name, details in config['accounts'].items(): | |
| print(f" {name} ({details['email']})") | |
| print(f" SSH Key: {details['ssh_key']}") | |
| if config['current']: | |
| print(f"\nCurrent account: {config['current']}") | |
| else: | |
| print("No accounts configured.") | |
| def use_account(args): | |
| config = load_config() | |
| if args.name in config['accounts']: | |
| config['current'] = args.name | |
| save_config(config) | |
| print(f"Now using account: {args.name}") | |
| # Set Git configuration | |
| subprocess.run(['git', 'config', '--global', 'user.email', config['accounts'][args.name]['email']]) | |
| subprocess.run(['git', 'config', '--global', 'user.name', args.name]) | |
| # Set GIT_SSH_COMMAND environment variable | |
| os.environ['GIT_SSH_COMMAND'] = f"ssh -i {config['accounts'][args.name]['ssh_key']} -o IdentitiesOnly=yes" | |
| else: | |
| print(f"Account not found: {args.name}") | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Manage multiple GitHub accounts") | |
| subparsers = parser.add_subparsers() | |
| # Add account | |
| add_parser = subparsers.add_parser('add', help='Add a new GitHub account') | |
| add_parser.add_argument('name', help='Account name') | |
| add_parser.add_argument('ssh_key', help='Path to SSH private key') | |
| add_parser.add_argument('email', help='GitHub email') | |
| add_parser.set_defaults(func=add_account) | |
| # Remove account | |
| remove_parser = subparsers.add_parser('remove', help='Remove a GitHub account') | |
| remove_parser.add_argument('name', help='Account name') | |
| remove_parser.set_defaults(func=remove_account) | |
| # List accounts | |
| list_parser = subparsers.add_parser('list', help='List configured GitHub accounts') | |
| list_parser.set_defaults(func=list_accounts) | |
| # Use account | |
| use_parser = subparsers.add_parser('use', help='Switch to a GitHub account') | |
| use_parser.add_argument('name', help='Account name') | |
| use_parser.set_defaults(func=use_account) | |
| args = parser.parse_args() | |
| if hasattr(args, 'func'): | |
| args.func(args) | |
| else: | |
| parser.print_help() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment