Skip to content

Instantly share code, notes, and snippets.

@ajm188
Last active May 7, 2025 18:45
Show Gist options
  • Save ajm188/e3c7a06e8d95482d9a0a29ed742c5457 to your computer and use it in GitHub Desktop.
Save ajm188/e3c7a06e8d95482d9a0a29ed742c5457 to your computer and use it in GitHub Desktop.
edit yaml across a bunch of repos
#!/bin/sh
set -euo pipefail
wd=$(pwd)
trap cleanup SIGINT SIGKILL
cleanup() {
cd $wd >/dev/null
}
DIRECTORY_GLOB="."
FILENAME_GLOB="*"
FILE_FILTER=".*"
UPDATE=
COMMIT_MESSAGE=""
BRANCH_NAME=
while [[ $# -gt 0 ]]; do
case $1 in
-d|--dir)
DIRECTORY_GLOB="$2"; shift; shift;
;;
-n|--name)
FILENAME_GLOB="$2"; shift; shift;
;;
-f|--filter)
FILE_FILTER="$2"; shift; shift;
;;
-b|--branch)
BRANCH_NAME="$2"; shift; shift;
;;
-u|--update)
# For example, set a deeply-nested key with filtering: (.foo.bar[] | select(.somekey == "someval").resources.requests.cpu) |= "100m")
UPDATE="$2"; shift; shift;
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
if [ -n "$COMMIT_MESSAGE" ]; then
echo "can only pass one posarg"
exit 1
fi
COMMIT_MESSAGE="$1"; shift
;;
esac
done
if [ -z "$BRANCH_NAME" ]; then
BRANCH_NAME="$(echo $COMMIT_MESSAGE | tr ' ' '-' | tr '[:upper:]' '[:lower:]')"
fi
for dir in $(find $DIRECTORY_GLOB -name "${FILENAME_GLOB}" -exec grep -l "${FILE_FILTER}" {} \; | cut -d/ -f2);
do
cd "./${dir}"
base=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
git stash save
git switch $base
git pull
git switch -c "$BRANCH_NAME"
find . \
-name "${FILENAME_GLOB}" \
-exec grep -l "${FILE_FILTER}" {} \; \
-exec yq -i "$UPDATE" {} \;
if [ -n "$(git status --porcelain)" ]; then
git add .
git commit -m "$COMMIT_MESSAGE"
git push origin $BRANCH_NAME
gh pr create --fill
fi
git switch $base
cd -
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment