]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/ansible/ansible_lint.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Do not set EDITOR/VISUAL for shell
[etc/vim.git] / .vim / bundle / ale / ale_linters / ansible / ansible_lint.vim
1 " Authors: Bjorn Neergaard <bjorn@neersighted.com>, Vytautas Macionis <vytautas.macionis@manomail.de>
2 " Description: ansible-lint for ansible-yaml files
3
4 call ale#Set('ansible_ansible_lint_executable', 'ansible-lint')
5
6 function! ale_linters#ansible#ansible_lint#GetExecutable(buffer) abort
7     return ale#Var(a:buffer, 'ansible_ansible_lint_executable')
8 endfunction
9
10 function! ale_linters#ansible#ansible_lint#Handle(buffer, version, lines) abort
11     for l:line in a:lines[:10]
12         if match(l:line, '^Traceback') >= 0
13             return [{
14             \   'lnum': 1,
15             \   'text': 'An exception was thrown. See :ALEDetail',
16             \   'detail': join(a:lines, "\n"),
17             \}]
18         endif
19     endfor
20
21     let l:version_group = ale#semver#GTE(a:version, [6, 0, 0]) ? '>=6.0.0' :
22     \                     ale#semver#GTE(a:version, [5, 0, 0]) ? '>=5.0.0' :
23     \                     '<5.0.0'
24     let l:output = []
25
26     if '>=6.0.0' is# l:version_group
27         let l:error_codes = { 'blocker': 'E', 'critical': 'E', 'major': 'W', 'minor': 'W', 'info': 'I' }
28         let l:linter_issues = ale#util#FuzzyJSONDecode(a:lines, [])
29
30         for l:issue in l:linter_issues
31             if ale#path#IsBufferPath(a:buffer, l:issue.location.path)
32                 if exists('l:issue.location.positions')
33                     let l:coord_keyname = 'positions'
34                 else
35                     let l:coord_keyname = 'lines'
36                 endif
37
38                 let l:column_member = printf(
39                 \    'l:issue.location.%s.begin.column', l:coord_keyname
40                 \)
41
42                 call add(l:output, {
43                 \   'lnum': exists(l:column_member) ? l:issue.location[l:coord_keyname].begin.line :
44                 \           l:issue.location[l:coord_keyname].begin,
45                 \   'col': exists(l:column_member) ? l:issue.location[l:coord_keyname].begin.column : 0,
46                 \   'text': l:issue.check_name,
47                 \   'detail': l:issue.description,
48                 \   'code': l:issue.severity,
49                 \   'type': l:error_codes[l:issue.severity],
50                 \})
51             endif
52         endfor
53     endif
54
55     if '>=5.0.0' is# l:version_group
56         " Matches patterns line the following:
57         "      test.yml:3:148: syntax-check 'var' is not a valid attribute for a Play
58         "      roles/test/tasks/test.yml:8: [package-latest] [VERY_LOW] Package installs should not use latest
59         "      D:\test\tasks\test.yml:8: [package-latest] [VERY_LOW] package installs should not use latest
60         let l:pattern = '\v^(%([a-zA-Z]:)?[^:]+):(\d+):%((\d+):)? %(\[([-[:alnum:]]+)\]) %(\[([_[:alnum:]]+)\]) (.*)$'
61         let l:error_codes = { 'VERY_HIGH': 'E', 'HIGH': 'E', 'MEDIUM': 'W', 'LOW': 'W', 'VERY_LOW': 'W', 'INFO': 'I' }
62
63         for l:match in ale#util#GetMatches(a:lines, l:pattern)
64             if ale#path#IsBufferPath(a:buffer, l:match[1])
65                 call add(l:output, {
66                 \   'lnum': l:match[2] + 0,
67                 \   'col': l:match[3] + 0,
68                 \   'text': l:match[6],
69                 \   'code': l:match[4],
70                 \   'type': l:error_codes[l:match[5]],
71                 \})
72             endif
73         endfor
74     endif
75
76     if '<5.0.0' is# l:version_group
77         " Matches patterns line the following:
78         "      test.yml:35: [EANSIBLE0002] Trailing whitespace
79         let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?: \[?([[:alnum:]]+)\]? (.*)$'
80
81         for l:match in ale#util#GetMatches(a:lines, l:pattern)
82             let l:code = l:match[4]
83
84             if l:code is# 'EANSIBLE0002'
85             \&& !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
86                 " Skip warnings for trailing whitespace if the option is off.
87                 continue
88             endif
89
90             if ale#path#IsBufferPath(a:buffer, l:match[1])
91                 call add(l:output, {
92                 \   'lnum': l:match[2] + 0,
93                 \   'col': l:match[3] + 0,
94                 \   'text': l:match[5],
95                 \   'code': l:code,
96                 \   'type': l:code[:0] is# 'E' ? 'E' : 'W',
97                 \})
98             endif
99         endfor
100     endif
101
102     return l:output
103 endfunction
104
105 function! ale_linters#ansible#ansible_lint#GetCommand(buffer, version) abort
106     let l:commands = {
107     \   '>=6.0.0': '%e --nocolor -f json -x yaml %s',
108     \   '>=5.0.0': '%e --nocolor --parseable-severity -x yaml %s',
109     \   '<5.0.0': '%e --nocolor -p %t'
110     \}
111     let l:command = ale#semver#GTE(a:version, [6, 0]) ? l:commands['>=6.0.0'] :
112     \               ale#semver#GTE(a:version, [5, 0]) ? l:commands['>=5.0.0'] :
113     \               l:commands['<5.0.0']
114
115     return l:command
116 endfunction
117
118 call ale#linter#Define('ansible', {
119 \   'name': 'ansible_lint',
120 \   'aliases': ['ansible', 'ansible-lint'],
121 \   'executable': function('ale_linters#ansible#ansible_lint#GetExecutable'),
122 \   'command': {buffer -> ale#semver#RunWithVersionCheck(
123 \       buffer,
124 \       ale_linters#ansible#ansible_lint#GetExecutable(buffer),
125 \       '%e --version',
126 \       function('ale_linters#ansible#ansible_lint#GetCommand'),
127 \   )},
128 \   'lint_file': 1,
129 \   'callback': {buffer, lines -> ale#semver#RunWithVersionCheck(
130 \       buffer,
131 \       ale_linters#ansible#ansible_lint#GetExecutable(buffer),
132 \       '%e --version',
133 \       {buffer, version -> ale_linters#ansible#ansible_lint#Handle(
134 \           buffer,
135 \           l:version,
136 \           lines)},
137 \   )},
138 \})