Skip to content

Instantly share code, notes, and snippets.

@themightychris
Created February 7, 2019 04:39
Show Gist options
  • Save themightychris/6b91b7d02773dfe03895ae1282281285 to your computer and use it in GitHub Desktop.
Save themightychris/6b91b7d02773dfe03895ae1282281285 to your computer and use it in GitHub Desktop.
batch json shell transforms with underscore cli
/*
data.label = "Slate: "+data.label;
data.handle = "slate-"+data.handle;
data.mysql.database = "slate-"+data.mysql.database;
data.mysql.username = "slate-"+data.mysql.username;
data.parent_hostname = data.parent_hostname.replace(/([a-z]a\.slate)/, 'wa-$1');
data.primary_hostname = data.primary_hostname.replace(/([a-z]a\.slate)/, 'wa-$1');
*/
/*
if (data.parent_hostname == 'v2.slate.is') {
data.parent_hostname = 'v3.cbl.slate.is';
data.parent_key = 'x3b96wjVy2TsJSQt';
}
*/
if (data.parent_hostname == 'v3.cbl.slate.is') {
data.parent_hostname = 'sub-skeleton.slatepowered.net';
data.parent_key = 'abcedef';
}
data;
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: process.js <diff|apply>"
exit 1
fi
process_script="$(cat ~/process.js)"
for site in *; do
json_path="${site}/site.json"
tmp_path="${site}/site.json.tmp"
bak_path="${site}/site.json.bak"
echo "${json_path}"
# diff or apply file
if [ "$1" == "apply" ]; then
echo "Patching to ${tmp_path}"
cat "${json_path}" | underscore process "${process_script}" > "${tmp_path}"
if [ $? -eq 0 ]; then
colordiff "${json_path}" "${tmp_path}"
echo "Moving ${json_path} to ${bak_path}"
mv "${json_path}" "${bak_path}"
echo "Moving ${tmp_path} to ${json_path}"
mv "${tmp_path}" "${json_path}"
fi
else
colordiff "${json_path}" <(cat "${json_path}" | underscore process "${process_script}")
fi
# Blank line
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment