summaryrefslogtreecommitdiff
path: root/ale_linters/terraform/terraform.vim
diff options
context:
space:
mode:
authorKeith Maxwell <keith.maxwell@gmail.com>2019-05-23 14:12:36 +0100
committerKeith Maxwell <keith.maxwell@gmail.com>2019-05-23 15:49:02 +0100
commit88fa0b9294c6e907e8e2e8bee2ca593dff91b473 (patch)
tree5e094c3c5b45f2137ce821a93a86a3a6653ded01 /ale_linters/terraform/terraform.vim
parent1a9b8a58c7e01f0926e51b39b7b37f932537cc0c (diff)
downloadale-88fa0b9294c6e907e8e2e8bee2ca593dff91b473.zip
Add a terraform linter
This linter uses the check functionality built into terraform. ALE already has a fixer using `terraform fmt` but this doesn't provide error messages. ALE already has a linter using `tflint` but this requires an extra application to be installed. For example this linter will give a warning that ! is an illegal character in the line below: variable "example" !{} This linter runs the buffer through the command below and parses the output: terraform fmt -no-color -check=true - This commit includes a basic implementation, documentation and tests. The only option is to control which executable is run. Tested with: $ terraform -version Terraform v0.11.13
Diffstat (limited to 'ale_linters/terraform/terraform.vim')
-rwxr-xr-xale_linters/terraform/terraform.vim49
1 files changed, 49 insertions, 0 deletions
diff --git a/ale_linters/terraform/terraform.vim b/ale_linters/terraform/terraform.vim
new file mode 100755
index 00000000..0429cb7a
--- /dev/null
+++ b/ale_linters/terraform/terraform.vim
@@ -0,0 +1,49 @@
+" Author: Keith Maxwell <keith.maxwell@gmail.com>
+" Description: terraform fmt to check for errors
+
+call ale#Set('terraform_terraform_executable', 'terraform')
+
+function! ale_linters#terraform#terraform#GetExecutable(buffer) abort
+ return ale#Var(a:buffer, 'terraform_terraform_executable')
+endfunction
+
+function! ale_linters#terraform#terraform#GetCommand(buffer) abort
+ return ale#Escape(ale_linters#terraform#terraform#GetExecutable(a:buffer))
+ \ . ' fmt -no-color --check=true -'
+endfunction
+
+function! ale_linters#terraform#terraform#Handle(buffer, lines) abort
+ let l:head = '^Error running fmt: In <standard input>: '
+ let l:output = []
+ let l:patterns = [
+ \ l:head.'At \(\d\+\):\(\d\+\): \(.*\)$',
+ \ l:head.'\(.*\)$'
+ \]
+
+ for l:match in ale#util#GetMatches(a:lines, l:patterns)
+ if len(l:match[2]) > 0
+ call add(l:output, {
+ \ 'lnum': str2nr(l:match[1]),
+ \ 'col': str2nr(l:match[2]),
+ \ 'text': l:match[3],
+ \ 'type': 'E',
+ \})
+ else
+ call add(l:output, {
+ \ 'lnum': line('$'),
+ \ 'text': l:match[1],
+ \ 'type': 'E',
+ \})
+ endif
+ endfor
+
+ return l:output
+endfunction
+
+call ale#linter#Define('terraform', {
+\ 'name': 'terraform',
+\ 'output_stream': 'stderr',
+\ 'executable': function('ale_linters#terraform#terraform#GetExecutable'),
+\ 'command': function('ale_linters#terraform#terraform#GetCommand'),
+\ 'callback': 'ale_linters#terraform#terraform#Handle',
+\})