syndicate_utils/src/json_translator.nim

41 lines
1.0 KiB
Nim
Raw Normal View History

2023-05-06 19:08:38 +00:00
# SPDX-FileCopyrightText: ☭ Emery Hemingway
2022-06-09 05:28:26 +00:00
# SPDX-License-Identifier: Unlicense
2023-05-18 11:41:37 +00:00
import std/[json, os, osproc]
2022-06-09 05:28:26 +00:00
import preserves # , preserves/jsonhooks,
import syndicate
from preserves/jsonhooks import toPreserveHook
import ./schema/config
2022-06-09 05:28:26 +00:00
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)
2023-05-06 19:08:38 +00:00
runActor("main") do (root: Ref; turn: var Turn):
2022-06-09 05:28:26 +00:00
connectStdio(root, turn)
2023-05-06 19:08:38 +00:00
during(turn, root, ?JsonTranslatorArguments) do (ds: Ref):
message(turn, ds, initRecord[Ref]("recv-json", runChild().translate()))
2022-06-09 05:28:26 +00:00