54 lines
1.3 KiB
Lua
54 lines
1.3 KiB
Lua
--- jasper/config.lua
|
|
--- Reads the .jasper_config.json file from the project root.
|
|
---
|
|
--- Expected format:
|
|
--- {
|
|
--- "attivita_id": 12345,
|
|
--- "inactivity_timeout": 10 -- optional, minutes
|
|
--- }
|
|
|
|
local M = {}
|
|
|
|
--- Walk up from `cwd` looking for .jasper_config.json.
|
|
--- @return string|nil absolute path to the config file, or nil
|
|
local function find_config_file()
|
|
local path = vim.fn.getcwd()
|
|
while true do
|
|
local candidate = path .. "/.jasper_config.json"
|
|
if vim.fn.filereadable(candidate) == 1 then
|
|
return candidate
|
|
end
|
|
local parent = vim.fn.fnamemodify(path, ":h")
|
|
if parent == path then
|
|
break -- reached filesystem root
|
|
end
|
|
path = parent
|
|
end
|
|
return nil
|
|
end
|
|
|
|
--- Load and decode .jasper_config.json.
|
|
--- @return table|nil decoded config, or nil if not found / invalid
|
|
function M.load()
|
|
local config_file = find_config_file()
|
|
if not config_file then
|
|
return nil
|
|
end
|
|
|
|
local lines = vim.fn.readfile(config_file)
|
|
if not lines or #lines == 0 then
|
|
vim.notify("[Jasper] .jasper_config.json is empty", vim.log.levels.WARN)
|
|
return nil
|
|
end
|
|
|
|
local ok, data = pcall(vim.fn.json_decode, table.concat(lines, "\n"))
|
|
if not ok or type(data) ~= "table" then
|
|
vim.notify("[Jasper] .jasper_config.json is not valid JSON", vim.log.levels.WARN)
|
|
return nil
|
|
end
|
|
|
|
return data
|
|
end
|
|
|
|
return M
|