Created
September 11, 2023 14:06
-
-
Save orisano/582a4397ef2183da291c15a401bcf241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import os | |
import os.path | |
import sys | |
import urllib.request | |
def grep(f, text): | |
for line in f: | |
t = line.decode() | |
if text in t: | |
return t.strip() | |
url = sys.argv[1] | |
allow_list = ["query.sql", "sqlc.json"] | |
prefix = """<script type="application/json" id="input">""" | |
with urllib.request.urlopen(url) as f: | |
x = grep(f, prefix) | |
raw = x.removeprefix(prefix).removesuffix("</script>") | |
data = json.loads(raw) | |
pg_dir = os.path.join("tmp", url.split("/")[-1]) | |
os.makedirs(pg_dir, exist_ok=True) | |
for f in data["files"]: | |
if f["name"] not in allow_list: | |
continue | |
p = os.path.join(pg_dir, f["name"]) | |
with open(p, "w") as wf: | |
wf.write(f["contents"]) | |
with open(os.path.join(pg_dir, "sqlc.json")) as f: | |
sqlc = json.load(f) | |
for p in sqlc["packages"]: | |
p["path"] = "go" | |
p["name"] = "querytest" | |
with open(os.path.join(pg_dir, "sqlc.json"), "w") as wf: | |
json.dump(sqlc, wf, indent=4) | |
with open(os.path.join(pg_dir, "query.sql")) as f: | |
queries = f.read().split("\n-- name:")[1:] | |
for i, q in enumerate(queries, 1): | |
body = q.split("\n", 1)[1] | |
with open(os.path.join(pg_dir, "query{}.sql".format(i)), "w") as wf: | |
wf.write(body) | |
with open(os.path.join(pg_dir, "cmd_test.go"), "w") as f: | |
f.write(""" | |
package sqlc_test | |
import ( | |
"os" | |
"testing" | |
"github.com/sqlc-dev/sqlc/internal/cmd" | |
) | |
func TestDo(t *testing.T) { | |
cmd.Do([]string{"generate"}, os.Stdin, os.Stdout, os.Stderr) | |
} | |
""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment