Skip to content

Commit

Permalink
nvim(after/ftplugin): create global user_command for ansible-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
gelocraft committed Dec 13, 2024
1 parent b374ccf commit 83785ba
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion nvim/after/ftplugin/yaml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,54 @@ local set = vim.opt_local
set.tabstop = 2
set.softtabstop = 2
set.shiftwidth = 2

if vim.fn.executable 'yamllint' ~= 0 then
set.makeprg = 'yamllint -f parsable'
set.makeprg = 'yamllint -f parsable'
end

if vim.fn.executable 'ansible-lint' ~= 0 then
---@param command string
local create_ansible_lint_command = function(command)
vim.api.nvim_create_user_command(command, function(opts)
local argc = #opts.fargs
if argc == 0 then
local current_buffer = vim.api.nvim_buf_get_name(0)
vim.cmd(
string.format(
'cexpr system("ansible-lint -p %s")',
current_buffer
)
)
else
vim.cmd(
string.format(
'cexpr system("ansible-lint -p %s")',
opts.args
)
)
end
end, { nargs = '*' })
end
local ansible_files = {
'ansible.cfg',
'inventory',
'inventory.ini',
'inventory.yaml',
'inventory/',
'playbook.yaml',
'playbooks/',
'roles/',
'host_vars/',
'group_vars/',
}

for _, f in ipairs(ansible_files) do
local ansible_file = (
vim.fn.filereadable(f) ~= 0 or vim.fn.isdirectory(f) ~= 0
)
if ansible_file then
create_ansible_lint_command 'AnsibleLint'
break
end
end
end

0 comments on commit 83785ba

Please sign in to comment.