#!/usr/bin/env python
#
# wrap puppet-lint --fix so that it can be made compatible with the way that
# kak runs its formatters

import subprocess
import sys
import tempfile

(_, name) = tempfile.mkstemp(prefix='kak-puppet-fix')
with open(name, 'w') as old:
    line = sys.stdin.readline()
    while len(line) > 0:
        old.write(line)
        old.flush()
        line = sys.stdin.readline()

    # we need to dump all output somewhere quiet
    devnull = open("/dev/null", 'w')
    subprocess.call(["puppet-lint", "--fix", name],
                    stdout=devnull,
                    stderr=devnull)

    # at this point the file may have been rewritten, so we need to reopen it
    with open(name) as new:
        line = new.read()
        while len(line) > 0:
            sys.stdout.write(line)
            sys.stdout.flush()
            line = new.read()
