tsm/src/config.nim

35 lines
957 B
Nim
Raw Normal View History

2024-03-20 12:17:18 -05:00
import std/[os, tables, strutils]
2024-03-19 11:57:34 -05:00
import usu
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-03-19 11:57:34 -05:00
# TODO: update when the API for usu is complete
2024-03-18 16:19:42 -05:00
proc loadConfigFile(): TsmConfig =
2024-03-20 12:17:18 -05:00
let configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.usu")
2024-03-19 11:57:34 -05:00
if fileExists configPath:
let usuNode = parseUsu(readFile configPath)
let topFields = usuNode.fields
if "dirs" in topFields:
for dir in usuNode.fields["dirs"].elems:
result.dirs.add dir.value
if "sessions" in topFields:
for session in usuNode.fields["sessions"].elems:
result.sessions.add Session(name: session.fields["name"].value,
dir: session.fields["dir"].value)
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()
2024-03-18 16:19:42 -05:00