Skip to content

Instantly share code, notes, and snippets.

@earonesty
Last active May 10, 2022 21:27
Show Gist options
  • Save earonesty/1d7cb531bb8fff8c228b7710126bcc33 to your computer and use it in GitHub Desktop.
Save earonesty/1d7cb531bb8fff8c228b7710126bcc33 to your computer and use it in GitHub Desktop.
#!/bin/env python
import yaml
import sys
import json
import argparse
from subprocess import Popen, PIPE
import argparse
import os
exists=os.path.isfile
def json_serial(obj):
# all obj's dumped to str
return str(obj)
# look for jq arguments, vs file arguments
offset=1
for arg in sys.argv[1:]:
if arg and arg[0] == '-' and not exists(arg):
offset+=1
else:
break
# construct arguments so that it looks like jq
files=[]
frm=[0]
index=offset
for arg in sys.argv[offset:]:
if exists(arg):
files.append(arg)
frm.insert(0,index)
index+=1
for index in frm:
sys.argv.pop(index)
if sys.argv:
# jq args are present
args=["jq"]+sys.argv
pipe = Popen(args, stdin=PIPE).stdin
else:
# no jq args... just dump to stdout
pipe = sys.stdout
if files:
for fin in files:
json.dump(yaml.load(open(fin)),pipe,default=json_serial)
else:
json.dump(yaml.load(sys.stdin),pipe,default=json_serial)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment