Implemented minconfig.json configuration file.
h3rald h3rald@h3rald.com
Sat, 16 Jul 2016 10:32:14 +0200
3 files changed,
42 insertions(+),
1 deletions(-)
M
core/utils.nim
→
core/utils.nim
@@ -1,5 +1,11 @@
-import tables, strutils, macros, critbits, httpclient +import tables, strutils, macros, critbits, httpclient, json, os import types, parser, interpreter + +proc cfgfile*(): string = + if hostOS == "windows": + return "HOMEPATH".getEnv / "minconfig.json" + else: + return "HOME".getEnv / "minconfig.json" proc isSymbol*(s: MinValue): bool = return s.kind == minSymbol@@ -261,3 +267,27 @@ proc reqObject*(i: var MinInterpreter, a: var MinValue) =
a = i.pop if not a.isObject: raiseInvalid("An object is required on the stack") + + +proc cfgRead(): JsonNode = + return cfgfile().parseFile + +proc cfgWrite(cfg: JsonNode) = + cfgfile().writeFile(cfg.pretty) + +proc cfgGet*(key: string): JsonNode = + return cfgRead()[key] + +proc cfgSet*(key: string, value: JsonNode) = + var cfg = cfgRead() + cfg[key] = value + cfg.cfgWrite() + +proc `%`*(c: CritBitTree[string]): JsonNode = + result = newJObject() + for key, value in c.pairs: + result[key] = %value + +proc critbit*(o: JsonNode): CritBitTree[string] = + for key, value in o.pairs: + result[key] = value.getStr
M
lib/min_comm.nim
→
lib/min_comm.nim
@@ -18,11 +18,13 @@ i.reqTwoStringLike(host, address)
i.link.hosts[host.getString] = address.getString for host, response in i.syncHosts().pairs: echo host, ": ", response + cfgSet("hosts", %i.link.hosts) .symbol("unreg") do (i: In): i.link.hosts.excl(i.link.name) for host, response in i.syncHosts().pairs: echo host, ": ", response + cfgSet("hosts", %i.link.hosts) .symbol("set-hosts") do (i: In): var q: MinValue@@ -34,6 +36,7 @@ let vals = pair.qVal
if not pair.isQuotation or vals.len != 2 or not vals[0].isStringLike or not vals[1].isStringLike: raiseInvalid("Invalid host quotation") i.link.hosts.replace(vals[0].getString, vals[1].getString) + cfgSet("hosts", %i.link.hosts) i.push("OK".newVal) .symbol("hosts") do (i: In):
M
minim.nim
→
minim.nim
@@ -170,6 +170,9 @@ discard
else: discard +if not cfgfile().existsFile: + cfgfile().writeFile("{}") + if s != "": minimString(s, DEBUGGING) elif file != "":@@ -179,6 +182,11 @@ var i = newMinInterpreter(DEBUGGING)
if HOSTNAME == "": HOSTNAME = ADDRESS & ":" & $PORT var link = newMinLink(HOSTNAME, ADDRESS, PORT, i) + # Load hosts + try: + link.hosts = cfgGet("hosts").critbit + except: + discard link.hosts[HOSTNAME] = ADDRESS & ":" & $PORT echo "MiNiM v"&version&" - Host '", HOSTNAME,"' started on ", ADDRESS, ":", PORT proc srv(link: ref MinLink) =