Read slfmt.config.json by default if present.

This commit is contained in:
2026-01-09 10:35:02 -06:00
parent 9219d3e86e
commit b0095153fc
2 changed files with 28 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
# Package
version = "1.0.0"
version = "1.0.1"
author = "Jonathan Bernard"
description = "Small utility to pretty-print strucutured logs."
license = "MIT"

View File

@@ -5,7 +5,7 @@ import cliutils, docopt, timeutils, zero_functional
from std/logging import Level
import std/nre except toSeq
const VERSION = "1.0.0"
const VERSION = "1.0.1"
const USAGE = """Usage:
slfmt [options]
@@ -47,7 +47,12 @@ Options:
--config <cfgFile>
Read expectation data from a json config file formatted as follows:
Read expectation data from a json config file. slfmt will, by default,
try to read from a file named `slfmt.config.json`, but will silently
ignore its absence. When --config is passed, slfmt will fail if the
named file is not found.
The config file is expected to be formatted as follows:
[
{
@@ -315,14 +320,24 @@ proc parseExpectations(args: Table[string, docopt.Value]): seq[Expectation] =
expected: isExpected,
count: 0))
if args["--config"]:
let filename = $args["--config"]
if not fileExists(filename):
stderr.writeLine("slfmt - WARN: " &
filename & " does not exist, ignoring")
# TODO: Consider refactoring and moving this outside of this function. Right
# now the config file is only used to store expectation configuration, but if
# that ever changes and we have additional config data then this confuses the
# purpose of this function. We should parse the config separately and pass
# that into this function to extract expectations.
var configFilename = "slfmt.config.json"
var configRequired = false
if args["--config"]:
configFilename = $args["--config"]
configRequired = true
if not fileExists(configFilename):
if configRequired:
raise newException(IOError, configFilename & " does not exist")
else: # file exists
try:
let cfg: JsonNode = parseFile(filename)
let cfg: JsonNode = parseFile(configFilename)
for expJson in cfg.getElems:
var exp = Expectation(
fieldName: expJson.getOrFail("fieldName").getStr,
@@ -389,8 +404,10 @@ proc parseExpectations(args: Table[string, docopt.Value]): seq[Expectation] =
result.add(exp)
except:
stderr.writeLine("slfmt - WARN: unable to parse config file, ignoring.")
stderr.writeLine("slfmt - DEBUG: " & getCurrentExceptionMsg())
if not configRequired:
stderr.writeLine("slfmt - WARN: unable to parse config file, ignoring.")
stderr.writeLine("slfmt - DEBUG: " & getCurrentExceptionMsg())
else: raise getCurrentException()
proc eraseAndWriteLine(f: File, s: string) =