Skip to content

Commit

Permalink
Merge pull request #111 from mew1033/add-severity
Browse files Browse the repository at this point in the history
Add severity to standard output. Fixes #93
  • Loading branch information
roaldnefs authored Nov 21, 2019
2 parents 8667afe + b611de7 commit 4a78467
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Options:
path to directories or files to skip. This option is
repeatable.
--json parse the output as JSON
--severity add the severity to the standard output
-c C Specify configuration file to use. Defaults to
".salt-lint"
```
Expand Down Expand Up @@ -112,6 +113,7 @@ rules:
*.jinja
210:
ignore: 'exclude_this_file.sls'
severity: True
```

## Pre-commit Setup
Expand Down
4 changes: 4 additions & 0 deletions saltlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def run(args=None):
default=[])
parser.add_option('--json', dest='json', action='store_true', default=False,
help='parse the output as JSON')
parser.add_option('--severity', dest='severity', action='store_true', default=False,
help='add the severity to the standard output')
parser.add_option('-c', help='Specify configuration file to use. Defaults to ".salt-lint"')
(options, parsed_args) = parser.parse_args(args if args is not None else sys.argv[1:])

Expand Down Expand Up @@ -111,6 +113,8 @@ def run(args=None):
# Define the formatter
if config.json:
formatter = formatters.JsonFormatter()
elif config.severity:
formatter = formatters.SeverityFormatter()
else:
formatter = formatters.Formatter()

Expand Down
5 changes: 5 additions & 0 deletions saltlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def _parse(self, content):
if 'json' in config:
self.json = config['json']

# Parse add severity
self.severity = self._options.get('severity', False)
if 'severity' in config:
self.severity = config['severity']

# Parse rule specific configuration, the configration can be listed by
# the rule ID and/or tag.
self.rules = dict()
Expand Down
33 changes: 33 additions & 0 deletions saltlint/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ def format(self, match, colored=False):
match.line)


class SeverityFormatter(object):
def process(self, matches, colored=False):
for match in matches:
print(self.format(match, colored))

def format(self, match, colored=False):
formatstr = u"{0} {sev} {1}\n{2}:{3}\n{4}\n"

if colored:
color = saltcolor.get_colors()
return formatstr.format(
u'{0}[{1}]{2}'.format(color['RED'], match.rule.id,
color['ENDC']),
u'{0}{1}{2}'.format(color['LIGHT_RED'], match.message,
color['ENDC']),
u'{0}{1}{2}'.format(color['BLUE'], match.filename,
color['ENDC']),
u'{0}{1}{2}'.format(color['CYAN'], str(match.linenumber),
color['ENDC']),
u'{0}{1}{2}'.format(color['MAGENTA'], match.line, color['ENDC']),
sev=u'{0}[{1}]{2}'.format(color['RED'], match.rule.severity,
color['ENDC'])
)
else:
return formatstr.format(
u'[{0}]'.format(match.rule.id),
match.message,
match.filename,
match.linenumber,
match.line,
sev=u'[{0}]'.format(match.rule.severity))


class JsonFormatter(object):

def process(self, matches, *args, **kwargs):
Expand Down

0 comments on commit 4a78467

Please sign in to comment.