tsm/src/config.nim

50 lines
1.3 KiB
Nim
Raw Normal View History

2024-09-14 10:15:14 -05:00
import std/[os, sequtils, strformat, strutils, parsecfg, tables]
import usu
2024-09-13 12:29:41 -05:00
import term
2024-03-18 16:19:42 -05:00
type
TsmConfig* = object
2024-09-14 10:15:14 -05:00
paths*: seq[string]
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.usu")
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)
proc loadUsuFile(p: string): UsuNode =
try:
return parseUsu(readFile p)
except:
termQuit fmt"failed to load config file\npath: {configPath}\nmessage: " & getCurrentExceptionMsg()
2024-09-14 10:15:14 -05:00
2024-03-18 16:19:42 -05:00
proc loadConfigFile(): TsmConfig =
if fileExists(configPath):
let usuNode = loadUsuFile(configPath)
let topFields = usuNode.fields
if "paths" in topFields:
for p in usuNode.fields["paths"].elems:
result.paths.add p.value
if "sessions" in topFields:
for session in usuNode.fields["sessions"].elems:
result.sessions.add Session(
# usu parser is leaving a newline at the end of first value in array?
name: session.fields["name"].value.strip(),
path: session.fields["path"].value
)
2024-03-18 16:19:42 -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