refactor(installer): use vim.system and add sync mode

This commit is contained in:
Oleksandr Smirnov 2025-02-28 14:41:45 +02:00
parent 40bd95cad8
commit 3dae7153bb
No known key found for this signature in database
2 changed files with 35 additions and 15 deletions

View file

@ -1,6 +1,7 @@
local c = require("gopher.config").commands
local r = require "gopher._utils.runner"
local u = require "gopher._utils"
local log = require "gopher._utils.log"
local installer = {}
local urls = {
@ -10,25 +11,43 @@ local urls = {
iferr = "github.com/koron/iferr",
}
---@param pkg string
local function install(pkg)
local url = urls[pkg] .. "@latest"
r.sync(c.go, {
args = { "install", url },
on_exit = function(data, status)
if not status == 0 then
error("go install failed: " .. data)
---@param opt vim.SystemCompleted
---@param url string
local function handle_intall_exit(opt, url)
if opt.code ~= 0 then
u.deferred_notify("go install failed: " .. url)
log.error("go install failed:", "url", url, "opt", vim.inspect(opt))
return
end
u.notify("installed: " .. url)
end,
})
u.deferred_notify("go install-ed: " .. url)
end
---@param url string
local function install(url)
r.async({ c.go, "install", url }, function(opt)
handle_intall_exit(opt, url)
end)
end
---@param url string
local function install_sync(url)
local rs = r.sync { c.go, "install", url }
handle_intall_exit(rs, url)
end
---Install required go deps
function installer.install_deps()
---@param sync? boolean
function installer.install_deps(sync)
sync = sync or false
for pkg, _ in pairs(urls) do
install(pkg)
local url = urls[pkg] .. "@latest"
if sync then
install_sync(url)
else
install(url)
end
end
end

View file

@ -11,4 +11,5 @@ command! -nargs=* GoGenerate :lua require"gopher".generate(<f-args>)
command! GoCmt :lua require"gopher".comment()
command! GoIfErr :lua require"gopher".iferr()
command! GoInstallDeps :lua require"gopher".install_deps()
command! GoInstallDepsSync :lua require"gopher".install_deps(true)
command! GopherLog :lua vim.cmd("tabnew " .. require("gopher._utils.log").get_outfile())