summaryrefslogtreecommitdiff
path: root/ale_linters/terraform/tflint.vim
blob: 93966ff3990160c969f2eb4f8869362a266cf566 (plain)
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
" Author: Nat Williams <nat.williams@gmail.com>
" Description: tflint for Terraform files
"
" See: https://www.terraform.io/
"      https://github.com/wata727/tflint

call ale#Set('terraform_tflint_options', '')
call ale#Set('terraform_tflint_executable', 'tflint')

function! ale_linters#terraform#tflint#Handle(buffer, lines) abort
    let l:output = []

    for l:error in ale#util#FuzzyJSONDecode(a:lines, [])
        if l:error.type is# 'ERROR'
            let l:type = 'E'
        elseif l:error.type is# 'NOTICE'
            let l:type = 'I'
        else
            let l:type = 'W'
        endif

        call add(l:output, {
        \   'lnum': l:error.line,
        \   'text': l:error.message,
        \   'type': l:type,
        \   'code': l:error.detector,
        \})
    endfor

    return l:output
endfunction

function! ale_linters#terraform#tflint#GetExecutable(buffer) abort
    return ale#Var(a:buffer, 'terraform_tflint_executable')
endfunction

function! ale_linters#terraform#tflint#GetCommand(buffer) abort
    let l:cmd = ale#Escape(ale#Var(a:buffer, 'terraform_tflint_executable'))

    let l:config_file = ale#path#FindNearestFile(a:buffer, '.tflint.hcl')
    if !empty(l:config_file)
        let l:cmd .= ' --config ' . ale#Escape(l:config_file)
    endif

    let l:opts = ale#Var(a:buffer, 'terraform_tflint_options')
    if !empty(l:opts)
        let l:cmd .= ' ' . l:opts
    endif

    let l:cmd .= ' -f json %t'

    return l:cmd
endfunction

call ale#linter#Define('terraform', {
\   'name': 'tflint',
\   'executable_callback': 'ale_linters#terraform#tflint#GetExecutable',
\   'command_callback': 'ale_linters#terraform#tflint#GetCommand',
\   'callback': 'ale_linters#terraform#tflint#Handle',
\})

" vim:sw=4