#!/usr/bin/env python

import sys
import json
import contextlib

import click


@click.command()
@click.option(
    "-k", "--key", help="key to embed into each object", type=str, required=True
)
def main(key: str) -> None:
    """
    pipe some lines into this, creates JSON objects with --key. use like:

    seq 10 | to-json-objects -k "number" | jq '. + {"data": "something"}'

    to create a stream of JSON objects
    """
    for line in map(str.rstrip, sys.stdin):
        sys.stdout.write(json.dumps({key: line}))
        sys.stdout.write("\n")

    sys.stdout.flush()


if __name__ == "__main__":
    with contextlib.suppress(KeyboardInterrupt):
        main()
