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" "sha1": "9c58351502f89a16caf031cbd1992ad3fdfd3c67"
} }
}, },
"yaml": { "usu": {
"version": "2.1.1", "version": "0.1.0",
"vcsRevision": "48a90e36e82bd97457dae87e86efe423dcd3bb40", "vcsRevision": "25574d165bb7f60900b36590bc49f3d90e47cea4",
"url": "https://github.com/flyx/NimYAML", "url": "https://github.com/usu-dev/usu-nim",
"downloadMethod": "git", "downloadMethod": "git",
"dependencies": [], "dependencies": [],
"checksums": { "checksums": {
"sha1": "302727fcd74c79d0697a4e909d26455d61a5b979" "sha1": "61a8c13946e3eea9dbe04a6141ed93811563026c"
} }
} }
}, },

View file

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

View file

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