From feb8cdf9e8e0c1b5d76253cc855aea26233b4dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Fri, 19 Jul 2024 14:28:53 +0200 Subject: [PATCH] Add support to change lint output format from environment Use REUSE_OUTPUT_FORMAT to allow overriding lint output formats in certain environments (like CI). --- changelog.d/added/format-env.md | 2 ++ docs/man/reuse-lint.rst | 9 +++++++++ src/reuse/lint.py | 32 ++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 changelog.d/added/format-env.md diff --git a/changelog.d/added/format-env.md b/changelog.d/added/format-env.md new file mode 100644 index 00000000..3ee98ce4 --- /dev/null +++ b/changelog.d/added/format-env.md @@ -0,0 +1,2 @@ +- Added `REUSE_OUTPUT_FORMAT` environment variable to configure output for + `lint`. diff --git a/docs/man/reuse-lint.rst b/docs/man/reuse-lint.rst index 2a086a87..a750185e 100644 --- a/docs/man/reuse-lint.rst +++ b/docs/man/reuse-lint.rst @@ -103,3 +103,12 @@ Options .. option:: -h, --help Display help and exit. + +Environment +----------- + +.. envvar:: REUSE_OUTPUT_FORMAT + + Specifies output format, one of ``plain``, ``lines``, ``github``, ``json`` + + It behaves same as corresponding command line options. diff --git a/src/reuse/lint.py b/src/reuse/lint.py index 87e4a440..36427a80 100644 --- a/src/reuse/lint.py +++ b/src/reuse/lint.py @@ -11,6 +11,7 @@ """ import json +import os import sys from argparse import ArgumentParser, Namespace from gettext import gettext as _ @@ -374,15 +375,26 @@ def run(args: Namespace, project: Project, out: IO[str] = sys.stdout) -> int: project, do_checksum=False, multiprocessing=not args.no_multiprocessing ) - if args.quiet: - pass - elif args.json: - out.write(format_json(report)) - elif args.lines: - out.write(format_lines(report)) - elif args.github: - out.write(format_github(report)) - else: - out.write(format_plain(report)) + formatters = { + "json": format_json, + "lines": format_lines, + "github": format_github, + "plain": format_plain, + } + + if not args.quiet: + output_format = os.environ.get("REUSE_OUTPUT_FORMAT") + + if output_format is not None and output_format in formatters: + formatter = formatters[output_format] + out.write(formatter(report)) + elif args.json: + out.write(format_json(report)) + elif args.lines: + out.write(format_lines(report)) + elif args.github: + out.write(format_github(report)) + else: + out.write(format_plain(report)) return 0 if report.is_compliant else 1