From 648b578abd10390afbe4ca4a49250df62739ebad Mon Sep 17 00:00:00 2001 From: "Roman v. Gemmeren" Date: Tue, 31 Mar 2020 13:10:29 +0200 Subject: [PATCH 1/6] 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', From d167f4556ebb8e4973fa3b785e75af7e64c04d08 Mon Sep 17 00:00:00 2001 From: "Roman v. Gemmeren" Date: Tue, 31 Mar 2020 13:10:46 +0200 Subject: [PATCH 2/6] add test for valid json-output --- tests/test_cli.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 517bc62..3d86108 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -25,6 +25,7 @@ import pty import shutil import sys import unittest +import json from tests.common import build_temp_workspace @@ -517,3 +518,11 @@ class CommandLineTestCase(unittest.TestCase): '\n' % path) self.assertEqual( (ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, '')) + + def test_json_output(self): + path = os.path.join(self.wd, 'a.yaml') + + with RunContext(self) as ctx: + cli.run(('-f', 'json', path)) + print (ctx.stdout) + self.assertTrue(json.loads(ctx.stdout)) From 7bbc2e60e4e1a39e794ccb94844fc2594a4f602d Mon Sep 17 00:00:00 2001 From: "Roman v. Gemmeren" Date: Tue, 31 Mar 2020 13:11:01 +0200 Subject: [PATCH 3/6] add note for json-output --- docs/quickstart.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 9cf5f17..5b0ad19 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -101,6 +101,25 @@ machine (for instance for :doc:`syntax highlighting in text editors file.yml:57:1: [error] trailing spaces (trailing-spaces) file.yml:60:3: [error] wrong indentation: expected 4 but found 2 (indentation) +Add the ``-f json`` arguments if you need an output formatted as json. +The output will then look like: + + +:: + + [ + { + "path": "file.yml", + "line": 6, + "char": 2, + "description": "[warning] missing starting space in comment (comments)", + "code": "yamllint", + "name": "yamllint", + "severity": "warning" + } + ] + + If you have a custom linting configuration file (see :doc:`how to configure yamllint `), it can be passed to yamllint using the ``-c`` option: From 09ec4d3ee745d670cc450c82e9a3500335b59cc9 Mon Sep 17 00:00:00 2001 From: "Roman v. Gemmeren" Date: Tue, 31 Mar 2020 15:47:43 +0200 Subject: [PATCH 4/6] fix @adrienverge's notes --- docs/quickstart.rst | 3 +-- yamllint/cli.py | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 5b0ad19..856e758 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -101,10 +101,9 @@ machine (for instance for :doc:`syntax highlighting in text editors file.yml:57:1: [error] trailing spaces (trailing-spaces) file.yml:60:3: [error] wrong indentation: expected 4 but found 2 (indentation) -Add the ``-f json`` arguments if you need an output formatted as json. +Add the ``-f json`` arguments if you need an output formatted as JSON. The output will then look like: - :: [ diff --git a/yamllint/cli.py b/yamllint/cli.py index 76d84ae..33af8bb 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -92,8 +92,6 @@ class Format(object): "line": problem.line, "char": problem.column, "description": problem.message, - "code": "yamllint", - "name": "yamllint", "severity": problem.level, }) @@ -130,6 +128,7 @@ def show_problems(problems, file, args_format, no_warn): return max_level + def run(argv=None): parser = argparse.ArgumentParser(prog=APP_NAME, description=APP_DESCRIPTION) @@ -147,7 +146,7 @@ def run(argv=None): action='store', help='custom configuration (as YAML source)') parser.add_argument('-f', '--format', - choices=('parsable', 'standard', 'colored', 'auto', 'json'), + choices=('parsable', 'standard', 'colored', 'json', 'auto'), default='auto', help='format for parsing output') parser.add_argument('-s', '--strict', action='store_true', From 2c52058530089a0985df42b3dc4035c2828a5a2a Mon Sep 17 00:00:00 2001 From: "Roman v. Gemmeren" Date: Tue, 31 Mar 2020 17:20:18 +0200 Subject: [PATCH 5/6] remove redundant json-calls --- yamllint/cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yamllint/cli.py b/yamllint/cli.py index 33af8bb..034f57a 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -87,13 +87,13 @@ class Format(object): @staticmethod def json(problem, filename): - return json.dumps({ + return { "path": filename, "line": problem.line, "char": problem.column, "description": problem.message, "severity": problem.level, - }) + } def show_problems(problems, file, args_format, no_warn): max_level = 0 @@ -107,7 +107,7 @@ def show_problems(problems, file, args_format, no_warn): if args_format == 'parsable': print(Format.parsable(problem, file)) elif args_format == 'json': - problems_json.append(json.loads(Format.json(problem, file))) + problems_json.append(Format.json(problem, file)) elif args_format == 'colored' or \ (args_format == 'auto' and supports_color()): if first: From 48c2c13dc04c6ef089be146f2f8bbcee279c7241 Mon Sep 17 00:00:00 2001 From: "Roman v. Gemmeren" Date: Thu, 2 Apr 2020 10:19:58 +0200 Subject: [PATCH 6/6] remove code/name from docs --- docs/quickstart.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 856e758..85d1fb9 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -112,8 +112,6 @@ The output will then look like: "line": 6, "char": 2, "description": "[warning] missing starting space in comment (comments)", - "code": "yamllint", - "name": "yamllint", "severity": "warning" } ]