From b0095153fcd7f7cdda3441f482fe80af53ff9539 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 9 Jan 2026 10:35:02 -0600 Subject: [PATCH] Read slfmt.config.json by default if present. --- slfmt.nimble | 2 +- src/slfmt.nim | 37 +++++++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/slfmt.nimble b/slfmt.nimble index 9a24a29..e9e882c 100644 --- a/slfmt.nimble +++ b/slfmt.nimble @@ -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" diff --git a/src/slfmt.nim b/src/slfmt.nim index 50cb81a..11a1cc8 100644 --- a/src/slfmt.nim +++ b/src/slfmt.nim @@ -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 - 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) =