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 # Package
version = "1.0.0" version = "1.0.1"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Small utility to pretty-print strucutured logs." description = "Small utility to pretty-print strucutured logs."
license = "MIT" license = "MIT"

View File

@@ -5,7 +5,7 @@ import cliutils, docopt, timeutils, zero_functional
from std/logging import Level from std/logging import Level
import std/nre except toSeq import std/nre except toSeq
const VERSION = "1.0.0" const VERSION = "1.0.1"
const USAGE = """Usage: const USAGE = """Usage:
slfmt [options] slfmt [options]
@@ -47,7 +47,12 @@ Options:
--config <cfgFile> --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, expected: isExpected,
count: 0)) count: 0))
if args["--config"]: # TODO: Consider refactoring and moving this outside of this function. Right
let filename = $args["--config"] # now the config file is only used to store expectation configuration, but if
if not fileExists(filename): # that ever changes and we have additional config data then this confuses the
stderr.writeLine("slfmt - WARN: " & # purpose of this function. We should parse the config separately and pass
filename & " does not exist, ignoring") # 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: try:
let cfg: JsonNode = parseFile(filename) let cfg: JsonNode = parseFile(configFilename)
for expJson in cfg.getElems: for expJson in cfg.getElems:
var exp = Expectation( var exp = Expectation(
fieldName: expJson.getOrFail("fieldName").getStr, fieldName: expJson.getOrFail("fieldName").getStr,
@@ -389,8 +404,10 @@ proc parseExpectations(args: Table[string, docopt.Value]): seq[Expectation] =
result.add(exp) result.add(exp)
except: except:
if not configRequired:
stderr.writeLine("slfmt - WARN: unable to parse config file, ignoring.") stderr.writeLine("slfmt - WARN: unable to parse config file, ignoring.")
stderr.writeLine("slfmt - DEBUG: " & getCurrentExceptionMsg()) stderr.writeLine("slfmt - DEBUG: " & getCurrentExceptionMsg())
else: raise getCurrentException()
proc eraseAndWriteLine(f: File, s: string) = proc eraseAndWriteLine(f: File, s: string) =