Skip to content

Commit

Permalink
fix: show once warning when md file is not found locally
Browse files Browse the repository at this point in the history
  • Loading branch information
finxxi committed Sep 11, 2024
1 parent c7e79f3 commit 291881b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lua/sf/md.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ H.list_md_to_retrieve = function()
local file = string.format('%s_%s.json', type, U.target_org)
local md_tbl = U.read_file_json_to_tbl(file, U.get_plugin_folder_path())

if md_tbl ~= nil then
for _, v in ipairs(md_tbl) do
if v["manageableState"] == 'unmanaged' then
md[v["fullName"]] = v
table.insert(md_names, v["fullName"])
end
end
end
end

require("fzf-lua").fzf_exec(md_names, {
actions = {
Expand Down
24 changes: 19 additions & 5 deletions lua/sf/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,30 @@ end
---@return table|nil
M.read_file_json_to_tbl = function(name, path)
local absolute_path = path .. name
local content = M.read_local_file(absolute_path)
local err_fn = function()
vim.notify_once('File not found: ' .. absolute_path, vim.log.levels.WARN)
end
local content = M.read_local_file(absolute_path, err_fn)
if content == nil then
return nil
end

return M.parse_from_json_to_tbl(content)
end

---@param absolute_path string
---@return string|nil
M.read_local_file = function(absolute_path)
--- Reads the content of a local file.
--- @param absolute_path string The path to the file.
--- @param err_fn function|nil Optional function to call in case of an error.
--- @return string|nil The file content or nil if an error occurred.
M.read_local_file = function(absolute_path, err_fn)
local ok, content = pcall(vim.fn.readfile, absolute_path)

if not ok then
M.notify_then_error('File not found: ' .. absolute_path)
if type(err_fn) == "function" then
return err_fn()
else
M.notify_then_error('File not found: ' .. absolute_path)
end
end

return content
Expand Down

0 comments on commit 291881b

Please sign in to comment.