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 # Package
version = "4.23.2" version = "4.23.3"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Personal issue tracker." description = "Personal issue tracker."
license = "MIT" license = "MIT"
@ -20,7 +20,7 @@ requires @[
# Dependencies from git.jdb-software.com/nim-jdb/packages # Dependencies from git.jdb-software.com/nim-jdb/packages
requires @[ requires @[
"cliutils >= 0.6.4", "cliutils >= 0.8.1",
"langutils >= 0.4.0", "langutils >= 0.4.0",
"timeutils >= 0.5.4", "timeutils >= 0.5.4",
"data_uri > 1.0.0", "data_uri > 1.0.0",

View File

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

View File

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