Compare commits

..

5 Commits
1.0.10 ... main

Author SHA1 Message Date
c00be8c1fc Add asdf tool-versions definitions pinning this to Nim 1.6 2024-05-10 11:59:58 -05:00
96ee649bf6 Change the logic around how we load our config.
The previous logic that was intended to find the first non-empty
filename that represented a valid file location was wrong. Replaced with
a simpler filter-based version.

Additionally, if the user provides a specific configuration filename, we
no longer fall back to the defaults if it is not found. Instead we just
err out and inform them that the file the specified was not found.
2022-04-02 08:45:30 -05:00
69177ffa17 Log locations when unable to load config.
When we can't find a config file, log all the locations we looked and
don't create a new file in $HOME/.ptkrc.

There is currently an intermittent bug in the config loading logic that
is leading to the default config file in $HOME/.ptkrc not being found
and then being overwritten with the default config. This is step one in
fixing it.
2022-03-19 08:49:26 -05:00
8b6405441a Support additional date and time formats (particularly YYYY-MM-DD). 2022-01-03 08:57:43 -06:00
aff927b4f4 Fix for Nim 1.6.2 2021-12-20 17:42:55 -06:00
6 changed files with 26 additions and 22 deletions

1
.tool-versions Normal file
View File

@ -0,0 +1 @@
nim 1.6.20

View File

@ -1,7 +1,7 @@
## Personal Time Keeping API Interface ## Personal Time Keeping API Interface
## =================================== ## ===================================
import asyncdispatch, base64, bcrypt, cliutils, docopt, jester, json, logging, import asyncdispatch, base64, bcrypt, cliutils, docopt, httpcore, jester, json, logging,
sequtils, strutils, os, tables, times, uuids sequtils, strutils, os, tables, times, uuids
import nre except toSeq import nre except toSeq
@ -61,10 +61,10 @@ template checkAuth(cfg: PtkApiCfg) =
var user {.inject.}: PtkUser = PtkUser() var user {.inject.}: PtkUser = PtkUser()
try: try:
if not request.headers.hasKey("Authorization"): if not headers(request).hasKey("Authorization"):
raiseEx "No auth token." raiseEx "No auth token."
let headerVal = request.headers["Authorization"] let headerVal = headers(request)["Authorization"]
if not headerVal.startsWith("Basic "): if not headerVal.startsWith("Basic "):
raiseEx "Invalid Authorization type (only 'Basic' is supported)." raiseEx "Invalid Authorization type (only 'Basic' is supported)."

View File

@ -22,10 +22,18 @@ const ISO_TIME_FORMAT* = "yyyy-MM-dd'T'HH:mm:ss"
## The canonical time format used by PTK. ## The canonical time format used by PTK.
const TIME_FORMATS* = @[ const TIME_FORMATS* = @[
(fmtStr: "yyyy-MM-dd'T'HH:mm:sszzz", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd HH:mm:sszzz", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd'T'HH:mm:sszz", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd HH:mm:sszz", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd'T'HH:mm:ssz", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd HH:mm:ssz", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd'T'HH:mm:ss", offsetFrom: OffsetFrom.None), (fmtStr: "yyyy-MM-dd'T'HH:mm:ss", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd HH:mm:ss", offsetFrom: OffsetFrom.None), (fmtStr: "yyyy-MM-dd HH:mm:ss", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd'T'HH:mm", offsetFrom: OffsetFrom.None), (fmtStr: "yyyy-MM-dd'T'HH:mm", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd HH:mm", offsetFrom: OffsetFrom.None), (fmtStr: "yyyy-MM-dd HH:mm", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM-dd", offsetFrom: OffsetFrom.None),
(fmtStr: "yyyy-MM", offsetFrom: OffsetFrom.None),
(fmtStr: "MM-dd'T'HH:mm:ss", offsetFrom: OffsetFrom.Year), (fmtStr: "MM-dd'T'HH:mm:ss", offsetFrom: OffsetFrom.Year),
(fmtStr: "MM-dd HH:mm:ss", offsetFrom: OffsetFrom.Year), (fmtStr: "MM-dd HH:mm:ss", offsetFrom: OffsetFrom.Year),
(fmtStr: "MM-dd'T'HH:mm", offsetFrom: OffsetFrom.Year), (fmtStr: "MM-dd'T'HH:mm", offsetFrom: OffsetFrom.Year),
@ -48,15 +56,16 @@ proc getIfExists*(n: JsonNode, key: string): JsonNode =
proc parseTime*(timeStr: string): DateTime = proc parseTime*(timeStr: string): DateTime =
## Helper to parse time strings trying multiple known formats. ## Helper to parse time strings trying multiple known formats.
let now = now()
for fmt in TIME_FORMATS: for fmt in TIME_FORMATS:
try: try:
let now = now()
let parsed = parse(timeStr, fmt.fmtStr) let parsed = parse(timeStr, fmt.fmtStr)
case fmt.offsetFrom: case fmt.offsetFrom:
of OffsetFrom.None: of OffsetFrom.None:
return parsed return parsed
of OffsetFrom.Year: of OffsetFrom.Year:
return initDateTime(parsed.monthday, parsed.month, now.year, return dateTime(now.year, parsed.month, parsed.monthday,
parsed.hour, parsed.minute, parsed.second, parsed.nanosecond, parsed.hour, parsed.minute, parsed.second, parsed.nanosecond,
now.timezone) now.timezone)
of OffsetFrom.Month: of OffsetFrom.Month:

View File

@ -1 +1 @@
const PTK_VERSION* = "1.0.10" const PTK_VERSION* = "1.0.14"

24
ptk.nim
View File

@ -307,27 +307,21 @@ Options:
quit() quit()
# Find and parse the .ptkrc file # Find and parse the .ptkrc file
let ptkrcLocations = @[ let ptkrcLocations =
if args["--config"]: $args["--config"] else:"", if args["--config"]: @[$args["--config"]]
".ptkrc", $getEnv("PTKRC"), $getEnv("HOME") & "/.ptkrc"] else: @[".ptkrc", $getEnv("PTKRC"), $getEnv("HOME") & "/.ptkrc"]
var ptkrcFilename: string = let foundPtkrcLocations =
foldl(ptkrcLocations, if len(a) > 0: a elif fileExists(b): b else: "") ptkrcLocations.filterIt(it.len > 0 and fileExists(it))
var cfg: JsonNode var cfg: JsonNode
var cfgFile: File if foundPtkrcLocations.len < 1:
if not fileExists(ptkrcFilename):
warn "ptk: could not find .ptkrc file." warn "ptk: could not find .ptkrc file."
ptkrcFilename = $getEnv("HOME") & "/.ptkrc" debug "ptk: considered the following locations:\n\t" & ptkrcLocations.join("\n\t")
try:
cfgFile = open(ptkrcFilename, fmWrite)
cfgFile.write("{\"timelineLogFile\": \"timeline.log.json\"}")
except: warn "ptk: could not write default .ptkrc to " & ptkrcFilename
finally: close(cfgFile)
try: cfg = parseFile(ptkrcFilename) try: cfg = parseFile(foundPtkrcLocations[0])
except: raise newException(IOError, except: raise newException(IOError,
"unable to read config file: " & ptkrcFilename & "unable to read config file: " & foundPtkrcLocations[0] &
"\x0D\x0A" & getCurrentExceptionMsg()) "\x0D\x0A" & getCurrentExceptionMsg())
# Find the time log file # Find the time log file

View File

@ -1,6 +1,6 @@
# Package # Package
version = "1.0.10" version = "1.0.14"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Personal Time Keeper" description = "Personal Time Keeper"
license = "MIT" license = "MIT"