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

[Feature] - Implement ability to tail logs #16

Merged
merged 6 commits into from
May 31, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Note: This is still incomplete in that it doesn't handle all possible resources

## ⚡️ Dependencies
- kubectl
- plenary.nvim

## 📦 Installation

Expand All @@ -25,6 +26,7 @@ Install the plugin with your preferred package manager:
return {
{
"ramilito/kubectl.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
keys = {
{
"<leader>k",
Expand Down
10 changes: 10 additions & 0 deletions ftplugin/k8s_pod_logs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local pod_view = require("kubectl.views.pods")

vim.api.nvim_buf_set_keymap(0, "n", "f", "", {
noremap = true,
silent = true,
desc = "Tail logs",
callback = function()
pod_view.TailLogs()
end,
})
1 change: 1 addition & 0 deletions ftplugin/k8s_pods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ api.nvim_buf_set_keymap(0, "n", "R", "", {
pod_view.Pods()
end,
})

19 changes: 19 additions & 0 deletions lua/kubectl/actions/commands.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
local M = {}

function M.shell_command(cmd, args, callback)
local loaded, Job = pcall(require, "plenary.job")
if not loaded then
vim.notify("plenary.nvim is not installed. Please install it to use this feature.", vim.log.levels.ERROR)
return
end

Job:new({
command = cmd,
args = args,
on_stdout = function(_, data)
callback(data)
end,
on_stderr = function(_, data)
callback(data)
end,
}):start()
end

-- Function to execute a shell command and return the output as a table of strings
function M.execute_shell_command(cmd, args)
local full_command = cmd .. " " .. table.concat(args, " ")
Expand Down
26 changes: 26 additions & 0 deletions lua/kubectl/views/pods/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,36 @@ function M.PodTop()
ResourceBuilder:new("top", { "top", "pods", "-A" }):fetch():splitData():displayFloat("k8s_top", "Top", "")
end

function M.TailLogs()
local buf = vim.api.nvim_get_current_buf()
vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(buf), 0 })

local function handle_output(data)
vim.schedule(function()
if vim.api.nvim_buf_is_valid(buf) then
local line_count = vim.api.nvim_buf_line_count(buf)
vim.api.nvim_buf_set_lines(buf, line_count, line_count, false, { data })
vim.api.nvim_set_option_value("modified", false, { buf = buf })
vim.api.nvim_win_set_cursor(0, { line_count + 1, 0 })
end
end)
end

commands.shell_command(
"kubectl",
{ "logs", "--follow", "--since=1s", selection.pod, "-n", selection.ns },
handle_output
)
end

function M.PodLogs(pod_name, namespace)
selection = { pod = pod_name, ns = namespace }
ResourceBuilder:new("logs", { "logs", pod_name, "-n", namespace })
:fetch()
:splitData()
:addHints({
{ key = "<f>", desc = "Follow" },
}, false, false)
:displayFloat("k8s_pod_logs", pod_name, "less")
end

Expand Down