Skip to content

Instantly share code, notes, and snippets.

@jclosure
Created January 8, 2022 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jclosure/8e1c34f0588bd77367790f6ea488d6fe to your computer and use it in GitHub Desktop.
Save jclosure/8e1c34f0588bd77367790f6ea488d6fe to your computer and use it in GitHub Desktop.
modify yaml on the fly as part of a make task
UNAME := $(shell uname)
###
#
# assuming a yaml like this:
# main:
# device: 21
#
###
# semantic parsing and changing of the yaml
set-device-mode:
ifeq ($(UNAME), Linux)
if ! command -v yq &> /dev/null ; then \
wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64; \
chmod a+x /usr/local/bin/yq; \
fi
endif
yq e -i '.main.device = 22' ./params.yaml
run: set-device-mode
echo "starting program"
@jclosure
Copy link
Author

jclosure commented Jan 8, 2022

Use the following pre-commit hook to revert it before checking back into VCS:

#!/bin/sh

# This hook ensures that we do not checkin a mod to the params.yaml, etc.

echo "resetting params to HEAD"
git checkout HEAD -- params.yaml
git add params.yaml

exit 0

@jclosure
Copy link
Author

jclosure commented Jan 8, 2022

Test replacement regex matches easily in shell:

sed -E 's/(\s+device:\s)(21)/\122/g' ./spiff_params/params.yaml  | diff params.yaml -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment