Skip to content

Commit

Permalink
feat: add config option to specify alternative curl binary
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Jul 16, 2024
1 parent 6ae246d commit 1036325
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ Or if you use [Lazy](https://github.com/folke/lazy.nvim), just pass the table in
{
-- Table of strings to specify default headers to be included in each request, i.e. "-i"
default_flags = { },
-- Specify an alternative curl binary that will be used to run curl commands
-- String of either full path, or binary in path
curl_binary = nil,
mappings = {
execute_curl = "<CR>"
}
Expand Down Expand Up @@ -302,6 +305,10 @@ curl.get_scoped_collections()
-- given collection when selected
curl.pick_global_collection()
curl.pick_scoped_collection()

-- Specify an alternative curl binary that will be used to run curl commands
-- String of either full path, or binary in path
curl.set_curl_binary("someothercurl")
```

</details>
Expand Down
10 changes: 10 additions & 0 deletions lua/curl/api.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

local parser = require("curl.parser")
local config = require("curl.config")
local cache = require("curl.cache")
local buffers = require("curl.buffers")
local output_parser = require("curl.output_parser")
Expand Down Expand Up @@ -92,6 +93,11 @@ M.execute_curl = function()
local cursor_pos, lines = buffers.get_command_buffer_and_pos()
local curl_command = parser.parse_curl_command(cursor_pos, lines)

local curl_alias = config.get("curl_binary")
if curl_alias ~= nil then
curl_command = curl_command:gsub("^curl", curl_alias)
end

if curl_command == "" then
notify.error("No curl command found under the cursor")
return
Expand Down Expand Up @@ -120,4 +126,8 @@ M.execute_curl = function()
})
end

M.set_curl_binary = function(binary_name)
config.set("curl_binary", binary_name)
end

return M
6 changes: 6 additions & 0 deletions lua/curl/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
local default_config = {
---@type table<string>
default_flags = {},
---@type string
curl_binary = nil,
---@type table<'execute_curl', string>
mappings = {
execute_curl = "<CR>",
Expand All @@ -26,4 +28,8 @@ function mod.get(key)
return mod.config
end

function mod.set(key, value)
mod.config[key] = value
end

return mod
43 changes: 43 additions & 0 deletions tests/config/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ local config = require("curl.config")
local test_util = require("tests.test_util")
local api = require("curl.api")

after_each(function()
api.close_curl_tab(true)
end)

describe("Config", function()
it("has default mapping", function()
local default_mapping = config.get("mappings")["execute_curl"]
Expand Down Expand Up @@ -37,4 +41,43 @@ describe("Config", function()
vim.fn.jobstart = mock_pre
curl.setup({})
end)

it("can set alternative curl alias from config", function()
local curl_alias = "/my/cool/curl"
curl.setup({ curl_binary = curl_alias })
test_util.assert_equals(config.get("curl_binary"), curl_alias, "Curl alias should be set")

local curl_command = "curl localhost:8000"

local mocked_jobstart = function(command, _)
test_util.assert_equals(curl_alias .. " localhost:8000 -sSL", command, "Curl alias should be used")
end
local mock_pre = vim.fn.jobstart
vim.fn.jobstart = mocked_jobstart

api.open_curl_tab()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { curl_command })
api.execute_curl()
vim.fn.jobstart = mock_pre
curl.setup({})
end)

it("can set alternative curl alias in runtime", function()
local curl_alias = "/my/cool/curl"
api.set_curl_binary(curl_alias)
test_util.assert_equals(config.get("curl_binary"), curl_alias, "Curl alias should be set")

local curl_command = "curl localhost:8000"

local mocked_jobstart = function(command, _)
test_util.assert_equals(curl_alias .. " localhost:8000 -sSL", command, "Curl alias should be used")
end
local mock_pre = vim.fn.jobstart
vim.fn.jobstart = mocked_jobstart

api.open_curl_tab()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { curl_command })
api.execute_curl()
vim.fn.jobstart = mock_pre
end)
end)

0 comments on commit 1036325

Please sign in to comment.