Extract logic for locating the config file to the cliutils library.

This commit is contained in:
Jonathan Bernard 2023-05-13 07:30:25 -05:00
parent b0e3f5a9d8
commit bcb1c7c17c
3 changed files with 13 additions and 20 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "4.23.2"
version = "4.23.3"
author = "Jonathan Bernard"
description = "Personal issue tracker."
license = "MIT"
@ -20,7 +20,7 @@ requires @[
# Dependencies from git.jdb-software.com/nim-jdb/packages
requires @[
"cliutils >= 0.6.4",
"cliutils >= 0.8.1",
"langutils >= 0.4.0",
"timeutils >= 0.5.4",
"data_uri > 1.0.0",
@ -28,4 +28,4 @@ requires @[
]
task updateVersion, "Update the version of this package.":
exec "update_nim_package_version pit 'src/pitpkg/cliconstants.nim'"
exec "update_nim_package_version pit 'src/pitpkg/cliconstants.nim'"

View File

@ -612,5 +612,6 @@ when isMainModule:
except:
fatal getCurrentExceptionMsg()
debug getCurrentException().getStackTrace()
#raise getCurrentException()
quit(QuitFailure)

View File

@ -413,14 +413,12 @@ proc find*(
### Configuration utilities
proc loadConfig*(args: Table[string, Value] = initTable[string, Value]()): PitConfig =
let pitrcLocations = @[
if args["--config"]: $args["--config"] else: "",
".pitrc", $getEnv("PITRC"), $getEnv("HOME") & "/.pitrc"]
var pitrcFilename: string
var pitrcFilename: string =
pitrcLocations --> fold("", if fileExists(it): it else: a)
if not fileExists(pitrcFilename):
try:
pitrcFilename = findConfigFile(".pitrc",
if args["--config"]: @[$args["--config"]] else: @[])
except ValueError:
warn "could not find .pitrc file: " & pitrcFilename
if isEmptyOrWhitespace(pitrcFilename):
pitrcFilename = $getEnv("HOME") & "/.pitrc"
@ -431,22 +429,16 @@ proc loadConfig*(args: Table[string, Value] = initTable[string, Value]()): PitCo
except: warn "could not write default .pitrc to " & pitrcFilename
finally: close(cfgFile)
var cfgJson: JsonNode
try: cfgJson = parseFile(pitrcFilename)
except: raise newException(IOError,
"unable to read config file: " & pitrcFilename &
"\x0D\x0A" & getCurrentExceptionMsg())
let cfg = CombinedConfig(docopt: args, json: cfgJson)
debug "loading config from '$#'" % [pitrcFilename]
let cfg = initCombinedConfig(pitrcFilename, args)
result = PitConfig(
cfg: cfg,
contexts: newTable[string,string](),
tasksDir: cfg.getVal("tasks-dir", ""))
if cfgJson.hasKey("contexts"):
for k, v in cfgJson["contexts"]:
result.contexts[k] = v.getStr()
for k, v in cfg.getJson("contexts", newJObject()):
result.contexts[k] = v.getStr()
if isEmptyOrWhitespace(result.tasksDir):
raise newException(Exception, "no tasks directory configured")