diff --git a/hurl.txt b/hurl.txt index d3b6634..dd49bb6 100644 --- a/hurl.txt +++ b/hurl.txt @@ -35,4 +35,8 @@ buymeacoffee docstrings sweepai pygithub -sandboxed \ No newline at end of file +sandboxed +filereadable +getcwd +infile +isdirectory diff --git a/lua/hurl/main.lua b/lua/hurl/main.lua index 29f411a..5ee8b62 100644 --- a/lua/hurl/main.lua +++ b/lua/hurl/main.lua @@ -58,12 +58,20 @@ end ---@param callback? function The callback function local function request(opts, callback) vim.notify('hurl: running request', vim.log.levels.INFO) + + -- Check vars.env exist on the current file buffer + -- Then inject the command with --variables-file vars.env + local env_file = vim.fn.expand('%:p:h') .. '/vars.env' + if vim.fn.filereadable(env_file) == 1 then + utils.log_info('hurl: found vars.env at ' .. env_file) + table.insert(opts, '--variables-file') + table.insert(opts, env_file) + end + local cmd = vim.list_extend({ 'hurl', '-i', '--no-color' }, opts) response = {} - if _HURL_GLOBAL_CONFIG.debug then - vim.fn.setqflist({ { filename = vim.inspect(cmd), text = vim.inspect(opts) } }) - end + utils.log_info('hurl: running command' .. vim.inspect(cmd)) vim.fn.jobstart(cmd, { on_stdout = on_output, @@ -118,6 +126,46 @@ local function run_current_file(opts) request(opts) end +-- NOTE: Refactor this later if there is a better way +-- Copy vars.env from current directory or tests directory to nvim cache directory +---@return table +local function get_env_file_in_folders() + local root_dir = vim.fn.expand('%:p:h') + local cache_dir = vim.fn.stdpath('cache') + local env_files = { + { path = root_dir .. '/vars.env', dest = cache_dir .. '/vars.env' }, + } + local scan_dir = { + { + dir = '/src', + }, + { + dir = '/test', + }, + { + dir = '/tests', + }, + { + dir = '/server', + }, + { + dir = '/src/tests', + }, + { + dir = '/server/tests', + }, + } + + for _, s in ipairs(scan_dir) do + local dir = root_dir .. s.dir + if vim.fn.isdirectory(dir) == 1 then + table.insert(env_files, { path = dir .. '/vars.env', dest = cache_dir .. '/vars.env' }) + end + end + + return scan_dir +end + --- Run selection ---@param opts table The options local function run_selection(opts) @@ -133,6 +181,21 @@ local function run_selection(opts) return end + local env_files = get_env_file_in_folders() + for _, env in ipairs(env_files) do + if vim.fn.filereadable(env.path) == 1 then + local success = utils.copy_file(env.path, env.dest) + if not success then + utils.log_error('hurl: copy vars.env failed', vim.log.levels.WARN) + else + utils.log_info('hurl: copy vars.env success ' .. env.dest) + table.insert(opts, '--variables-file') + table.insert(opts, env.dest) + end + break + end + end + table.insert(opts, fname) request(opts) diff --git a/lua/hurl/utils.lua b/lua/hurl/utils.lua index cad40a5..16b7f7a 100644 --- a/lua/hurl/utils.lua +++ b/lua/hurl/utils.lua @@ -148,4 +148,29 @@ util.is_html_response = function(content_type) return string.find(content_type, 'text/html') ~= nil end +--- Copy file +---@param src string +---@param dest string +---@return boolean +util.copy_file = function(src, dest) + local infile = io.open(src, 'rb') + if not infile then + return false + end + + local outfile = io.open(dest, 'wb') + if not outfile then + infile:close() + return false + end + + local data = infile:read('*a') -- Read the whole file + outfile:write(data) + + infile:close() + outfile:close() + + return true +end + return util diff --git a/test/example.hurl b/test/example.hurl index f3b2dbc..ed6c8dd 100644 --- a/test/example.hurl +++ b/test/example.hurl @@ -1,4 +1,4 @@ -GET https://api.mangadex.org/statistics/manga/8b34f37a-0181-4f0b-8ce3-01217e9a602c +GET https://api.mangadex.org/statistics/manga/{{manga_id}} GET https://example.org diff --git a/test/vars.env b/test/vars.env new file mode 100644 index 0000000..fc28aa7 --- /dev/null +++ b/test/vars.env @@ -0,0 +1 @@ +manga_id=8b34f37a-0181-4f0b-8ce3-01217e9a602c