2024-09-16 18:00:44 -05:00
|
|
|
import std/[os, sequtils, strformat, strutils, tables]
|
2024-09-16 11:14:43 -05:00
|
|
|
import usu
|
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
|
|
|
|
|
2024-09-16 11:14:43 -05:00
|
|
|
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)
|
|
|
|
|
2024-09-16 11:14:43 -05:00
|
|
|
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 =
|
2024-09-16 11:14:43 -05:00
|
|
|
if fileExists(configPath):
|
|
|
|
let usuNode = loadUsuFile(configPath)
|
|
|
|
let topFields = usuNode.fields
|
|
|
|
if "paths" in topFields:
|
|
|
|
for p in usuNode.fields["paths"].elems:
|
2024-09-26 11:59:03 -05:00
|
|
|
result.paths.add p.value.strip() # usu is adding a newline....
|
2024-09-16 11:14:43 -05:00
|
|
|
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
|
|
|
|
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
|
|
|
|