diff options
| author | William Boman <william@redwill.se> | 2022-04-12 23:41:12 +0200 |
|---|---|---|
| committer | William Boman <william@redwill.se> | 2022-04-12 23:41:27 +0200 |
| commit | 39f84cd24fac6284212909b391df85297019a09e (patch) | |
| tree | 1c18de8d8bc43dcb793101cd4ebc0e4353f66bdb | |
| parent | add clarity_lsp (#594) (diff) | |
| download | mason-39f84cd24fac6284212909b391df85297019a09e.tar mason-39f84cd24fac6284212909b391df85297019a09e.tar.gz mason-39f84cd24fac6284212909b391df85297019a09e.tar.bz2 mason-39f84cd24fac6284212909b391df85297019a09e.tar.lz mason-39f84cd24fac6284212909b391df85297019a09e.tar.xz mason-39f84cd24fac6284212909b391df85297019a09e.tar.zst mason-39f84cd24fac6284212909b391df85297019a09e.zip | |
feat(fetch): add ability to download file instead of writing to stdout
| -rw-r--r-- | lua/nvim-lsp-installer/core/fetch.lua | 28 | ||||
| -rw-r--r-- | tests/core/fetch_spec.lua | 30 |
2 files changed, 51 insertions, 7 deletions
diff --git a/lua/nvim-lsp-installer/core/fetch.lua b/lua/nvim-lsp-installer/core/fetch.lua index d97da59b..4c0d1f25 100644 --- a/lua/nvim-lsp-installer/core/fetch.lua +++ b/lua/nvim-lsp-installer/core/fetch.lua @@ -12,28 +12,42 @@ local HEADERS = { iwr = ("-Headers @{'User-Agent' = '%s'}"):format(USER_AGENT), } +---@alias FetchOpts {out_file:string} + ---@async ---@param url string @The url to fetch. -local function fetch(url) +---@param opts FetchOpts +local function fetch(url, opts) + opts = opts or {} log.fmt_debug("Fetching URL %s", url) local platform_specific = Result.failure() if platform.is_win then - platform_specific = powershell.command( - ([[Write-Output (iwr %s -UseBasicParsing -Uri %q).Content;]]):format(HEADERS.iwr, url) - ) + if opts.out_file then + platform_specific = powershell.command( + ([[iwr %s -UseBasicParsing -Uri %q; -OutFile %q]]):format(HEADERS.iwr, url, opts.out_file) + ) + else + platform_specific = powershell.command( + ([[Write-Output (iwr %s -UseBasicParsing -Uri %q).Content;]]):format(HEADERS.iwr, url) + ) + end end return platform_specific :recover_catching(function() - return spawn.wget({ HEADERS.wget, "-nv", "-O", "-", url }):get_or_throw() + return spawn.wget({ HEADERS.wget, "-nv", "-O", opts.out_file or "-", url }):get_or_throw() end) :recover_catching(function() - return spawn.curl({ HEADERS.curl, "-fsSL", url }):get_or_throw() + return spawn.curl({ HEADERS.curl, "-fsSL", opts.out_file and { "-o", opts.out_file } or vim.NIL, url }):get_or_throw() end) :map(function(result) - return result.stdout + if opts.out_file then + return result + else + return result.stdout + end end) end diff --git a/tests/core/fetch_spec.lua b/tests/core/fetch_spec.lua index dac7c7b8..b4929167 100644 --- a/tests/core/fetch_spec.lua +++ b/tests/core/fetch_spec.lua @@ -29,6 +29,7 @@ describe("fetch", function() assert.spy(spawn.curl).was_called_with { { "-H", "User-Agent: nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)" }, "-fsSL", + vim.NIL, "https://api.github.com", } end) @@ -46,4 +47,33 @@ describe("fetch", function() assert.equals([[{"data": "here"}]], result:get_or_throw()) end) ) + + it( + "should respect out_file opt", + async_test(function() + stub(spawn, "wget") + stub(spawn, "curl") + spawn.wget.returns(Result.failure "wget failure") + spawn.curl.returns(Result.failure "curl failure") + fetch("https://api.github.com/data", { out_file = "/test.json" }) + + assert.spy(spawn.wget).was_called_with { + { + "--header", + "User-Agent: nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)", + }, + "-nv", + "-O", + "/test.json", + "https://api.github.com/data", + } + + assert.spy(spawn.curl).was_called_with { + { "-H", "User-Agent: nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)" }, + "-fsSL", + { "-o", "/test.json" }, + "https://api.github.com/data", + } + end) + ) end) |
