diff options
| author | William Boman <william@redwill.se> | 2022-07-08 18:34:38 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-08 18:34:38 +0200 |
| commit | 976aa4fbee8a070f362cab6f6ec84e9251a90cf9 (patch) | |
| tree | 5e8d9c9c59444a25c7801b8f39763c4ba6e1f76d /lua/mason-core/fetch.lua | |
| parent | feat: add gotests, gomodifytags, impl (#28) (diff) | |
| download | mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.gz mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.bz2 mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.lz mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.xz mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.zst mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.zip | |
refactor: add mason-schemas and mason-core modules (#29)
* refactor: add mason-schemas and move generated filetype map to mason-lspconfig
* refactor: add mason-core module
Diffstat (limited to 'lua/mason-core/fetch.lua')
| -rw-r--r-- | lua/mason-core/fetch.lua | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/lua/mason-core/fetch.lua b/lua/mason-core/fetch.lua new file mode 100644 index 00000000..168d0b99 --- /dev/null +++ b/lua/mason-core/fetch.lua @@ -0,0 +1,124 @@ +local log = require "mason-core.log" +local platform = require "mason-core.platform" +local Result = require "mason-core.result" +local spawn = require "mason-core.spawn" +local powershell = require "mason-core.managers.powershell" +local _ = require "mason-core.functional" + +local USER_AGENT = "mason.nvim (+https://github.com/williamboman/mason.nvim)" + +---@alias FetchMethod +---| '"GET"' +---| '"POST"' +---| '"PUT"' +---| '"PATCH"' +---| '"DELETE"' + +---@alias FetchOpts {out_file: string, method: FetchMethod, headers: table<string, string>, data: string} + +---@async +---@param url string @The url to fetch. +---@param opts FetchOpts | nil +local function fetch(url, opts) + opts = opts or {} + if not opts.headers then + opts.headers = {} + end + if not opts.method then + opts.method = "GET" + end + opts.headers["User-Agent"] = USER_AGENT + log.fmt_debug("Fetching URL %s", url) + + local platform_specific = Result.failure() + + if platform.is_win then + local header_entries = _.join( + ", ", + _.map(function(pair) + return ("%q = %q"):format(pair[1], pair[2]) + end, _.to_pairs(opts.headers)) + ) + local headers = ("@{%s}"):format(header_entries) + if opts.out_file then + platform_specific = powershell.command( + ([[iwr %s -UseBasicParsing -Method %q -Uri %q %s -OutFile %q;]]):format( + headers, + opts.method, + url, + opts.data and ("-Body %s"):format(opts.data) or "", + opts.out_file + ) + ) + else + platform_specific = powershell.command( + ([[Write-Output (iwr %s -Method %q -UseBasicParsing %s -Uri %q).Content;]]):format( + headers, + opts.method, + opts.data and ("-Body %s"):format(opts.data) or "", + url + ) + ) + end + end + + return platform_specific + :recover_catching(function() + local headers = + _.sort_by(_.identity, _.map(_.compose(_.format "--header='%s'", _.join ": "), _.to_pairs(opts.headers))) + return spawn + .wget({ + headers, + "-nv", + "-O", + opts.out_file or "-", + ("--method=%s"):format(opts.method), + opts.data and { + ("--body-data=%s"):format(opts.data) or vim.NIL, + } or vim.NIL, + url, + }) + :get_or_throw() + end) + :recover_catching(function() + local headers = _.sort_by( + _.nth(2), + _.map( + _.compose(function(header) + return { "-H", header } + end, _.join ": "), + _.to_pairs(opts.headers) + ) + ) + return spawn + .curl({ + headers, + "-fsSL", + { + "-X", + opts.method, + }, + opts.data and { "-d", "@-" } or vim.NIL, + opts.out_file and { "-o", opts.out_file } or vim.NIL, + url, + on_spawn = function(_, stdio) + local stdin = stdio[1] + if opts.data then + log.trace("Writing stdin to curl", opts.data) + stdin:write(opts.data) + end + stdin:close() + end, + }) + :get_or_throw() + end) + :map(function(result) + if opts.out_file then + return result + else + return result.stdout + end + end) +end + +return fetch |
