#!/usr/bin/env python3
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]

"""

from __future__ import print_function
import sys
import json
import collections


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

if __name__ == '__main__':
  main()