Last active
September 6, 2024 03:37
-
-
Save morganestes/e5bd6f77f039a5554589112b622596a3 to your computer and use it in GitHub Desktop.
Remove gitignore from Pantheon sites
This file contains 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 bash | |
## | |
# Fixes a Pantheon site's gitignore and (optionally) applies any upstream updates. | |
## | |
function usage() { | |
NO_FORMAT="\033[0m" | |
F_BOLD="\033[1m" | |
C_GREEN="\033[38;5;2m" | |
C_DEEPPINK4="\033[38;5;53m" | |
C_YELLOW2="\033[38;5;190m" | |
C_WHITE="\033[38;5;15m" | |
C_DARKVIOLET="\033[48;5;92m" | |
echo -e "\n${F_BOLD}Updates the upstream gitignore for a given site environment.${NO_FORMAT}\n" | |
echo -e "Usage: ${C_WHITE}${C_DARKVIOLET}${0##*/}${NO_FORMAT} ${C_GREEN}SITE_ENV${NO_FORMAT} ${C_YELLOW2}[OPTIONS]${NO_FORMAT}\n" | |
echo -e "${C_GREEN}SITE_ENV${NO_FORMAT} is the site name in the form of ${F_BOLD}<site>.<env>${NO_FORMAT} (e.g. my-site.dev)." | |
echo -e "Environment defaults to ${F_BOLD}${C_DEEPPINK4}dev${NO_FORMAT} if not specified." | |
echo -e "\n${F_BOLD}Options${NO_FORMAT}: | |
\t${C_YELLOW2}-h${NO_FORMAT} \tDisplay this help and exit. | |
\t${C_YELLOW2}-u${NO_FORMAT} \tApply upstream updates after updating the gitignore. | |
\t${C_YELLOW2}-v${NO_FORMAT} \tVerbose output. | |
\t${C_YELLOW2}-p${NO_FORMAT} ${F_BOLD}PATH${NO_FORMAT}\tAlternate path to the working directory. Default: ${C_DEEPPINK4}${TMPDIR:-/tmp}${NO_FORMAT}.\n" | |
} | |
# Set defaults. | |
# Treat everything before the first dot as the site name. | |
SITE="${1%%.*}" | |
# Treat everything after the first dot as the environment. | |
ENV="${1##*.}" | |
SITE_ENV="${SITE}.${ENV}" | |
# Flag defaults. | |
APPLY_UPSTREAM_UPDATES=0 | |
verbose=0 | |
# Set up the working directories. | |
working_dir="${working_dir:-${TMPDIR:-/tmp}}" | |
# Remove any trailing slashes and add our custom path. | |
working_dir="${working_dir%%/}/fix-pantheon-gitignore" | |
sites_folder="${working_dir}/sites" | |
# Parse option flags. | |
OPTIND=2 # Start at the second argument since the first is a required positional argument. | |
while getopts "uvp:h" opt; do | |
case "$opt" in | |
u) APPLY_UPSTREAM_UPDATES=1 ;; | |
v) verbose=$((verbose + 1)) ;; | |
p) working_dir="$OPTARG" ;; | |
h) | |
usage | |
exit 0 | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
*) | |
usage >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift "$((OPTIND - 1))" # Discard the options and sentinel -- | |
# Ludicrously verbose mode. | |
[ "$verbose" -ge 3 ] && set -x | |
# Check arguments. We need at least the site name. | |
[ -z "$SITE" ] && usage && exit 1 | |
# Convert to lowercase. | |
SITE="${SITE,,}" | |
ENV="${ENV,,}" | |
# Set the environment if not specified. The expansion of the site name results in it becoming the environment so we need to check it too. | |
if [ -z "$ENV" ] || [ "$SITE" = "$ENV" ]; then | |
ENV="dev" | |
# Add the environment (back) to the site name once it's all figured out. | |
SITE_ENV="${SITE}.${ENV}" | |
fi | |
if [ "$verbose" -ge 2 ]; then | |
cat <<DEBUG | |
SITE: $SITE | |
ENV: $ENV | |
SITE_ENV: $SITE_ENV | |
working_dir: $working_dir | |
sites_folder: $sites_folder | |
APPLY_UPSTREAM_UPDATES: $APPLY_UPSTREAM_UPDATES | |
verbose: $verbose | |
DEBUG | |
fi | |
if [ 'test' = "$ENV" ] || [ 'live' = "$ENV" ]; then | |
printf "Cannot update test or live environments, only development environments.\nTry again with 'dev' or a multidev environment name.\n" | |
exit 2 | |
fi | |
# Add the environment (back) to the site name. | |
SITE_ENV="${SITE}.${ENV}" | |
if [ $verbose ]; then | |
printf "\n%s start: %s\n" "$SITE" "$(date)" | |
printf "Updating upstream for %s\n" "$SITE_ENV" | |
fi | |
mkdir -p "${sites_folder}" | |
cd "${sites_folder}" || exit 2 | |
# Clone the site's repo into a folder with the site name so we can update it. | |
#### | |
#### NOTE: this is a large repo and can quickly eat up disk space and network data. Be careful with how you many sites you run at a time. | |
#### | |
if [ $verbose ]; then | |
echo "Cloning $SITE_ENV to ${sites_folder}/${SITE}" | |
fi | |
eval "$(terminus connection:info --field=git_command --format=string "$SITE_ENV") --branch master --single-branch" & | |
pid=$! | |
wait "$pid" | |
cd "$SITE" || exit 2 | |
if [ -f ".gitignore" ]; then | |
rm .gitignore | |
git add .gitignore | |
git commit -m "Removed superseded upstream gitignore" | |
git push origin master --force-with-lease | |
fi | |
if [ $APPLY_UPSTREAM_UPDATES -eq 1 ]; then | |
# Apply the updated upstream changes. | |
if [ $verbose ]; then | |
echo "Applying upstream updates for $SITE_ENV" | |
fi | |
terminus upstream:updates:apply "$SITE_ENV" --accept-upstream | |
fi | |
# Make sure the site is as up-to-date as possible. | |
terminus wp "$SITE_ENV" -- core update-db || true | |
terminus env:cc "$SITE_ENV" | |
# Clean up. | |
rm -rf "${sites_folder:?}/$SITE" | |
if [ $verbose ]; then | |
# Say goodbye. | |
printf "click %s to check the site\n" "$(terminus env:view "$SITE_ENV" --print)?cachebuster=1" | |
printf "%s end: %s\n\n" "$SITE" "$(date)" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment