Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add severity to standard output. Fixes #93 #111

Merged
merged 4 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Options:
-L list all the rules
-r RULESDIR specify one or more rules directories using one or
more -r arguments. Any -r flags override the default
rules in /path/to/salt-lint/saltlint/rules, unless
rules in /path/to/salt-lint/saltlint/rules, unless
-R is also used.
-R Use default rules in
/path/to/salt-lint/saltlint/rules in addition to any
Expand All @@ -54,6 +54,7 @@ Options:
path to directories or files to skip. This option is
repeatable.
--json parse the output as JSON
--add-severity add the severity to 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'
add-severity: True
```

## Pre-commit Setup
Expand Down
5 changes: 5 additions & 0 deletions saltlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ def _parse(self, content):
# Parse json
self.json = self._options.get('json', False)

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

# Parse rule specific configuration, the configration can be listed by
# the rule ID and/or tag.
self.rules = dict()
Expand Down
19 changes: 13 additions & 6 deletions saltlint/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@

class Formatter(object):

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

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

def format(self, match, colored=False):
formatstr = u"{0} {1}\n{2}:{3}\n{4}\n"
if colored:
color = saltcolor.get_colors()
return formatstr.format(
Expand All @@ -31,15 +35,18 @@ def format(self, match, colored=False):
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'])
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)
match.line,
sev=u'[{0}]'.format(match.rule.severity))


class JsonFormatter(object):
Expand Down