Skip to content

Commit

Permalink
Add support to change lint output format from environment
Browse files Browse the repository at this point in the history
Use REUSE_OUTPUT_FORMAT to allow overriding lint output formats in certain
environments (like CI).
  • Loading branch information
nijel committed Jul 19, 2024
1 parent 350a3d3 commit b104679
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions changelog.d/added/format-env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added `REUSE_OUTPUT_FORMAT` environment variable to configure output for
`lint`.
9 changes: 9 additions & 0 deletions docs/man/reuse-lint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
32 changes: 22 additions & 10 deletions src/reuse/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from __future__ import annotations

import json
import os
import sys
from argparse import ArgumentParser, Namespace
from gettext import gettext as _
Expand Down Expand Up @@ -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

0 comments on commit b104679

Please sign in to comment.