forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.lua
More file actions
42 lines (35 loc) · 1.28 KB
/
setup.lua
File metadata and controls
42 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
local M = {}
local function find_config_path()
local config = vim.fn.expand('$XDG_CONFIG_HOME')
if config and vim.fn.isdirectory(config) > 0 then
return config
elseif vim.fn.has('win32') > 0 then
config = vim.fn.expand('~/AppData/Local')
if vim.fn.isdirectory(config) > 0 then return config end
else
config = vim.fn.expand('~/.config')
if vim.fn.isdirectory(config) > 0 then return config
else print("Error: could not find config path")
end
end
end
local function json_body(response)
if response.headers['content-type'] == 'application/json' then
return vim.fn.json_decode(response.body)
end
end
local function oauth_user(token)
return vim.fn.json_decode(vim.fn.system("curl -s --header \"Authorization: Bearer " .. token .. "\" https://api.github.com/user"))
end
local function oauth_save(oauth_token)
local user_data = oauth_user(oauth_token)
local github = {oauth_token = oauth_token, user = user_data.login}
return github
end
M.get_cred = function ()
local userdata = vim.fn.json_decode(vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')"))
local token = userdata["github.com"].oauth_token
local user = oauth_user(token)
return {user = user, token = token}
end
return M