2017-01-30 15:27:26 +00:00
|
|
|
" Author: hauleth - https://github.com/hauleth
|
|
|
|
|
2017-07-03 14:37:32 +00:00
|
|
|
" always, yes, never
|
|
|
|
call ale#Set('dockerfile_hadolint_use_docker', 'never')
|
2018-01-29 21:21:50 +00:00
|
|
|
call ale#Set('dockerfile_hadolint_docker_image', 'hadolint/hadolint')
|
2017-07-03 14:37:32 +00:00
|
|
|
|
2017-01-30 15:27:26 +00:00
|
|
|
function! ale_linters#dockerfile#hadolint#Handle(buffer, lines) abort
|
2017-04-15 11:52:08 +00:00
|
|
|
" Matches patterns line the following:
|
|
|
|
"
|
2021-07-05 12:54:03 +00:00
|
|
|
" -:19 DL3001 warning: Pipe chain should start with a raw value.
|
2018-01-23 21:46:59 +00:00
|
|
|
" /dev/stdin:19:3 unexpected thing
|
2021-04-10 12:17:55 +00:00
|
|
|
let l:pattern = '\v^%(/dev/stdin|-):(\d+):?(\d+)? ((DL|SC)(\d+) )?((.+)?: )?(.+)$'
|
2017-04-15 11:52:08 +00:00
|
|
|
let l:output = []
|
|
|
|
|
2017-04-17 23:35:53 +00:00
|
|
|
for l:match in ale#util#GetMatches(a:lines, l:pattern)
|
2017-04-15 11:52:08 +00:00
|
|
|
let l:lnum = 0
|
2018-01-23 21:46:59 +00:00
|
|
|
let l:colnum = 0
|
2017-04-15 11:52:08 +00:00
|
|
|
|
2017-08-10 23:31:42 +00:00
|
|
|
if l:match[1] isnot# ''
|
2017-04-15 11:52:08 +00:00
|
|
|
let l:lnum = l:match[1] + 0
|
|
|
|
endif
|
|
|
|
|
2018-01-23 21:46:59 +00:00
|
|
|
if l:match[2] isnot# ''
|
|
|
|
let l:colnum = l:match[2] + 0
|
|
|
|
endif
|
|
|
|
|
2020-11-29 12:50:24 +00:00
|
|
|
" Shellcheck knows a 'style' severity - pin it to info level as well.
|
|
|
|
if l:match[7] is# 'style'
|
|
|
|
let l:type = 'I'
|
|
|
|
elseif l:match[7] is# 'info'
|
|
|
|
let l:type = 'I'
|
|
|
|
elseif l:match[7] is# 'warning'
|
|
|
|
let l:type = 'W'
|
|
|
|
else
|
|
|
|
let l:type = 'E'
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:text = l:match[8]
|
|
|
|
let l:detail = l:match[8]
|
2018-01-23 21:46:59 +00:00
|
|
|
let l:domain = 'https://github.com/hadolint/hadolint/wiki/'
|
2021-07-05 12:54:03 +00:00
|
|
|
let l:code = ''
|
|
|
|
let l:link = ''
|
2018-01-23 21:46:59 +00:00
|
|
|
|
|
|
|
if l:match[4] is# 'SC'
|
|
|
|
let l:domain = 'https://github.com/koalaman/shellcheck/wiki/'
|
|
|
|
endif
|
|
|
|
|
|
|
|
if l:match[5] isnot# ''
|
|
|
|
let l:code = l:match[4] . l:match[5]
|
|
|
|
let l:link = ' ( ' . l:domain . l:code . ' )'
|
2021-07-05 12:54:03 +00:00
|
|
|
let l:text = l:code . ': ' . l:detail
|
2018-01-23 21:46:59 +00:00
|
|
|
let l:detail = l:code . l:link . "\n\n" . l:detail
|
|
|
|
else
|
|
|
|
let l:type = 'E'
|
2021-07-05 12:54:03 +00:00
|
|
|
let l:detail = 'hadolint could not parse the file because of a syntax error.'
|
2018-01-23 21:46:59 +00:00
|
|
|
endif
|
2017-04-15 11:52:08 +00:00
|
|
|
|
|
|
|
call add(l:output, {
|
|
|
|
\ 'lnum': l:lnum,
|
2018-01-23 21:46:59 +00:00
|
|
|
\ 'col': l:colnum,
|
2017-04-15 11:52:08 +00:00
|
|
|
\ 'type': l:type,
|
|
|
|
\ 'text': l:text,
|
2018-01-23 21:46:59 +00:00
|
|
|
\ 'detail': l:detail
|
2017-04-15 11:52:08 +00:00
|
|
|
\})
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:output
|
2017-01-30 15:27:26 +00:00
|
|
|
endfunction
|
|
|
|
|
2017-07-03 14:37:32 +00:00
|
|
|
" This is a little different than the typical 'executable' callback. We want
|
|
|
|
" to afford the user the chance to say always use docker, never use docker,
|
|
|
|
" and use docker if the hadolint executable is not present on the system.
|
|
|
|
"
|
|
|
|
" In the case of neither docker nor hadolint executables being present, it
|
|
|
|
" really doesn't matter which we return -- either will have the effect of
|
|
|
|
" 'nope, can't use this linter!'.
|
|
|
|
|
|
|
|
function! ale_linters#dockerfile#hadolint#GetExecutable(buffer) abort
|
|
|
|
let l:use_docker = ale#Var(a:buffer, 'dockerfile_hadolint_use_docker')
|
|
|
|
|
|
|
|
" check for mandatory directives
|
2017-08-08 07:39:13 +00:00
|
|
|
if l:use_docker is# 'never'
|
2017-07-03 14:37:32 +00:00
|
|
|
return 'hadolint'
|
2017-08-08 07:39:13 +00:00
|
|
|
elseif l:use_docker is# 'always'
|
2017-07-03 14:37:32 +00:00
|
|
|
return 'docker'
|
|
|
|
endif
|
|
|
|
|
|
|
|
" if we reach here, we want to use 'hadolint' if present...
|
|
|
|
if executable('hadolint')
|
|
|
|
return 'hadolint'
|
|
|
|
endif
|
|
|
|
|
|
|
|
"... and 'docker' as a fallback.
|
|
|
|
return 'docker'
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#dockerfile#hadolint#GetCommand(buffer) abort
|
|
|
|
let l:command = ale_linters#dockerfile#hadolint#GetExecutable(a:buffer)
|
2021-04-10 12:17:55 +00:00
|
|
|
let l:opts = '--no-color -'
|
2018-09-04 15:51:18 +00:00
|
|
|
|
2017-08-08 07:39:13 +00:00
|
|
|
if l:command is# 'docker'
|
2021-04-10 12:17:55 +00:00
|
|
|
return printf('docker run --rm -i %s hadolint %s',
|
|
|
|
\ ale#Var(a:buffer, 'dockerfile_hadolint_docker_image'),
|
|
|
|
\ l:opts)
|
2017-07-03 14:37:32 +00:00
|
|
|
endif
|
2018-09-04 15:51:18 +00:00
|
|
|
|
2021-04-10 12:17:55 +00:00
|
|
|
return 'hadolint ' . l:opts
|
2017-07-03 14:37:32 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2017-01-30 15:27:26 +00:00
|
|
|
call ale#linter#Define('dockerfile', {
|
2017-04-15 11:52:08 +00:00
|
|
|
\ 'name': 'hadolint',
|
2019-02-22 18:05:04 +00:00
|
|
|
\ 'executable': function('ale_linters#dockerfile#hadolint#GetExecutable'),
|
|
|
|
\ 'command': function('ale_linters#dockerfile#hadolint#GetCommand'),
|
2017-04-15 11:52:08 +00:00
|
|
|
\ 'callback': 'ale_linters#dockerfile#hadolint#Handle',
|
|
|
|
\})
|