Skip to content

Commit 8c76318

Browse files
committed
refactor: improve token file reading with utils
Replace direct vim file reading and JSON decoding functions with utility functions for better error handling and code consistency. This change makes the token retrieval more robust by safely handling file reading and JSON parsing failures.
1 parent 3a562fd commit 8c76318

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

lua/CopilotChat/config/providers.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ local function get_github_token()
8686
}
8787

8888
for _, file_path in ipairs(file_paths) do
89-
if vim.fn.filereadable(file_path) == 1 then
90-
local userdata = vim.fn.json_decode(vim.fn.readfile(file_path))
91-
for key, value in pairs(userdata) do
92-
if string.find(key, 'github.com') then
93-
cached_github_token = value.oauth_token
94-
return value.oauth_token
89+
local file_data = utils.read_file(file_path)
90+
if file_data then
91+
local parsed_data = utils.json_decode(file_data)
92+
if parsed_data then
93+
for key, value in pairs(parsed_data) do
94+
if string.find(key, 'github.com') then
95+
cached_github_token = value.oauth_token
96+
return value.oauth_token
97+
end
9598
end
9699
end
97100
end

0 commit comments

Comments
 (0)