From abcb1ee3010bb3f822a9ce76e17df01d733c695f Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 16 Sep 2024 11:14:43 -0500 Subject: [PATCH] refactor!!: go back to usu because why not --- nimble.lock | 10 +++++----- src/config.nim | 38 ++++++++++++++++++++++---------------- tsm.nimble | 2 +- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/nimble.lock b/nimble.lock index f0a214e..5215ac8 100644 --- a/nimble.lock +++ b/nimble.lock @@ -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" } } }, diff --git a/src/config.nim b/src/config.nim index 97cb290..96d60a3 100644 --- a/src/config.nim +++ b/src/config.nim @@ -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() diff --git a/tsm.nimble b/tsm.nimble index a4fe0ff..e77e7d1 100644 --- a/tsm.nimble +++ b/tsm.nimble @@ -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"