#!/bin/bash
#
# Requires python3. Reads YAML on stdin; outputs JSON on stdout.

set -e

PYTHON=python3

function fail() {
  echo > /dev/stderr
  echo "$*" > /dev/stderr
  exit 1
}

$PYTHON -c '' || fail "Could not find Python 3 at \"$PYTHON\". Do you have it installed?"
$PYTHON -c 'import yaml' || fail "Could not import yaml in Python; try running 'pip3 install pyyaml'"

if [ -t 0 ]; then
  echo "Please input the YAML on standard input." > /dev/stderr
  echo > /dev/stderr
fi

python3 -c "import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin.read())))"
