Last active
November 28, 2017 18:22
-
-
Save caius/a06091f9f8612269d5b8ee3d2d46f222 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
# | |
# Diffs the order of octodns zone files on disk against what order they | |
# are expected to be in by octodns. Fills in a gap with octodns-valiate | |
# output to make it easier to write correct config files by hand. | |
# | |
# Expects a number of arguments, each being a path to a yaml config file. | |
# | |
# Outputs a diff for a file path if there are changes required, otherwise | |
# outputs nothing. | |
# | |
# Exits 0 on success, or > 0 on error. | |
# | |
# USAGE: | |
# octodns-order-diff config/zones/*.yaml | |
# | |
# Make sure we've at least one file to check | |
if [[ -z $1 ]]; then | |
echo "ERROR: Zone config files required as arguments" | |
exit 1 | |
fi | |
exitcode=0 | |
for zonepath in ${BASH_ARGV[*]} ; do | |
# Check this zone file exists | |
if [[ ! -f $zonepath ]]; then | |
echo "ERROR: zone file '${zonepath}' does not exist" >>/dev/stderr | |
exitcode=2 | |
continue | |
fi | |
# Diff keys as written in the file (read in ruby, as that preserves order) | |
# against keys as sorted by the same python module octodns uses. | |
# | |
# Outputs a unified diff if there's any changes | |
diff -U10 \ | |
<(ruby -ryaml -e 'puts YAML.load(ARGF.read).keys' -- $zonepath) \ | |
<(python -c '#!/usr/bin/env python | |
from __future__ import absolute_import, division, print_function, \ | |
unicode_literals | |
import sys | |
from yaml import load, Loader, Dumper | |
from natsort import natsorted | |
filepath = sys.argv[1] | |
with open(filepath, "r") as fh: | |
zonedata = load(fh, Loader=Loader) | |
sorted_records = natsorted(dict.keys(zonedata)) | |
for k in sorted_records: | |
print(k) | |
' $zonepath) | awk "NR == 1 { print(\"+++ $zonepath\"); next }; NR == 2 { print (\"--- $zonepath\"); next }; { print \$0 }" | |
done | |
exit $exitcode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment