2022-05-13 09:27:02 +00:00
|
|
|
-- env fileread
|
|
|
|
local util = require("go.utils")
|
|
|
|
local log = util.log
|
|
|
|
local M = {}
|
2022-06-30 16:44:39 +00:00
|
|
|
local vfn = vim.fn
|
2022-05-13 09:27:02 +00:00
|
|
|
local sep = require("go.utils").sep()
|
|
|
|
|
|
|
|
function M.envfile(f)
|
2022-06-30 16:44:39 +00:00
|
|
|
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
|
2022-05-13 09:27:02 +00:00
|
|
|
local goenv = workfolder .. sep .. (f or ".env")
|
|
|
|
|
2022-06-30 16:44:39 +00:00
|
|
|
if vfn.filereadable(goenv) == 1 then
|
2022-05-13 09:27:02 +00:00
|
|
|
return goenv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-30 16:44:39 +00:00
|
|
|
function M.append(env, val)
|
|
|
|
local oldval = vfn.getenv(env)
|
2022-06-30 22:26:14 +00:00
|
|
|
if val == vim.NIL or string.find(oldval, val) then
|
|
|
|
return
|
|
|
|
end
|
2022-06-30 16:44:39 +00:00
|
|
|
local newval = oldval .. ":" .. val
|
|
|
|
vfn.setenv(env, newval)
|
|
|
|
end
|
|
|
|
|
2022-05-13 09:27:02 +00:00
|
|
|
function M.load_env(env, setToEnv)
|
2022-05-14 11:24:19 +00:00
|
|
|
setToEnv = setToEnv or true
|
2022-05-13 09:27:02 +00:00
|
|
|
env = env or M.envfile()
|
2022-06-30 16:44:39 +00:00
|
|
|
if vfn.filereadable(env) == 0 then
|
2022-05-13 09:27:02 +00:00
|
|
|
return false
|
|
|
|
end
|
2022-05-14 11:24:19 +00:00
|
|
|
local lines = util.lines_from(env)
|
2022-05-13 09:27:02 +00:00
|
|
|
local envs = {}
|
|
|
|
for _, line in ipairs(lines) do
|
2022-05-14 11:24:19 +00:00
|
|
|
for k, v in string.gmatch(line, "([%w_]+)=([%w%c%p%z]+)") do
|
2022-05-13 09:27:02 +00:00
|
|
|
envs[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-14 11:24:19 +00:00
|
|
|
log(envs)
|
2022-05-13 09:27:02 +00:00
|
|
|
if setToEnv then
|
|
|
|
for key, val in pairs(envs) do
|
2022-06-30 16:44:39 +00:00
|
|
|
vfn.setenv(key, val)
|
2022-05-13 09:27:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return envs
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|