Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add luacheck and make it pass #85

Merged
merged 5 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Type Check Code Base
on:
pull_request: ~
push:
branches:
- master
jobs:
build:
name: Type Check Code Base
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Type Check Code Base
uses: mrcjkb/lua-typecheck-action@v0
with:
directories: |
lua
configpath: .luarc.json
4 changes: 4 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Global objects defined by the C code
read_globals = {
"vim",
}
20 changes: 14 additions & 6 deletions ftplugin/k8s_deployments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ api.nvim_buf_set_keymap(0, "n", "i", "", {
callback = function()
local ns, name = tables.getCurrentSelection(unpack({ 1, 2 }))

local get_images = "get deploy " .. name .. " -n " .. ns .. ' -o jsonpath="{.spec.template.spec.containers[*].image}"'
local get_images = "get deploy "
.. name
.. " -n "
.. ns
.. ' -o jsonpath="{.spec.template.spec.containers[*].image}"'

local container_images = {}

Expand Down Expand Up @@ -95,11 +99,15 @@ api.nvim_buf_set_keymap(0, "n", "r", "", {
local ns, name = tables.getCurrentSelection(unpack({ 1, 2 }))
buffers.confirmation_buffer("Are you sure that you want to restart the deployment: " .. name, nil, function(confirm)
if confirm then
commands.shell_command_async("kubectl", { "rollout", "restart", "deployment/" .. name, "-n", ns }, function(response)
vim.schedule(function()
vim.notify(response)
end)
end)
commands.shell_command_async(
"kubectl",
{ "rollout", "restart", "deployment/" .. name, "-n", ns },
function(response)
vim.schedule(function()
vim.notify(response)
end)
end
)
end
end)
end,
Expand Down
14 changes: 11 additions & 3 deletions ftplugin/k8s_pods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ api.nvim_buf_set_keymap(0, "n", "<S-f>", "", {
local current_port_result = current_ports_result[1]

local confirmation_str = function(local_port, dest_port)
return "Are you sure that you want to port forward from " .. dest_port .. " on " .. pod_name .. " to " .. local_port .. " locally?"
return "Are you sure that you want to port forward from "
.. dest_port
.. " on "
.. pod_name
.. " to "
.. local_port
.. " locally?"
end

vim.ui.input({ prompt = "Local port: " }, function(local_port)
Expand All @@ -134,7 +140,8 @@ api.nvim_buf_set_keymap(0, "n", "<S-f>", "", {
current_port_result = dest_port
buffers.confirmation_buffer(confirmation_str(local_port, current_port_result), nil, function(confirm)
if confirm then
local port_forward_query = { "port-forward", "-n", namespace, "pods/" .. pod_name, local_port .. ":" .. dest_port }
local port_forward_query =
{ "port-forward", "-n", namespace, "pods/" .. pod_name, local_port .. ":" .. dest_port }
commands.shell_command_async("kubectl", port_forward_query, function(response)
vim.schedule(function()
vim.notify(response)
Expand All @@ -146,7 +153,8 @@ api.nvim_buf_set_keymap(0, "n", "<S-f>", "", {
else
buffers.confirmation_buffer(confirmation_str(local_port, current_port_result), nil, function(confirm)
if confirm then
local port_forward_query = { "port-forward", "-n", namespace, "pods/" .. pod_name, input .. ":" .. current_port_result }
local port_forward_query =
{ "port-forward", "-n", namespace, "pods/" .. pod_name, local_port .. ":" .. current_port_result }
commands.shell_command_async("kubectl", port_forward_query, function(response)
vim.schedule(function()
vim.notify(response)
Expand Down
4 changes: 2 additions & 2 deletions lua/kubectl/actions/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function M.filter_buffer(content, marks, filetype, opts)
if buf == -1 then
buf = create_buffer(bufname, "prompt")
vim.keymap.set("n", "q", function()
vim.bo.modified = false
api.nvim_set_option_value("modified", false, { buf = buf })
vim.cmd.close()
end, { buffer = buf, silent = true })
end
Expand All @@ -86,7 +86,7 @@ function M.filter_buffer(content, marks, filetype, opts)
state.setFilter(input)
end

vim.bo.modified = false
api.nvim_set_option_value("modified", false, { buf = buf })
vim.cmd.close()
vim.api.nvim_input("R")
end)
Expand Down
2 changes: 1 addition & 1 deletion lua/kubectl/actions/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function M.shell_command_async(cmd, args, on_exit, on_stdout)
end
end,

stderr = function(err, data)
stderr = function(_, data)
vim.schedule(function()
if data then
vim.notify(data, vim.log.levels.ERROR)
Expand Down
11 changes: 8 additions & 3 deletions lua/kubectl/resourcebuilder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ local tables = require("kubectl.utils.tables")
local ResourceBuilder = {}
ResourceBuilder.__index = ResourceBuilder

function ResourceBuilder:new(resource, args)
local self = setmetatable({}, ResourceBuilder)
function ResourceBuilder:new(resource, args) -- luacheck: ignore
self = setmetatable({}, ResourceBuilder)
self.resource = resource
self.args = args
self.header = { data = nil, marks = nil }
Expand Down Expand Up @@ -152,7 +152,12 @@ function ResourceBuilder:displayFloat(filetype, title, syntax, usePrettyData)
"display data " .. "[" .. self.resource .. "]",
})
notifications.Close()
buffers.floating_buffer(displayData, self.extmarks, filetype, { title = title, syntax = syntax, header = self.header })
buffers.floating_buffer(
displayData,
self.extmarks,
filetype,
{ title = title, syntax = syntax, header = self.header }
)

return self
end
Expand Down
9 changes: 7 additions & 2 deletions lua/kubectl/utils/marks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end

function M.set_sortby_header()
local sortby = state.sortby
if #sortby.mark == 0 then
if #sortby.mark == 0 and state.marks.header[1] then
local extmark = vim.api.nvim_buf_get_extmark_by_id(0, state.marks.ns_id, state.marks.header[1], { details = true })
if extmark and #extmark >= 3 then
local start_row, start_col, end_row, end_col = extmark[1], extmark[2], extmark[3].end_row, extmark[3].end_col
Expand All @@ -32,7 +32,12 @@ function M.set_sortby_header()
end

if #sortby.mark > 0 then
M.set_virtual_text_on_mark(0, state.marks.ns_id, { sortby.mark[1], sortby.mark[2], sortby.mark[3] }, sortby.current_word .. " ▼")
M.set_virtual_text_on_mark(
0,
state.marks.ns_id,
{ sortby.mark[1], sortby.mark[2], sortby.mark[3] },
sortby.current_word .. " ▼"
)
end
end
function M.set_virtual_text_on_mark(bufnr, ns_id, mark, virt_text)
Expand Down
5 changes: 1 addition & 4 deletions lua/kubectl/utils/tables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ function M.getCurrentSelection(...)
end

function M.isEmpty(table)
for _ in pairs(table) do
return false
end
return true
return next(table) == nil
end

return M
14 changes: 12 additions & 2 deletions lua/kubectl/utils/time.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function M.since(timestamp, fresh, currentTime)
end

if not currentTime then
currentTime = os.time(os.date("!*t"))
currentTime = M.currentTime()
end

local parsedTime = M.parse(timestamp)
Expand Down Expand Up @@ -60,7 +60,17 @@ function M.since(timestamp, fresh, currentTime)
end

function M.currentTime()
return os.time(os.date("!*t"))
local date = os.date("!*t")
if date then
return os.time({
year = date.year,
month = date.month,
day = date.day,
hour = date.hour,
min = date.min,
sec = date.sec,
})
end
end

return M
1 change: 0 additions & 1 deletion lua/kubectl/views/configmaps/definition.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

local M = {}
local time = require("kubectl.utils.time")

Expand Down
20 changes: 11 additions & 9 deletions lua/kubectl/views/configmaps/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ local definition = require("kubectl.views.configmaps.definition")
local M = {}

function M.View(cancellationToken)
ResourceBuilder:new("configmaps"):setCmd({ "get", "--raw", "/api/v1/{{NAMESPACE}}configmaps" }):fetchAsync(function(self)
self:decodeJson():process(definition.processRow):sort():prettyPrint(definition.getHeaders)
ResourceBuilder:new("configmaps")
:setCmd({ "get", "--raw", "/api/v1/{{NAMESPACE}}configmaps" })
:fetchAsync(function(self)
self:decodeJson():process(definition.processRow):sort():prettyPrint(definition.getHeaders)

vim.schedule(function()
self
:addHints({
{ key = "<d>", desc = "describe" },
}, true, true)
:display("k8s_configmaps", "Configmaps", cancellationToken)
vim.schedule(function()
self
:addHints({
{ key = "<d>", desc = "describe" },
}, true, true)
:display("k8s_configmaps", "Configmaps", cancellationToken)
end)
end)
end)
end

function M.Edit(name, namespace)
Expand Down
6 changes: 3 additions & 3 deletions lua/kubectl/views/containers/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ local function getPorts(ports)
end
return string_ports
end

local function getContainerState(state)
for key, _ in pairs(state) do
return key
end
local key = next(state)
return key
end

function M.processContainerRow(row)
Expand Down
22 changes: 12 additions & 10 deletions lua/kubectl/views/containers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ function M.selectContainer(name)
end

function M.View(pod, ns)
ResourceBuilder:new("containers"):setCmd({ "get", "--raw", "/api/v1/namespaces/" .. ns .. "/pods/" .. pod }):fetchAsync(function(self)
self:decodeJson():process(definition.processContainerRow, true):prettyPrint(definition.getContainerHeaders)
ResourceBuilder:new("containers")
:setCmd({ "get", "--raw", "/api/v1/namespaces/" .. ns .. "/pods/" .. pod })
:fetchAsync(function(self)
self:decodeJson():process(definition.processContainerRow, true):prettyPrint(definition.getContainerHeaders)

vim.schedule(function()
self
:addHints({
{ key = "<l>", desc = "logs" },
{ key = "<enter>", desc = "exec" },
}, false, false)
:displayFloat("k8s_containers", pod, "", true)
vim.schedule(function()
self
:addHints({
{ key = "<l>", desc = "logs" },
{ key = "<enter>", desc = "exec" },
}, false, false)
:displayFloat("k8s_containers", pod, "", true)
end)
end)
end)
end

function M.tailLogs(pod, ns)
Expand Down
36 changes: 20 additions & 16 deletions lua/kubectl/views/deployments/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ local definition = require("kubectl.views.deployments.definition")
local M = {}

function M.View(cancellationToken)
ResourceBuilder:new("deployments"):setCmd({ "get", "--raw", "/apis/apps/v1/{{NAMESPACE}}deployments" }):fetchAsync(function(self)
self:decodeJson():process(definition.processRow):sort():prettyPrint(definition.getHeaders)
vim.schedule(function()
self
:addHints({
{ key = "<r>", desc = "restart" },
{ key = "<d>", desc = "desc" },
{ key = "<enter>", desc = "pods" },
}, true, true)
:display("k8s_deployments", "Deployments", cancellationToken)
ResourceBuilder:new("deployments")
:setCmd({ "get", "--raw", "/apis/apps/v1/{{NAMESPACE}}deployments" })
:fetchAsync(function(self)
self:decodeJson():process(definition.processRow):sort():prettyPrint(definition.getHeaders)
vim.schedule(function()
self
:addHints({
{ key = "<r>", desc = "restart" },
{ key = "<d>", desc = "desc" },
{ key = "<enter>", desc = "pods" },
}, true, true)
:display("k8s_deployments", "Deployments", cancellationToken)
end)
end)
end)
end

function M.Edit(name, namespace)
Expand All @@ -27,12 +29,14 @@ function M.Edit(name, namespace)
end

function M.DeploymentDesc(deployment_desc, namespace)
ResourceBuilder:new("desc"):setCmd({ "describe", "deployment", deployment_desc, "-n", namespace }):fetchAsync(function(self)
self:splitData()
vim.schedule(function()
self:displayFloat("k8s_deployment_desc", deployment_desc, "yaml")
ResourceBuilder:new("desc")
:setCmd({ "describe", "deployment", deployment_desc, "-n", namespace })
:fetchAsync(function(self)
self:splitData()
vim.schedule(function()
self:displayFloat("k8s_deployment_desc", deployment_desc, "yaml")
end)
end)
end)
end

return M
7 changes: 6 additions & 1 deletion lua/kubectl/views/filter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ function M.filter()
{ key = "<enter>", desc = "apply" },
{ key = "<q>", desc = "close" },
}, false, false)
buffers.filter_buffer("Filter: ", marks, "k8s_filter", { title = "Filter", header = { data = header, marks = marks } })
buffers.filter_buffer(
"Filter: ",
marks,
"k8s_filter",
{ title = "Filter", header = { data = header, marks = marks } }
)
end

return M
6 changes: 5 additions & 1 deletion lua/kubectl/views/namespace/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ function M.changeNamespace(name)
vim.api.nvim_win_close(win, true)
vim.api.nvim_input("R")
else
commands.shell_command_async("kubectl", { "config", "set-context", "--current", "--namespace=" .. name }, handle_output)
commands.shell_command_async(
"kubectl",
{ "config", "set-context", "--current", "--namespace=" .. name },
handle_output
)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/kubectl/views/pods/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end

function M.processRow(rows)
local data = {}
local currentTime = os.time(os.date("!*t"))
local currentTime = time.currentTime()
if rows and rows.items then
for i = 1, #rows.items do
local row = rows.items[i]
Expand Down
3 changes: 2 additions & 1 deletion lua/kubectl/views/services/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ local function getType(type)
end

--TODO: Get externalip
local function getExternalIP(spec)
---@diagnostic disable-next-line: unused-local
local function getExternalIP(spec) -- luacheck: ignore
return ""
end

Expand Down
2 changes: 1 addition & 1 deletion stylua.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
indent_type = "Spaces"
indent_width = 2
column_width = 140
column_width = 120

[sort_requires]
enabled = true
Loading