Skip to content

Commit

Permalink
create combined file path if doesnt exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Revolyssup committed Feb 25, 2025
1 parent 04be7b3 commit 58287b9
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions apisix/cli/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,36 @@ do
end


local function ensure_dir(path)
-- Extract directory from path
local dir = path:match("(.*/)")
if dir then
-- Try to create directory recursively.
-- This uses "mkdir -p" to avoid error if the directory already exists.
local ok = os.execute("mkdir -p " .. dir)
if not ok then
error("Failed to create directory: " .. dir)
end
end
end

function _M.gen_trusted_certs_combined_file(combined_filepath, paths)
local combined_file = assert(io.open(combined_filepath, "w"))
-- Ensure the directory for combined_filepath exists.
ensure_dir(combined_filepath)

local combined_file, err = io.open(combined_filepath, "w")
if not combined_file then
error("Failed to open or create combined file at " .. combined_filepath ..
". Error: " .. tostring(err))
end

for _, path in ipairs(paths) do
local cert_file = assert(io.open(path, "r"))
combined_file:write(cert_file:read("*a"))
local cert_file, cert_err = io.open(path, "r")
if not cert_file then
error("Failed to open certificate file " .. path .. ": " .. tostring(cert_err))
end
local data = cert_file:read("*a") or ""
combined_file:write(data)
combined_file:write("\n")
cert_file:close()
end
Expand Down

0 comments on commit 58287b9

Please sign in to comment.