File size: 1,375 Bytes
d02c2a6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | """Example UI script invoked via POST /api/scripts.
The API streams stdout/stderr back to the caller. To return structured data,
print a single JSON object on the final line of stdout — the route parses it
into the `result` field of the response.
"""
import argparse
import json
import sys
import time
def main() -> int:
parser = argparse.ArgumentParser(description="Example ui_scripts entry point.")
parser.add_argument("--message", default="hello", help="Message to echo back.")
parser.add_argument(
"--count", type=int, default=3, help="Number of log lines to emit."
)
parser.add_argument(
"--delay", type=float, default=0.0, help="Seconds to sleep between log lines."
)
parser.add_argument(
"--fail", action="store_true", help="Exit non-zero to demo failure handling."
)
args = parser.parse_args()
for i in range(args.count):
print(f"[{i + 1}/{args.count}] {args.message}", flush=True)
if args.delay > 0:
time.sleep(args.delay)
if args.fail:
print("intentional failure", file=sys.stderr, flush=True)
return 1
# Final line: JSON payload the API surfaces as `result`.
print(
json.dumps({"message": args.message, "count": args.count, "ok": True}),
flush=True,
)
return 0
if __name__ == "__main__":
sys.exit(main())
|