#!/usr/bin/env python3 """Minimal jq stand-in for entrypoint.sh tests. Only the jq expressions used by entrypoint.sh are implemented. Unknown expressions fail loudly so new jq usage is noticed instead of silently producing wrong results. """ import json import re import sys expr = "" flags = set() named_args = {} args = sys.argv[1:] while args: current = args.pop(0) if current in {"-e", "-c", "-r", "-s"}: flags.add(current) continue if current == "--arg": key = args.pop(0) value = args.pop(0) named_args[key] = value continue if current == "--": if not args: raise SystemExit("missing jq expression") expr = args.pop(0) break if current.startswith("-"): flags.add(current) continue expr = current break raw = sys.stdin.read() def dump(value): if "-r" in flags and isinstance(value, (str, int, float)) and not isinstance(value, bool): sys.stdout.write(str(value)) else: sys.stdout.write(json.dumps(value, separators=(",", ":"))) if expr == "length": data = json.loads(raw or "null") print(len(data)) raise SystemExit(0) if expr == "add": arrays = [json.loads(line) for line in raw.splitlines() if line.strip()] merged = [] for item in arrays: merged.extend(item) dump(merged) raise SystemExit(0) if expr == "sort_by(.created_at) | reverse": data = json.loads(raw or "[]") dump(sorted(data, key=lambda item: item["created_at"], reverse=True)) raise SystemExit(0) if expr == "[.[].tag_name]": data = json.loads(raw or "[]") dump([item["tag_name"] for item in data]) raise SystemExit(0) match = re.fullmatch(r"any\(\.\[\]; \. == \$(\w+)\)", expr) if match: data = json.loads(raw or "[]") needle = named_args[match.group(1)] sys.stdout.write("true" if any(item == needle for item in data) else "false") raise SystemExit(0) match = re.fullmatch(r"\.\[(\d+):\]", expr) if match: data = json.loads(raw or "[]") dump(data[int(match.group(1)):]) raise SystemExit(0) if expr == ".[]": data = json.loads(raw or "[]") for item in data: dump(item) sys.stdout.write("\n") raise SystemExit(0) match = re.fullmatch(r"\.(id|tag_name|name)", expr) if match: data = json.loads(raw or "null") value = data.get(match.group(1)) dump(value) raise SystemExit(0) raise SystemExit(f"unsupported jq expression: {expr}")