]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/terraform/tfsec.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:

Merge commit '76265755a1add77121c8f9dabb3e9bb70fe9a972' as '.vim/bundle/ale'
[etc/vim.git] / .vim / bundle / ale / ale_linters / terraform / tfsec.vim
1 " Description: tfsec for Terraform files
2 "
3 " See: https://www.terraform.io/
4 "      https://github.com/aquasecurity/tfsec
5
6 call ale#Set('terraform_tfsec_options', '')
7 call ale#Set('terraform_tfsec_executable', 'tfsec')
8
9 let s:separator = has('win32') ? '\' : '/'
10
11 function! ale_linters#terraform#tfsec#Handle(buffer, lines) abort
12     let l:output = []
13     let l:json = ale#util#FuzzyJSONDecode(a:lines, {})
14
15     " if there's no warning, 'result' is `null`.
16     if empty(get(l:json, 'results'))
17         return l:output
18     endif
19
20     for l:result in get(l:json, 'results', [])
21         if l:result.severity is# 'LOW'
22             let l:type = 'I'
23         elseif l:result.severity is# 'CRITICAL'
24             let l:type = 'E'
25         else
26             let l:type = 'W'
27         endif
28
29         call add(l:output, {
30         \   'filename': l:result.location.filename,
31         \   'lnum': l:result.location.start_line,
32         \   'end_lnum': l:result.location.end_line,
33         \   'text': l:result.description,
34         \   'code': l:result.long_id,
35         \   'type': l:type,
36         \})
37     endfor
38
39     return l:output
40 endfunction
41
42 " Construct command arguments to tfsec with `terraform_tfsec_options`.
43 function! ale_linters#terraform#tfsec#GetCommand(buffer) abort
44     let l:cmd = '%e'
45
46     let l:config = ale_linters#terraform#tfsec#FindConfig(a:buffer)
47
48     if !empty(l:config)
49         let l:cmd .= ' --config-file ' . l:config
50     endif
51
52     let l:opts = ale#Var(a:buffer, 'terraform_tfsec_options')
53
54     if !empty(l:opts)
55         let l:cmd .= ' ' . l:opts
56     endif
57
58     let l:cmd .= ' --format json'
59
60     return l:cmd
61 endfunction
62
63 " Find the nearest configuration file of tfsec.
64 function! ale_linters#terraform#tfsec#FindConfig(buffer) abort
65     let l:config_dir = ale#path#FindNearestDirectory(a:buffer, '.tfsec')
66
67     if !empty(l:config_dir)
68         " https://aquasecurity.github.io/tfsec/v1.28.0/guides/configuration/config/
69         for l:basename in ['config.yml', 'config.json']
70             let l:config = ale#path#Simplify(join([l:config_dir, l:basename], s:separator))
71
72             if filereadable(l:config)
73                 return ale#Escape(l:config)
74             endif
75         endfor
76     endif
77
78     return ''
79 endfunction
80
81 call ale#linter#Define('terraform', {
82 \   'name': 'tfsec',
83 \   'executable': {b -> ale#Var(b, 'terraform_tfsec_executable')},
84 \   'cwd': '%s:h',
85 \   'command': function('ale_linters#terraform#tfsec#GetCommand'),
86 \   'callback': 'ale_linters#terraform#tfsec#Handle',
87 \})