syndicate_utils/src/json_translator.nim

41 lines
1007 B
Nim

# SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[asyncdispatch, json, os, osproc, strutils]
import preserves # , preserves/jsonhooks,
import syndicate
from preserves/jsonhooks import toPreserveHook
proc runChild: string =
let params = commandLineParams()
if params.len < 1:
stderr.writeLine "not enough parameters"
quit 1
let
cmd = params[0]
args = params[1..params.high]
try: result = execProcess(command=cmd, args=args, options={poUsePath})
except:
stderr.writeLine "execProcess failed"
quit 1
if result == "":
stderr.writeLine "no ouput"
quit 1
proc translate(output: string): Assertion =
var js: JsonNode
try: js = parseJson output
except:
stderr.writeLine "parseJson failed"
quit 1
js.toPreserveHook(Ref)
bootDataspace("main") do (root: Ref; turn: var Turn):
connectStdio(root, turn)
discard publish(turn, root,
initRecord[Ref]("output", runChild().translate()))
runForever()