From efbd29024c9df98e2f6d71a77d8e4206e721abd0 Mon Sep 17 00:00:00 2001 From: Chandler Newby Date: Thu, 14 Nov 2019 16:52:58 -0700 Subject: [PATCH 1/4] Add severity to standard output. Fixes #93 --- saltlint/config.py | 5 +++++ saltlint/formatters/__init__.py | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/saltlint/config.py b/saltlint/config.py index 65202f6..b64a59a 100644 --- a/saltlint/config.py +++ b/saltlint/config.py @@ -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() diff --git a/saltlint/formatters/__init__.py b/saltlint/formatters/__init__.py index ab7abf8..5dfa87d 100644 --- a/saltlint/formatters/__init__.py +++ b/saltlint/formatters/__init__.py @@ -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( @@ -31,7 +35,9 @@ 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( @@ -39,7 +45,8 @@ def format(self, match, colored=False): match.message, match.filename, match.linenumber, - match.line) + match.line, + sev=match.rule.severity) class JsonFormatter(object): From cfbfd583012bc488c3f4664dd2d95001c17b92cb Mon Sep 17 00:00:00 2001 From: Chandler Newby Date: Thu, 14 Nov 2019 16:57:20 -0700 Subject: [PATCH 2/4] Add add-severity to README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b1052b4..e3ca9e2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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" ``` @@ -112,6 +113,7 @@ rules: *.jinja 210: ignore: 'exclude_this_file.sls' +add-severity: True ``` ## Pre-commit Setup From 8d936a4085b6585429d81907ef611f0290f698c2 Mon Sep 17 00:00:00 2001 From: Chandler Newby Date: Thu, 14 Nov 2019 17:25:49 -0700 Subject: [PATCH 3/4] Add brackets around severity in nocolor output --- saltlint/formatters/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/saltlint/formatters/__init__.py b/saltlint/formatters/__init__.py index 5dfa87d..c887bfd 100644 --- a/saltlint/formatters/__init__.py +++ b/saltlint/formatters/__init__.py @@ -46,7 +46,7 @@ def format(self, match, colored=False, add_severity=False): match.filename, match.linenumber, match.line, - sev=match.rule.severity) + sev=u'[{0}]'.format(match.rule.severity)) class JsonFormatter(object): From b611de71494b35c841d984a24a871e6c8000a790 Mon Sep 17 00:00:00 2001 From: Chandler Newby Date: Wed, 20 Nov 2019 15:38:30 -0700 Subject: [PATCH 4/4] Make severity formatter more in line with ansible-lint --- README.md | 4 ++-- saltlint/cli.py | 4 ++++ saltlint/config.py | 6 +++--- saltlint/formatters/__init__.py | 38 +++++++++++++++++++++++++++------ 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e3ca9e2..046b49f 100644 --- a/README.md +++ b/README.md @@ -54,7 +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 + --severity add the severity to the standard output -c C Specify configuration file to use. Defaults to ".salt-lint" ``` @@ -113,7 +113,7 @@ rules: *.jinja 210: ignore: 'exclude_this_file.sls' -add-severity: True +severity: True ``` ## Pre-commit Setup diff --git a/saltlint/cli.py b/saltlint/cli.py index 8ef1de8..eabbd42 100644 --- a/saltlint/cli.py +++ b/saltlint/cli.py @@ -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:]) @@ -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() diff --git a/saltlint/config.py b/saltlint/config.py index b64a59a..b45823a 100644 --- a/saltlint/config.py +++ b/saltlint/config.py @@ -99,9 +99,9 @@ def _parse(self, content): 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'] + 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. diff --git a/saltlint/formatters/__init__.py b/saltlint/formatters/__init__.py index c887bfd..bd07f66 100644 --- a/saltlint/formatters/__init__.py +++ b/saltlint/formatters/__init__.py @@ -14,15 +14,41 @@ class Formatter(object): - def process(self, matches, colored=False, add_severity=False): + def process(self, matches, colored=False): for match in matches: - print(self.format(match, colored, add_severity)) + print(self.format(match, colored)) - def format(self, match, colored=False, add_severity=False): - if add_severity: - formatstr = u"{0}{sev} {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( + 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']) + ) else: - formatstr = u"{0} {1}\n{2}:{3}\n{4}\n" + return formatstr.format( + u'[{0}]'.format(match.rule.id), + match.message, + match.filename, + match.linenumber, + 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()