-
Notifications
You must be signed in to change notification settings - Fork 3
/
linter.py
84 lines (63 loc) · 2.34 KB
/
linter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
# linter.py
# Linter for SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Markus Liljedahl
# Copyright (c) 2017 Markus Liljedahl
#
# License: MIT
#
from SublimeLinter.lint import Linter, util
import re
class CfnLint(Linter):
"""Provides an interface to cfn-lint."""
cmd = ('cfn-lint', '--template', '${file}', '--format', 'parseable')
regex = (
r'^.+?:(?P<line>\d+):(?P<col>\d+):\d+:\d+:'
r'((?P<warning>W|I)|(?P<error>E))(?P<code>.{4})'
r':(?P<message>.+)'
)
multiline = True
line_col_base = (1, 1)
tempfile_suffix = '-'
error_stream = util.STREAM_STDOUT
word_re = None
defaults = {
'selector': 'source.yaml, source.json',
'strict': True
}
def communicate(self, cmd, code=None):
"""Run an external executable using stdin to pass code and return its output."""
relfilename = self.filename
is_cfn = False
# Check if we're processing a CloudFormation file
with open(relfilename, 'r', encoding='utf8') as file:
content = file.read()
regex = re.compile(r'"?AWSTemplateFormatVersion"?\s*')
if regex.search(content):
is_cfn = True
if is_cfn:
# Add ignore rules
ignore_rules = self.settings.get('ignore_rules', [])
if len(ignore_rules) > 0:
cmd.append('--ignore-checks')
for ignore_rule in ignore_rules:
cmd.append(ignore_rule)
# Add apprent rules paths
append_rules = self.settings.get('append_rules', [])
if len(append_rules) > 0:
cmd.append('--append-rules')
for append_rule in append_rules:
cmd.append(append_rule)
# Add override specification file
override_spec = self.settings.get('override_spec')
if override_spec:
cmd.append('--override-spec')
cmd.append(override_spec)
# Add additional checks
include_checks = self.settings.get('include_checks', [])
if len(include_checks) > 0:
cmd.append('--include-checks')
for include_rule in include_checks:
cmd.append(include_rule)
return super().communicate(cmd, code)