Skip to content

Instantly share code, notes, and snippets.

@shanerk
Created March 22, 2024 18:47
Show Gist options
  • Save shanerk/52d317a14c71ecdcb7ee90eb128abf72 to your computer and use it in GitHub Desktop.
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
#!/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