Created
July 30, 2020 18:36
-
-
Save nightspotlight/ca3f1ff8a6ff4d659ea85c3af37596f1 to your computer and use it in GitHub Desktop.
Switch your Google Cloud service accounts on the command line
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
# put this in your ~/.profile, then reload your shell | |
function gcloud-activate() { | |
local action="$1" | |
local account="" | |
local credentials="" | |
case "${action}" in | |
alpha) | |
account="[email protected]" | |
credentials="${HOME}/.config/gcloud/my-project-alpha.json" | |
;; | |
beta) | |
account="[email protected]" | |
credentials="${HOME}/.config/gcloud/my-project-beta.json" | |
;; | |
reset) | |
account="[email protected]" | |
credentials="" | |
;; | |
*) { echo "Usage: ${FUNCNAME[0]} [alpha|beta|reset]"; return 1; };; | |
esac | |
# GOOGLE_APPLICATION_CREDENTIALS is consumed by Terraform | |
if [ -n "${credentials}" ] && [ -r "${credentials}" ]; then | |
export GOOGLE_APPLICATION_CREDENTIALS="${credentials}" | |
gcloud auth activate-service-account --key-file="${GOOGLE_APPLICATION_CREDENTIALS}" | |
else | |
unset GOOGLE_APPLICATION_CREDENTIALS | |
fi | |
gcloud config set account "${account}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment