Created
March 22, 2024 18:47
-
-
Save shanerk/52d317a14c71ecdcb7ee90eb128abf72 to your computer and use it in GitHub Desktop.
Bash script to perform destructive changes against a Salesforce Org based on a GIT diff
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
#!/bin/bash | |
cleanExit() { | |
#rm dchanges.txt* | |
exit | |
} | |
QUICK=false | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
-d) | |
QUICK=true | |
shift | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
echo "Invalid option: $1" | |
exit 1 ## Could be optional. | |
;; | |
esac | |
shift | |
done | |
STATUS=$(git status) | |
if [[ "${STATUS}" != *"nothing to commit, working tree clean"* ]] ;then | |
echo 'Working tree must be clean to run this command. Please commit your changes and try again.' | |
cleanExit | |
fi | |
BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
if [ $QUICK != 'true' ] | |
then | |
## Get the default org | |
# echo "π Getting orgs..." | |
# DORG="" | |
# ORGS=$(sf org list) | |
# echo $ORGS | |
# set -o noglob | |
# IFS=$'\n' arr=($ORGS) | |
# set +o noglob | |
# for i in "${arr[@]}" | |
# do | |
# l="$(echo "${i}" | sed -e 's/^[[:space:]]*//')" | |
# if [[ $l = "π*" ]] || [[ $1 = "| (U) " ]] | |
# then | |
# IFS=$' ' arr2=($l) | |
# DORG="${arr2[2]}" | |
# fi | |
# done | |
DORG="dev" | |
#echo "π₯ Default org = $DORG" | |
## Get revision ID | |
echo "π₯ Please provide the GIT revision ID, this will be used to diff against your current HEAD (i.e. release/1.39 or HEAD~1)" | |
read -p "π₯ GIT Revision ID: " GIT_REV | |
ANSWER=${ANSWER:-HEAD} | |
echo | |
## Confirm manifest is correct | |
echo "π₯ Destroy manifest:" | |
echo " =================" | |
git diff $GIT_REV --diff-filter=D --name-status --color -- force-app/ | cat | |
echo | |
read -p "π₯ Confirm the destroy manifest above is correct (Y/n): " ANSWER | |
ANSWER=${ANSWER:-Y} | |
case ${ANSWER:0:1} in | |
y|Y ) | |
;; | |
* ) | |
echo "π₯ Try a different GIT revision ID. Exiting..." | |
cleanExit | |
;; | |
# Continue | |
esac | |
else | |
GIT_REV=HEAD~1 | |
fi | |
# Convert manifest to SFDX format | |
git diff $GIT_REV --diff-filter=D --name-only --color -- force-app/ > temp | |
tr '\n' ',' < temp > dchanges.txt # replace newlines with commas | |
rm temp | |
sed -i -e "s/\,/' '/g" dchanges.txt # put each filename in its own quotes | |
echo "'$(cat dchanges.txt)" > dchanges.txt # fix start of file | |
sed -i -e "s/' '$/'/g" dchanges.txt # fix end of file | |
CHANGES=$(<dchanges.txt) | |
rm dchanges.txt* | |
if [ $QUICK != 'true' ] | |
then | |
echo | |
read -p "π₯ Target org for destructive changes ($DORG): " ORG | |
ORG=${ORG:-$DORG} | |
if [ -z "$ORG" ] | |
then | |
echo "No org provided. Exiting..." | |
cleanExit | |
fi | |
echo | |
echo "π₯ Destroy command:" | |
echo " ================" | |
echo "sfdx project delete source -r -o $ORG -p $CHANGES" | |
echo | |
read -p "π₯ To execute the destroy command above, type (d): " ANSWER | |
case ${ANSWER:0:1} in | |
d|D ) | |
;; | |
* ) | |
echo "π₯ No operations performed. Exiting..." | |
cleanExit | |
;; | |
# Continue | |
esac | |
fi | |
echo | |
echo "π₯ Peforming delete operations..." | |
echo | |
if [ $QUICK != 'true' ] | |
then | |
# We have to checkout the old rev because the deleted files are no longer in the current | |
git checkout $GIT_REV -q | |
eval $"sf project delete source -r -o $ORG -p $CHANGES" | |
git checkout $BRANCH -q | |
read -p "π₯ Operation complete. Open SFDX org? (y/N): " | |
ANSWER=${ANSWER:-N} | |
case ${ANSWER:0:1} in | |
y|Y ) | |
echo | |
sfdx force:org:open -u $ORG | |
;; | |
* ) | |
;; | |
# Continue | |
esac | |
echo | |
else | |
echo "π₯ Performing delete operations against default org..." | |
echo | |
# We have to checkout the old rev because the deleted files are no longer in the current | |
git checkout $GIT_REV -q | |
eval $"sf project delete source -r -p $CHANGES" | |
git checkout $BRANCH -q | |
fi | |
echo | |
echo "π₯ Exiting..." | |
cleanExit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment