diff --git a/yaml2json b/yaml2json index da02a4d..9aef73a 100755 --- a/yaml2json +++ b/yaml2json @@ -1,17 +1,20 @@ -#!/usr/bin/env bash - +#!/usr/bin/env python3 +# # YAML to JSON conversion script # Based on https://www.geeksforgeeks.org/convert-yaml-to-json/ # -# This script takes a YAML file as the first arg and outputs the -# converted JSON to stdout. +# This script takes a YAML file as the first arg, converts the +# YAML content to JSON, and outputs the converted JSON content +# to stdout. -set -e +import json +import sys -if ! python -c "import yaml" &>/dev/null; then - echo -e "PyYAML is not installed." -fi +import yaml -if ! python -c "import yaml, json; print(json.dumps(yaml.load(open('${1}'), Loader=yaml.FullLoader), indent=4))"; then - echo "Some kinda error occurred while attempting to convert YAML to JSON" -fi +try: + print(json.dumps(yaml.load(open(sys.argv[1]), Loader=yaml.FullLoader), indent=4)) +except IndexError: + print("YAML file must be supplied as first arg") +except FileNotFoundError: + print("YAML file not found")