#!/usr/bin/env python
r"""Command-line tool to format software release JSON for slapos.

Inspired by json.tool from python, but enforcing 2 spaces and non-sorted keys.
The files are modified in-place.

Usage::

    format-json file1.json [file2.json]

"""

import os
import sys
import json
import collections


def main():
  exit_code = 0
  for f in sys.argv[1:]:
    print 'Processing %s' % (f,)
    with open(f, 'rb') as infile:
      try:
        obj = json.load(infile, object_pairs_hook=collections.OrderedDict)
      except ValueError as e:
        exit_code = 1
        print e
      else:
        with open(f, 'wb') as outfile:
          json.dump(obj, outfile, sort_keys=False, indent=2, separators=(',', ': '))
          outfile.write('\n')
  sys.exit(exit_code)

if __name__ == '__main__':
  main()
