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 126c0485..2f314e23 100644 --- a/src/reuse/lint.py +++ b/src/reuse/lint.py @@ -13,6 +13,7 @@ from __future__ import annotations import json +import os import sys from argparse import ArgumentParser, Namespace from gettext import gettext as _ @@ -376,15 +377,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