mirror of
https://codeberg.org/hyperreal/bin
synced 2024-11-01 16:43:08 +01:00
Rewrite yaml2json in Python
This commit is contained in:
parent
fdad459398
commit
6257cfde0d
25
yaml2json
25
yaml2json
@ -1,17 +1,20 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env python3
|
||||||
|
#
|
||||||
# YAML to JSON conversion script
|
# YAML to JSON conversion script
|
||||||
# Based on https://www.geeksforgeeks.org/convert-yaml-to-json/
|
# Based on https://www.geeksforgeeks.org/convert-yaml-to-json/
|
||||||
#
|
#
|
||||||
# This script takes a YAML file as the first arg and outputs the
|
# This script takes a YAML file as the first arg, converts the
|
||||||
# converted JSON to stdout.
|
# 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
|
import yaml
|
||||||
echo -e "PyYAML is not installed."
|
|
||||||
fi
|
|
||||||
|
|
||||||
if ! python -c "import yaml, json; print(json.dumps(yaml.load(open('${1}'), Loader=yaml.FullLoader), indent=4))"; then
|
try:
|
||||||
echo "Some kinda error occurred while attempting to convert YAML to JSON"
|
print(json.dumps(yaml.load(open(sys.argv[1]), Loader=yaml.FullLoader), indent=4))
|
||||||
fi
|
except IndexError:
|
||||||
|
print("YAML file must be supplied as first arg")
|
||||||
|
except FileNotFoundError:
|
||||||
|
print("YAML file not found")
|
||||||
|
Loading…
Reference in New Issue
Block a user