Skip to content

Commit

Permalink
fix(env): parser (#138)
Browse files Browse the repository at this point in the history
closes #129.
  • Loading branch information
gorillamoe authored Aug 14, 2024
1 parent b158c04 commit 966658a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 69 deletions.
5 changes: 4 additions & 1 deletion lua/kulala/db/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
local M = {}

M.data = {
env = {},
selected_env = nil, -- string - name of selected env
http_client_env = nil, -- table of envs from http-client.env.json
http_client_env_base = nil, -- table of base env values which should be applied to all requests
env = {}, -- table of envs from document sources
}

return M
13 changes: 0 additions & 13 deletions lua/kulala/global_store/init.lua

This file was deleted.

2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs")

local M = {}

M.VERSION = "3.1.2"
M.VERSION = "3.1.3"
M.UI_ID = "kulala://ui"
M.SCRATCHPAD_ID = "kulala://scratchpad"
M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt"
Expand Down
8 changes: 2 additions & 6 deletions lua/kulala/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local UI = require("kulala.ui")
local SELECTOR = require("kulala.ui.selector")
local ENV_PARSER = require("kulala.parser.env")
local GLOBAL_STORE = require("kulala.global_store")
local DB = require("kulala.db")
local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local JUMPS = require("kulala.jumps")
Expand All @@ -11,9 +10,6 @@ local M = {}

M.setup = function(config)
CONFIG.setup(config)
GLOBAL_STORE.set("selected_env", CONFIG.get().default_env)
vim.g.kulala_selected_env = CONFIG.get().default_env
ENV_PARSER.load_envs()
end

M.run = function()
Expand Down Expand Up @@ -76,7 +72,7 @@ M.set_selected_env = function(env)
end
return
end
GLOBAL_STORE.set("selected_env", env)
vim.g.kulala_selected_env = env
end

return M
45 changes: 21 additions & 24 deletions lua/kulala/parser/env.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
local Config = require("kulala.config")
local FS = require("kulala.utils.fs")
local GLOBAL_STORE = require("kulala.global_store")
local DYNAMIC_VARS = require("kulala.parser.dynamic_vars")
local DB = require("kulala.db")

local M = {}

local http_client_env_json = FS.find_file_in_parent_dirs("http-client.env.json")
local dotenv = FS.find_file_in_parent_dirs(".env")

M.get_env = function()
local http_client_env_json = FS.find_file_in_parent_dirs("http-client.env.json")
local dotenv = FS.find_file_in_parent_dirs(".env")
local env = {}

for key, value in pairs(vim.fn.environ()) do
env[key] = value
end
if http_client_env_json then
local f = vim.fn.json_decode(vim.fn.readfile(http_client_env_json))
if f._base then
GLOBAL_STORE.set("http_client_env_base", f._base)
end
f._base = nil
GLOBAL_STORE.set("http_client_env", f)
if not f then
vim.notify("http-client.env.json is not a valid json file", vim.log.levels.ERROR)
return env
end
local selected_env_name = GLOBAL_STORE.get("selected_env")
local selected_env = f[selected_env_name]
if selected_env then
env = vim.tbl_extend("force", env, selected_env)
end
elseif dotenv then

if dotenv then
local dotenv_env = vim.fn.readfile(dotenv)
for _, line in ipairs(dotenv_env) do
-- if the line is not empy and not a comment, then
Expand All @@ -41,12 +25,25 @@ M.get_env = function()
end
end
end

if http_client_env_json then
local f = vim.fn.json_decode(vim.fn.readfile(http_client_env_json))
if f._base then
DB.data.http_client_env_base = f._base
end
f._base = nil
DB.data.http_client_env = f
local selected_env = f[vim.g.kulala_selected_env or Config.get().default_env]
if selected_env then
env = vim.tbl_extend("force", env, selected_env)
end
end

for key, value in pairs(DB.data.env) do
env[key] = value
end

return env
end

M.load_envs = M.get_env

return M
29 changes: 14 additions & 15 deletions lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ local DYNAMIC_VARS = require("kulala.parser.dynamic_vars")
local ENV_PARSER = require("kulala.parser.env")
local FS = require("kulala.utils.fs")
local GLOBALS = require("kulala.globals")
local GLOBAL_STORE = require("kulala.global_store")
local GRAPHQL_PARSER = require("kulala.parser.graphql")
local REQUEST_VARIABLES = require("kulala.parser.request_variables")
local STRING_UTILS = require("kulala.utils.string")
local PARSER_UTILS = require("kulala.parser.utils")
local PLUGIN_TMP_DIR = FS.get_plugin_tmp_dir()
local M = {}

local function parse_string_variables(str, variables)
local env = ENV_PARSER.get_env()
local function parse_string_variables(str, variables, env)
local function replace_placeholder(variable_name)
local value = ""
-- If the variable name contains a `$` symbol then try to parse it as a dynamic variable
Expand All @@ -23,7 +21,7 @@ local function parse_string_variables(str, variables)
value = variable_value
end
elseif variables[variable_name] then
value = parse_string_variables(variables[variable_name], variables)
value = parse_string_variables(variables[variable_name], variables, env)
elseif env[variable_name] then
value = env[variable_name]
elseif REQUEST_VARIABLES.parse(variable_name) then
Expand All @@ -42,10 +40,10 @@ local function parse_string_variables(str, variables)
return result
end

local function parse_headers(headers, variables)
local function parse_headers(headers, variables, env)
local h = {}
for key, value in pairs(headers) do
h[key] = parse_string_variables(value, variables)
h[key] = parse_string_variables(value, variables, env)
end
return h
end
Expand Down Expand Up @@ -73,18 +71,18 @@ local function encode_url_params(url)
return url
end

local function parse_url(url, variables)
url = parse_string_variables(url, variables)
local function parse_url(url, variables, env)
url = parse_string_variables(url, variables, env)
url = encode_url_params(url)
url = url:gsub('"', "")
return url
end

local function parse_body(body, variables)
local function parse_body(body, variables, env)
if body == nil then
return nil
end
return parse_string_variables(body, variables)
return parse_string_variables(body, variables, env)
end

M.get_document = function()
Expand Down Expand Up @@ -294,6 +292,7 @@ function M.parse()
ft = "text",
}

local env = ENV_PARSER.get_env()
local document_variables, requests = M.get_document()
local req = M.get_request_at_cursor(requests)

Expand All @@ -302,11 +301,11 @@ function M.parse()
document_variables = extend_document_variables(document_variables, req)

res.show_icon_line_number = req.show_icon_line_number
res.url = parse_url(req.url, document_variables)
res.url = parse_url(req.url, document_variables, env)
res.method = req.method
res.http_version = req.http_version
res.headers = parse_headers(req.headers, document_variables)
res.body = parse_body(req.body, document_variables)
res.headers = parse_headers(req.headers, document_variables, env)
res.body = parse_body(req.body, document_variables, env)
res.metadata = req.metadata

-- We need to append the contents of the file to
Expand Down Expand Up @@ -334,8 +333,8 @@ function M.parse()
end

-- Merge headers from the _base environment if it exists
if GLOBAL_STORE.get("http_client_env_base") then
local default_headers = GLOBAL_STORE.get("http_client_env_base")["DEFAULT_HEADERS"]
if DB.data.http_client_env_base then
local default_headers = DB.data.http_client_env_base["DEFAULT_HEADERS"]
if default_headers then
for key, value in pairs(default_headers) do
key = key:lower()
Expand Down
7 changes: 3 additions & 4 deletions lua/kulala/ui/selector.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local GLOBAL_STORE = require("kulala.global_store")
local DB = require("kulala.db")
local FS = require("kulala.utils.fs")

local M = {}

function M.select_env()
if not GLOBAL_STORE.get("http_client_env") then
if not DB.data.http_client_env then
return
end

local envs = {}
for key, _ in pairs(GLOBAL_STORE.get("http_client_env")) do
for key, _ in pairs(DB.data.http_client_env) do
table.insert(envs, key)
end

Expand All @@ -20,7 +20,6 @@ function M.select_env()
if not result then
return
end
GLOBAL_STORE.set("selected_env", result)
vim.g.kulala_selected_env = result
end)
end
Expand Down
9 changes: 4 additions & 5 deletions lua/telescope/_extensions/kulala.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if not has_telescope then
return nil
end

local GLOBAL_STORE = require("kulala.global_store")
local DB = require("kulala.db")
local FS = require("kulala.utils.fs")

local action_state = require("telescope.actions.state")
Expand Down Expand Up @@ -43,12 +43,12 @@ local function kulala_search(_)
end

local function kulala_env_select(_)
if not GLOBAL_STORE.get("http_client_env") then
if not DB.data.http_client_env then
return
end

local envs = {}
for key, _ in pairs(GLOBAL_STORE.get("http_client_env")) do
for key, _ in pairs(DB.data.http_client_env) do
table.insert(envs, key)
end

Expand All @@ -65,15 +65,14 @@ local function kulala_env_select(_)
if selection == nil then
return
end
GLOBAL_STORE.set("selected_env", selection.value)
vim.g.kulala_selected_env = selection.value
end)
return true
end,
previewer = previewers.new_buffer_previewer({
title = "Environment",
define_preview = function(self, entry)
local env = GLOBAL_STORE.get("http_client_env")[entry.value]
local env = DB.data.http_client_env[entry.value]
if env == nil then
return
end
Expand Down

0 comments on commit 966658a

Please sign in to comment.