Extract logic for locating the config file to the cliutils library.
This commit is contained in:
parent
b0e3f5a9d8
commit
bcb1c7c17c
@ -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'"
|
||||
|
@ -612,5 +612,6 @@ when isMainModule:
|
||||
|
||||
except:
|
||||
fatal getCurrentExceptionMsg()
|
||||
debug getCurrentException().getStackTrace()
|
||||
#raise getCurrentException()
|
||||
quit(QuitFailure)
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user