From 966658af457638dd4e9a5fc3ed9a685862ff68e7 Mon Sep 17 00:00:00 2001 From: Marco Kellershoff <1384938+gorillamoe@users.noreply.github.com> Date: Wed, 14 Aug 2024 23:30:49 +0200 Subject: [PATCH] fix(env): parser (#138) closes #129. --- lua/kulala/db/init.lua | 5 +++- lua/kulala/global_store/init.lua | 13 -------- lua/kulala/globals/init.lua | 2 +- lua/kulala/init.lua | 8 ++--- lua/kulala/parser/env.lua | 45 +++++++++++++--------------- lua/kulala/parser/init.lua | 29 +++++++++--------- lua/kulala/ui/selector.lua | 7 ++--- lua/telescope/_extensions/kulala.lua | 9 +++--- 8 files changed, 49 insertions(+), 69 deletions(-) delete mode 100644 lua/kulala/global_store/init.lua diff --git a/lua/kulala/db/init.lua b/lua/kulala/db/init.lua index 82045f7..e7b780d 100644 --- a/lua/kulala/db/init.lua +++ b/lua/kulala/db/init.lua @@ -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 diff --git a/lua/kulala/global_store/init.lua b/lua/kulala/global_store/init.lua deleted file mode 100644 index df10909..0000000 --- a/lua/kulala/global_store/init.lua +++ /dev/null @@ -1,13 +0,0 @@ -KULALA_GLOBAL_STORE = KULALA_GLOBAL_STORE or {} - -local M = {} - -M.set = function(key, value) - KULALA_GLOBAL_STORE[key] = value -end - -M.get = function(key) - return KULALA_GLOBAL_STORE[key] -end - -return M diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index 06b4dcf..5d4ad57 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -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" diff --git a/lua/kulala/init.lua b/lua/kulala/init.lua index 4b595a2..faf5a29 100644 --- a/lua/kulala/init.lua +++ b/lua/kulala/init.lua @@ -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") @@ -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() @@ -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 diff --git a/lua/kulala/parser/env.lua b/lua/kulala/parser/env.lua index be425c7..3e90e0d 100644 --- a/lua/kulala/parser/env.lua +++ b/lua/kulala/parser/env.lua @@ -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 @@ -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 diff --git a/lua/kulala/parser/init.lua b/lua/kulala/parser/init.lua index ebf8d4e..b7aa3c4 100644 --- a/lua/kulala/parser/init.lua +++ b/lua/kulala/parser/init.lua @@ -4,7 +4,6 @@ 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") @@ -12,8 +11,7 @@ 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 @@ -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 @@ -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 @@ -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() @@ -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) @@ -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 @@ -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() diff --git a/lua/kulala/ui/selector.lua b/lua/kulala/ui/selector.lua index 0355b66..acbae84 100644 --- a/lua/kulala/ui/selector.lua +++ b/lua/kulala/ui/selector.lua @@ -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 @@ -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 diff --git a/lua/telescope/_extensions/kulala.lua b/lua/telescope/_extensions/kulala.lua index 0074d33..996e35d 100644 --- a/lua/telescope/_extensions/kulala.lua +++ b/lua/telescope/_extensions/kulala.lua @@ -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") @@ -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 @@ -65,7 +65,6 @@ 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 @@ -73,7 +72,7 @@ local function kulala_env_select(_) 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