2024-09-14 10:15:14 -05:00
|
|
|
import std/[os, sequtils, strformat, strutils, parsecfg, tables]
|
2024-09-13 12:29:41 -05:00
|
|
|
import term
|
2024-03-18 16:19:42 -05:00
|
|
|
|
|
|
|
type
|
2024-03-18 16:43:38 -05:00
|
|
|
TsmConfig* = object
|
2024-09-14 10:15:14 -05:00
|
|
|
paths*: seq[string]
|
2024-03-18 16:43:38 -05:00
|
|
|
sessions*: seq[Session]
|
2024-03-18 16:19:42 -05:00
|
|
|
|
|
|
|
Session = object
|
2024-09-14 10:15:14 -05:00
|
|
|
name*, path*: string
|
|
|
|
|
|
|
|
var configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.ini")
|
2024-03-18 16:19:42 -05:00
|
|
|
|
2024-09-13 14:47:45 -05:00
|
|
|
proc sessionNames*(tc: TsmConfig): seq[string] =
|
2024-09-13 14:46:00 -05:00
|
|
|
tc.sessions.mapIt(it.name)
|
|
|
|
|
2024-09-14 10:15:14 -05:00
|
|
|
template check(cond: bool, msg: string) =
|
|
|
|
if not cond:
|
|
|
|
termQuit fmt"failed to load config file\npath: {configPath}\nmessage: " & msg
|
|
|
|
|
2024-03-18 16:19:42 -05:00
|
|
|
proc loadConfigFile(): TsmConfig =
|
2024-09-14 10:15:14 -05:00
|
|
|
let dict = loadConfig(configPath)
|
|
|
|
for k, v in dict.pairs:
|
|
|
|
if k == "paths":
|
|
|
|
for path, v2 in v.pairs:
|
|
|
|
check v2 == "", fmt"unexpected value in [paths] section {v}"
|
|
|
|
result.paths.add path
|
|
|
|
else:
|
|
|
|
check k.startsWith("session."), fmt"unexpected config section: {k}"
|
|
|
|
let name = k.replace("session.")
|
|
|
|
check v.hasKey("path"), fmt"expected value for path in section: {k}"
|
|
|
|
let path = v["path"]
|
|
|
|
result.sessions.add Session(name:name, path:path)
|
2024-03-18 16:19:42 -05:00
|
|
|
|
2024-03-18 16:43:38 -05:00
|
|
|
proc loadTsmConfig*(): TsmConfig =
|
2024-03-18 16:19:42 -05:00
|
|
|
result = loadConfigFile()
|
2024-09-14 10:15:14 -05:00
|
|
|
let tsmDirs = getEnv("TSM_PATHS")
|
2024-03-18 16:19:42 -05:00
|
|
|
if tsmDirs != "":
|
2024-09-14 10:15:14 -05:00
|
|
|
result.paths = tsmDirs.split(":")
|
2024-03-18 16:19:42 -05:00
|
|
|
|
|
|
|
when isMainModule:
|
2024-03-19 11:57:34 -05:00
|
|
|
echo loadConfigFile()
|
2024-09-14 10:15:14 -05:00
|
|
|
|