mirror of
https://github.com/daylinmorgan/tsm.git
synced 2024-12-22 05:00:44 -06:00
feat: start config implementation
This commit is contained in:
parent
31bfbc1a94
commit
70a828b91d
1 changed files with 39 additions and 0 deletions
39
src/config.nim
Normal file
39
src/config.nim
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import std/[os, parsecfg, sequtils, tables, strutils]
|
||||||
|
|
||||||
|
import term
|
||||||
|
|
||||||
|
type
|
||||||
|
TsmConfig = object
|
||||||
|
dirs: seq[string]
|
||||||
|
sessions: seq[Session]
|
||||||
|
|
||||||
|
Session = object
|
||||||
|
name, dir: string
|
||||||
|
|
||||||
|
proc loadConfigFile(): TsmConfig =
|
||||||
|
let configPath = getHomeDir() / ".config/tsm/config.ini"
|
||||||
|
if configPath.fileExists():
|
||||||
|
let iniTable = loadConfig(configPath)
|
||||||
|
if "sessions" in iniTable:
|
||||||
|
for key, value in iniTable["sessions"].pairs:
|
||||||
|
result.sessions.add Session(name: key, dir: value)
|
||||||
|
if "dirs" in iniTable:
|
||||||
|
for key, value in iniTable["dirs"].pairs:
|
||||||
|
if value != "":
|
||||||
|
termError "[dirs] table should only contain a list of paths"
|
||||||
|
result.dirs.add key
|
||||||
|
|
||||||
|
proc loadTsmConfig(): TsmConfig =
|
||||||
|
result = loadConfigFile()
|
||||||
|
let tsmDirs = getEnv("TSM_DIRS")
|
||||||
|
if tsmDirs != "":
|
||||||
|
result.dirs = tsmDirs.split(":")
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
let dict = loadConfig(getHomeDir() / ".config/tsm/config.ini")
|
||||||
|
let sections = dict.sections().toSeq()
|
||||||
|
if "sessions" in sections:
|
||||||
|
for key, value in dict["sessions"].pairs:
|
||||||
|
echo key, value
|
||||||
|
|
||||||
|
echo loadTsmConfig()
|
Loading…
Reference in a new issue