Add JSON output option
This commit is contained in:
@@ -20,6 +20,7 @@ import os.path
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import json
|
||||||
|
|
||||||
from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION
|
from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION
|
||||||
from yamllint.config import YamlLintConfig, YamlLintConfigError
|
from yamllint.config import YamlLintConfig, YamlLintConfigError
|
||||||
@@ -48,6 +49,18 @@ class Format(object):
|
|||||||
'level': problem.level,
|
'level': problem.level,
|
||||||
'message': problem.message})
|
'message': problem.message})
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def json_output(problem, filename):
|
||||||
|
output_dict = {}
|
||||||
|
output_dict["path"] = filename
|
||||||
|
output_dict["line"] = problem.line
|
||||||
|
output_dict["char"] = problem.column
|
||||||
|
output_dict["description"] = problem.message
|
||||||
|
output_dict["code"] = "YAMLLINT"
|
||||||
|
output_dict["name"] = "YAMLLINT"
|
||||||
|
output_dict["severity"] = problem.level
|
||||||
|
return json.dumps(output_dict)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def standard(problem, filename):
|
def standard(problem, filename):
|
||||||
line = ' %d:%d' % (problem.line, problem.column)
|
line = ' %d:%d' % (problem.line, problem.column)
|
||||||
@@ -87,8 +100,8 @@ def run(argv=None):
|
|||||||
action='store',
|
action='store',
|
||||||
help='custom configuration (as YAML source)')
|
help='custom configuration (as YAML source)')
|
||||||
parser.add_argument('-f', '--format',
|
parser.add_argument('-f', '--format',
|
||||||
choices=('parsable', 'standard'), default='standard',
|
choices=('parsable', 'standard', 'json'),
|
||||||
help='format for parsing output')
|
default='standard', help='format for parsing output')
|
||||||
parser.add_argument('-s', '--strict',
|
parser.add_argument('-s', '--strict',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='return non-zero exit code on warnings '
|
help='return non-zero exit code on warnings '
|
||||||
@@ -134,6 +147,8 @@ def run(argv=None):
|
|||||||
for problem in linter.run(f, conf, filepath):
|
for problem in linter.run(f, conf, filepath):
|
||||||
if args.format == 'parsable':
|
if args.format == 'parsable':
|
||||||
print(Format.parsable(problem, file))
|
print(Format.parsable(problem, file))
|
||||||
|
elif args.format == 'json':
|
||||||
|
print(Format.json_output(problem, file))
|
||||||
elif sys.stdout.isatty():
|
elif sys.stdout.isatty():
|
||||||
if first:
|
if first:
|
||||||
print('\033[4m%s\033[0m' % file)
|
print('\033[4m%s\033[0m' % file)
|
||||||
|
|||||||
Reference in New Issue
Block a user