From 648b578abd10390afbe4ca4a49250df62739ebad Mon Sep 17 00:00:00 2001 From: "Roman v. Gemmeren" Date: Tue, 31 Mar 2020 13:10:29 +0200 Subject: [PATCH] add json-formatted output --- yamllint/cli.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/yamllint/cli.py b/yamllint/cli.py index e99fd2c..76d84ae 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -21,6 +21,7 @@ import io import os import platform import sys +import json from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION from yamllint import linter @@ -84,10 +85,22 @@ class Format(object): line += ' \033[2m(%s)\033[0m' % problem.rule return line + @staticmethod + def json(problem, filename): + return json.dumps({ + "path": filename, + "line": problem.line, + "char": problem.column, + "description": problem.message, + "code": "yamllint", + "name": "yamllint", + "severity": problem.level, + }) def show_problems(problems, file, args_format, no_warn): max_level = 0 first = True + problems_json = [] for problem in problems: max_level = max(max_level, PROBLEM_LEVELS[problem.level]) @@ -95,6 +108,8 @@ def show_problems(problems, file, args_format, no_warn): continue if args_format == 'parsable': print(Format.parsable(problem, file)) + elif args_format == 'json': + problems_json.append(json.loads(Format.json(problem, file))) elif args_format == 'colored' or \ (args_format == 'auto' and supports_color()): if first: @@ -107,12 +122,14 @@ def show_problems(problems, file, args_format, no_warn): first = False print(Format.standard(problem, file)) + if args_format == 'json': + print(json.dumps(problems_json)) + if not first and args_format != 'parsable': print('') return max_level - def run(argv=None): parser = argparse.ArgumentParser(prog=APP_NAME, description=APP_DESCRIPTION) @@ -130,7 +147,7 @@ def run(argv=None): action='store', help='custom configuration (as YAML source)') parser.add_argument('-f', '--format', - choices=('parsable', 'standard', 'colored', 'auto'), + choices=('parsable', 'standard', 'colored', 'auto', 'json'), default='auto', help='format for parsing output') parser.add_argument('-s', '--strict', action='store_true',