Created
August 21, 2013 20:38
-
-
Save pix0r/6299935 to your computer and use it in GitHub Desktop.
in-place cleanjson
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
#!/bin/sh | |
if [ "$1" = "" ]; then | |
echo "Usage: $0 <file.json> [file2.json...]" | |
exit -1 | |
fi | |
CLEANJSON=`which cleanjson` | |
if [ "$CLEANJSON" = "" ]; then | |
echo "Please install cleanjson and ensure it's on your PATH" | |
echo "E.g. npm install -g cleanjson" | |
exit -1 | |
fi | |
TEMPDIR=`mktemp -d -t cleanjson` | |
if [ ! -d $TEMPDIR ]; then | |
echo "Error creating temp directory" | |
exit -1 | |
fi | |
for file in $*; do | |
if [ ! -f $file ]; then | |
echo "File not found: $file" | |
rm -rf $TEMPDIR | |
exit -1 | |
fi | |
dir=`dirname $file` | |
mkdir -p $TEMPDIR/$dir | |
cat $file | $CLEANJSON > $TEMPDIR/$file | |
if [ $? -ne 0 ]; then | |
echo "Error cleaning $file" | |
rm -rf $TEMPDIR | |
exit -1 | |
fi | |
mv $TEMPDIR/$file $file | |
if [ $? -ne 0 ]; then | |
echo "Error replacing $file" | |
rm -rf $TEMPDIR | |
exit -1 | |
fi | |
done | |
rm -rf $TEMPDIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment