From 0af2ddc5ee813c71df35ff9d47bba345f2b97322 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 11 Jan 2024 16:11:33 +0100 Subject: [PATCH 1/2] Allow logging errors --- src/logger.nim | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/logger.nim b/src/logger.nim index 5c049c14..c0923763 100644 --- a/src/logger.nim +++ b/src/logger.nim @@ -23,8 +23,14 @@ template withLevel*(verbosity: Verbosity, body: untyped): untyped = body consoleLogger.levelThreshold = currentLevel +template logError*(msgs: varargs[string]) = + consoleLogger.useStderr = true + error(msgs) + template logNormal*(msgs: varargs[string]) = + consoleLogger.useStderr = false notice(msgs) template logDetailed*(msgs: varargs[string]) = + consoleLogger.useStderr = false info(msgs) From c3056af595f7b014596f5303adde654bae4c9d00 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 12 Jan 2024 15:30:37 +0100 Subject: [PATCH 2/2] Use show error in create command --- src/cli.nim | 2 +- src/create/create.nim | 25 ++++++++++--------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/cli.nim b/src/cli.nim index 32b287d5..32556937 100644 --- a/src/cli.nim +++ b/src/cli.nim @@ -349,7 +349,7 @@ let colorStdout* = shouldUseColor(stdout) colorStderr* = shouldUseColor(stderr) -proc showError*(s: string, writeHelp = true) = +proc showError* (s: string, writeHelp = true) {.noreturn.} = const errorPrefix = "Error: " if colorStderr: stderr.styledWrite(fgRed, errorPrefix) diff --git a/src/create/create.nim b/src/create/create.nim index 14dba527..7492c72a 100644 --- a/src/create/create.nim +++ b/src/create/create.nim @@ -5,14 +5,12 @@ import "."/[approaches, articles] proc create*(conf: Conf) = if conf.action.kind == actCreate: if conf.action.exerciseCreate.len == 0: - let msg = "Please specify an exercise, using --exercise " - stderr.writeLine msg - quit 1 + let msg = "please specify an exercise, using --exercise " + showError(msg) if conf.action.approachSlug.len > 0: if conf.action.articleSlug.len > 0: - let msg = &"Both --approach and --article were provided. Please specify only one." - stderr.writeLine msg - quit 1 + let msg = &"both --approach and --article were provided. Please specify only one." + showError(msg) let trackConfigPath = conf.trackDir / "config.json" let trackConfig = parseFile(trackConfigPath, TrackConfig) let trackExerciseSlugs = getSlugs(trackConfig.exercises, conf, trackConfigPath) @@ -24,11 +22,10 @@ proc create*(conf: Conf) = elif userExercise in trackExerciseSlugs.practice: conf.trackDir / "exercises" / "practice" / $userExercise else: - let msg = &"The `-e, --exercise` option was used to specify an " & + let msg = &"the `-e, --exercise` option was used to specify an " & &"exercise slug, but `{userExercise}` is not an slug in the " & &"track config:\n{trackConfigPath}" - stderr.writeLine msg - quit 1 + showError(msg) createApproach(Slug(conf.action.approachSlug), userExercise, exerciseDir) elif conf.action.articleSlug.len > 0: @@ -43,16 +40,14 @@ proc create*(conf: Conf) = elif userExercise in trackExerciseSlugs.practice: conf.trackDir / "exercises" / "practice" / $userExercise else: - let msg = &"The `-e, --exercise` option was used to specify an " & + let msg = &"the `-e, --exercise` option was used to specify an " & &"exercise slug, but `{userExercise}` is not an slug in the " & &"track config:\n{trackConfigPath}" - stderr.writeLine msg - quit 1 + showError(msg) createArticle(Slug(conf.action.articleSlug), userExercise, exerciseDir) else: - let msg = "Please specify `--article ` or `--approach `" - stderr.writeLine msg - quit 1 + let msg = "please specify `--article ` or `--approach `" + showError(msg) else: quit 1