refactor!!: go back to usu because why not

This commit is contained in:
Daylin Morgan 2024-09-16 11:14:43 -05:00
parent 8c866db5f0
commit abcb1ee301
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 28 additions and 22 deletions

View file

@ -31,14 +31,14 @@
"sha1": "9c58351502f89a16caf031cbd1992ad3fdfd3c67"
}
},
"yaml": {
"version": "2.1.1",
"vcsRevision": "48a90e36e82bd97457dae87e86efe423dcd3bb40",
"url": "https://github.com/flyx/NimYAML",
"usu": {
"version": "0.1.0",
"vcsRevision": "25574d165bb7f60900b36590bc49f3d90e47cea4",
"url": "https://github.com/usu-dev/usu-nim",
"downloadMethod": "git",
"dependencies": [],
"checksums": {
"sha1": "302727fcd74c79d0697a4e909d26455d61a5b979"
"sha1": "61a8c13946e3eea9dbe04a6141ed93811563026c"
}
}
},

View file

@ -1,4 +1,5 @@
import std/[os, sequtils, strformat, strutils, parsecfg, tables]
import usu
import term
type
@ -9,28 +10,33 @@ type
Session = object
name*, path*: string
var configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.ini")
var configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.usu")
proc sessionNames*(tc: TsmConfig): seq[string] =
tc.sessions.mapIt(it.name)
template check(cond: bool, msg: string) =
if not cond:
termQuit fmt"failed to load config file\npath: {configPath}\nmessage: " & msg
proc loadUsuFile(p: string): UsuNode =
try:
return parseUsu(readFile p)
except:
termQuit fmt"failed to load config file\npath: {configPath}\nmessage: " & getCurrentExceptionMsg()
proc loadConfigFile(): TsmConfig =
let dict = loadConfig(configPath)
for k, v in dict.pairs:
if k == "paths":
for path, v2 in v.pairs:
check v2 == "", fmt"unexpected value in [paths] section {v}"
result.paths.add path
else:
check k.startsWith("session."), fmt"unexpected config section: {k}"
let name = k.replace("session.")
check v.hasKey("path"), fmt"expected value for path in section: {k}"
let path = v["path"]
result.sessions.add Session(name:name, path:path)
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
)
proc loadTsmConfig*(): TsmConfig =
result = loadConfigFile()

View file

@ -14,5 +14,5 @@ requires "nim >= 2.0.0"
requires "illwill >= 0.4.1"
requires "cligen"
requires "https://github.com/daylinmorgan/bbansi >= 0.1.1"
requires "yaml"
requires "https://github.com/usu-dev/usu-nim"