Skip to content

Instantly share code, notes, and snippets.

@ergosteur
Last active May 19, 2024 15:39
Show Gist options
  • Save ergosteur/02249e08456f3fac683503a5211cadf4 to your computer and use it in GitHub Desktop.
Save ergosteur/02249e08456f3fac683503a5211cadf4 to your computer and use it in GitHub Desktop.
bash script to create and switch between multiple domains to use with GAMADV-XTD3. Place in the same directory as your gam executable. (made with copilot assistance)
#!/bin/bash
# Script name: gaminit
#
# This script sets the GAMCFGDIR environment variable to the directory where the script is located,
# appended with a domain name provided as an argument. If the specified domain subdirectory does not exist,
# it prompts the user to confirm if they want the directory to be created.
#
# Does NOT WORK with standard gam, as only GAMADV-XTD3 supports the GAMCFGDIR environment variable!!
#
# If a 'gam' executable is found in the same directory as the script, it adds the directory to the PATH.
#
# Usage: source ./gaminit.sh domain
# Example: source ./gaminit.sh example.com
#
# Note: To make GAMCFGDIR available in your current shell, you must source this script instead of running it.
# Check if the script is being sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Reminder: To make GAMCFGDIR available in your current shell, you should source this script: source ./gaminit.sh domain"
fi
# Check if an argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 domain"
return 1
fi
# Get the domain from user argument
DOMAIN="$1"
# Get the directory where the script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Append the domain to the directory
DIR="$SCRIPT_DIR/$DOMAIN"
# Check if the directory exists
if [ ! -d "$DIR" ]; then
# If the shell is interactive, prompt the user to create the directory
if [[ $- == *i* ]]; then
read -p "$DIR does not exist. Do you want to create it? (y/n) " -n 1 -r
echo # move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
mkdir -p "$DIR"
echo "Created $DIR. Remember to initialize gam for the new domain $DOMAIN."
echo "gam create project"
echo "or"
echo "gam update project"
echo "gam oauth create"
echo "gam user admin@$DOMAIN check serviceaccount"
echo "gam check svcacct user admin@$DOMAIN"
else
echo "Directory not created. Exiting."
return 1
fi
else
# If the shell is not interactive, exit with an error
echo "Directory $DIR does not exist. Cannot create directory in a non-interactive shell. Exiting."
return 1
fi
fi
# Set the GAMCFGDIR environment variable to this directory
export GAMCFGDIR=$DIR
# Check if a 'gam' executable exists in the same directory as the script
if [ -f "$SCRIPT_DIR/gam" ]; then
# Add the directory to the PATH
export PATH=$SCRIPT_DIR:$PATH
echo "Added $SCRIPT_DIR to PATH."
fi
# Print the value of GAMCFGDIR
echo "GAMCFGDIR is set to $GAMCFGDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment