Skip to content

Instantly share code, notes, and snippets.

@ezzra
Created November 16, 2021 15:53
Show Gist options
  • Save ezzra/ec5a575a6cab368179bbd6a95e6f7fe6 to your computer and use it in GitHub Desktop.
Save ezzra/ec5a575a6cab368179bbd6a95e6f7fe6 to your computer and use it in GitHub Desktop.
gh cli wrapper with multiple users depending on repo
#!/bin/bash
# This wrapper will enable multi user support for github cli "gh".
# You need to link your "gh" command against this script and it will
# call the original command after changing the config credentials to the
# specific user
gh_user=$(git config gh.github.com.user)
# print infomessage
if [ -z "$gh_user" ]; then
echo "-> gh_user is not set, do so with 'git config gh.github.com.user NAME' for this repo"
exit
else
echo "-> using gh_user: ${gh_user}"
fi
# the general gh config file is symlinked to the config file for this specific user
rm ~/.config/gh/hosts.yml
ln -s ~/.config/gh/hosts-${gh_user}.yml ~/.config/gh/hosts.yml
# this is the original gh-command path, yours might differ from this
/usr/bin/gh $@
@dili91
Copy link

dili91 commented Dec 19, 2021

Nice :) Slightly changed the last line in my case to make this work:
$(which gh) $@

@ursetto
Copy link

ursetto commented May 26, 2022

Thanks. I didn't use your implementation directly, but it gave me the idea to wrap gh in a script that pulls GH_TOKEN and GH_ENTERPRISE_TOKEN from a keychain. It's a hack, but seems better than permanently storing unencrypted credentials in hosts.yml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment