Skip to content

Commit

Permalink
mv log_error to utils (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohdearaugustin authored May 30, 2024
1 parent d273204 commit 0539e74
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
11 changes: 4 additions & 7 deletions lua/modules/command.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
-- modules/command.lua

local Command = {}
local Utils = require("modules.utils")

-- You can replace this with a more sophisticated logging function if available
local function log_error(message)
print("Error: " .. message)
end
local Command = {}

function Command.run_shell_command(cmd)
-- Attempt to open a pipe to run the command and capture both stdout and stderr
local handle, err = io.popen(cmd .. " 2>&1", "r")
if not handle then
-- Log the error and return nil along with the error message
log_error("Failed to run command: " .. cmd .. "\n" .. tostring(err))
Utils.log_error("Failed to run command: " .. cmd .. "\n" .. tostring(err))
return nil, "Error running command: " .. tostring(err)
end

Expand All @@ -22,7 +19,7 @@ function Command.run_shell_command(cmd)
local success, close_err = handle:close()
if not success then
-- Log the error and return nil along with the error message
log_error("Failed to close command handle for: " .. cmd .. "\n" .. tostring(close_err))
Utils.log_error("Failed to close command handle for: " .. cmd .. "\n" .. tostring(close_err))
return nil, "Error closing command handle: " .. tostring(close_err)
end

Expand Down
45 changes: 21 additions & 24 deletions lua/modules/helm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@ local Command = require("modules.command")
local Repository = require("modules.repository")
local TelescopePicker = require("modules.telescope_picker")
local Kubectl = require("modules.kubectl")
local Utils = require("modules.utils")

local Helm = {}

local function log_error(message)
print("Error: " .. message)
end

local function fetch_releases(namespace)
-- Construct the helm list command with the provided namespace
local releases_cmd = string.format("helm list -n %s -q", namespace)
local releases, err = Command.run_shell_command(releases_cmd)

-- Check if the command was successful
if not releases or releases == "" then
log_error(err or "Failed to fetch Helm releases.")
Utils.log_error(err or "Failed to fetch Helm releases.")
return nil
end

Expand All @@ -27,7 +24,7 @@ local function fetch_releases(namespace)

-- Check if the list is empty
if #release_list == 0 then
log_error("No Helm releases available.")
Utils.log_error("No Helm releases available.")
return nil
end

Expand All @@ -37,13 +34,13 @@ end
function Helm.dependency_update_from_buffer()
local file_path = vim.api.nvim_buf_get_name(0)
if file_path == "" then
log_error("No file selected")
Utils.log_error("No file selected")
return
end

local chart_directory = file_path:match("(.*/)")
if not chart_directory then
log_error("Failed to determine chart directory from file path")
Utils.log_error("Failed to determine chart directory from file path")
return
end

Expand All @@ -63,28 +60,28 @@ function Helm.dependency_update_from_buffer()
print("Adding missing repository: " .. repo_check_err)
end
else
log_error("Repository information is missing in Chart.yaml")
Utils.log_error("Repository information is missing in Chart.yaml")
end

-- Execute the dependency update command
local result, err = Command.run_shell_command(helm_cmd)
if result then
print("Helm dependency update successful: \n" .. result)
else
log_error("Helm dependency update failed: " .. (err or "Unknown error"))
Utils.log_error("Helm dependency update failed: " .. (err or "Unknown error"))
end
end

function Helm.dependency_build_from_buffer()
local file_path = vim.api.nvim_buf_get_name(0)
if file_path == "" then
log_error("No file selected")
Utils.log_error("No file selected")
return
end

local chart_directory = file_path:match("(.*/)")
if not chart_directory then
log_error("Failed to determine chart directory from file path")
Utils.log_error("Failed to determine chart directory from file path")
return
end

Expand All @@ -93,7 +90,7 @@ function Helm.dependency_build_from_buffer()
if result then
print("Helm dependency build successful: \n" .. result)
else
log_error("Helm dependency build failed: " .. (err or "Unknown error"))
Utils.log_error("Helm dependency build failed: " .. (err or "Unknown error"))
end
end

Expand All @@ -103,7 +100,7 @@ local function generate_helm_template(chart_directory)

-- Handle the case where original_directory is nil
if original_directory == "" then
log_error("Failed to get the current working directory")
Utils.log_error("Failed to get the current working directory")
return "Error: Failed to get the current working directory"
end

Expand All @@ -119,21 +116,21 @@ local function generate_helm_template(chart_directory)
return result
else
local error_message = "Helm template generation failed: " .. (err or "Unknown error")
log_error(error_message)
Utils.log_error(error_message)
return error_message
end
end

function Helm.template_from_buffer()
local file_path = vim.api.nvim_buf_get_name(0)
if file_path == "" then
log_error("No file selected")
Utils.log_error("No file selected")
return
end

local chart_directory = file_path:match("(.*/)")
if not chart_directory then
log_error("Failed to determine chart directory from file path")
Utils.log_error("Failed to determine chart directory from file path")
return
end

Expand All @@ -154,12 +151,12 @@ function Helm.deploy_from_buffer()
Kubectl.select_namespace(function(namespace)
local file_path = vim.api.nvim_buf_get_name(0)
if file_path == "" then
log_error("No file selected")
Utils.log_error("No file selected")
return
end
local chart_directory = file_path:match("(.*/)")
if not chart_directory then
log_error("Failed to determine chart directory from file path")
Utils.log_error("Failed to determine chart directory from file path")
return
end

Expand All @@ -175,7 +172,7 @@ function Helm.deploy_from_buffer()
if result and result ~= "" then
print("Deployment successful: \n" .. result)
else
log_error("Deployment failed: " .. (helm_err or "Unknown error"))
Utils.log_error("Deployment failed: " .. (helm_err or "Unknown error"))
end
end)
end)
Expand All @@ -187,12 +184,12 @@ function Helm.dryrun_from_buffer()
Kubectl.select_namespace(function(namespace)
local file_path = vim.api.nvim_buf_get_name(0)
if file_path == "" then
log_error("No file selected")
Utils.log_error("No file selected")
return
end
local chart_directory = file_path:match("(.*/)")
if not chart_directory then
log_error("Failed to determine chart directory from file path")
Utils.log_error("Failed to determine chart directory from file path")
return
end

Expand All @@ -208,7 +205,7 @@ function Helm.dryrun_from_buffer()

-- Check if the result is nil or empty
if not result or result == "" then
log_error("Dry run failed: " .. (ns_err or "Unknown error"))
Utils.log_error("Dry run failed: " .. (ns_err or "Unknown error"))
return
end

Expand Down Expand Up @@ -259,7 +256,7 @@ function Helm.remove_deployment()
if result and result ~= "" then
print("Deployment removal successful: \n" .. result)
else
log_error("Deployment removal failed: " .. (helm_err or "Unknown error"))
Utils.log_error("Deployment removal failed: " .. (helm_err or "Unknown error"))
end
end)
end)
Expand Down
21 changes: 9 additions & 12 deletions lua/modules/kubectl.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
-- modules/kubectl.lua

local Utils = require("modules.utils")
local Command = require("modules.command")
local TelescopePicker = require("modules.telescope_picker")

local Kubectl = {}

local function log_error(message)
print("Error: " .. message)
end

local function fetch_contexts()
-- Run the shell command to get Kubernetes contexts
local contexts, err = Command.run_shell_command("kubectl config get-contexts -o name")

-- Check if the command was successful
if not contexts or contexts == "" then
log_error(err or "Failed to fetch Kubernetes contexts.")
Utils.log_error(err or "Failed to fetch Kubernetes contexts.")
return nil
end

Expand All @@ -24,7 +21,7 @@ local function fetch_contexts()

-- Check if the list is empty
if #context_list == 0 then
log_error("No Kubernetes contexts available.")
Utils.log_error("No Kubernetes contexts available.")
return nil
end

Expand All @@ -37,7 +34,7 @@ local function fetch_namespaces()

-- Check if the command was successful
if not namespaces or namespaces == "" then
log_error("Failed to fetch namespaces: " .. (err or "No namespaces found."))
Utils.log_error("Failed to fetch namespaces: " .. (err or "No namespaces found."))
return nil
end

Expand All @@ -46,7 +43,7 @@ local function fetch_namespaces()

-- Check if the list is empty
if #namespace_list == 0 then
log_error("No namespaces available.")
Utils.log_error("No namespaces available.")
return nil
end

Expand Down Expand Up @@ -79,7 +76,7 @@ function Kubectl.select_namespace(callback)
print(string.format("Namespace %s created successfully.", new_ns_name))
callback(new_ns_name)
else
log_error("Failed to create namespace: " .. (create_ns_err or "Unknown error"))
Utils.log_error("Failed to create namespace: " .. (create_ns_err or "Unknown error"))
end
end)
else
Expand All @@ -105,7 +102,7 @@ function Kubectl.apply_from_buffer()
TelescopePicker.select_from_list("Select Namespace", namespace_list, function(selected_namespace)
local file_path = vim.api.nvim_buf_get_name(0)
if file_path == "" then
log_error("No file selected")
Utils.log_error("No file selected")
return
end

Expand All @@ -114,7 +111,7 @@ function Kubectl.apply_from_buffer()
if result and result ~= "" then
print("kubectl apply successful: \n" .. result)
else
log_error("kubectl apply failed: " .. (apply_err or "Unknown error"))
Utils.log_error("kubectl apply failed: " .. (apply_err or "Unknown error"))
end
end)
end)
Expand Down Expand Up @@ -146,7 +143,7 @@ function Kubectl.select_and_delete_namespace()
if result then
print("Namespace " .. selected_namespace .. " successfully deleted.")
else
log_error("Failed to delete namespace " .. selected_namespace .. ": " .. (err or "Unknown error"))
Utils.log_error("Failed to delete namespace " .. selected_namespace .. ": " .. (err or "Unknown error"))
end
else
print("Deletion cancelled.")
Expand Down
10 changes: 10 additions & 0 deletions lua/modules/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- modules/utils.lua

local Utils = {}

-- Function log_error
function Utils.log_error(message)
print("Error: " .. message)
end

return Utils

0 comments on commit 0539e74

Please sign in to comment.