tsm/src/config.nim

35 lines
806 B
Nim
Raw Normal View History

2024-09-13 14:46:00 -05:00
import std/[os, sequtils, streams, strformat, strutils, ]
2024-09-13 12:29:41 -05:00
import yaml
import term
2024-03-18 16:19:42 -05:00
type
TsmConfig* = object
dirs*: seq[string]
sessions*: seq[Session]
2024-03-18 16:19:42 -05:00
Session = object
name*, dir*: string
2024-03-18 16:19:42 -05:00
2024-09-13 14:46:00 -05:00
proc sessionNames*(tc: TsmConfig): seq[string] =
tc.sessions.mapIt(it.name)
2024-03-18 16:19:42 -05:00
proc loadConfigFile(): TsmConfig =
2024-09-13 12:29:41 -05:00
let configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.yml")
try:
var s = newFileStream(configPath)
load(s, result)
s.close()
except:
termError fmt(
"failed to load config file\npath: {configPath}\nmessage: {getCurrentExceptionMsg()}"
)
2024-03-18 16:19:42 -05:00
proc loadTsmConfig*(): TsmConfig =
2024-03-18 16:19:42 -05:00
result = loadConfigFile()
let tsmDirs = getEnv("TSM_DIRS")
if tsmDirs != "":
result.dirs = tsmDirs.split(":")
when isMainModule:
2024-03-19 11:57:34 -05:00
echo loadConfigFile()