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 c = require("gopher.config").commands
local r = require "gopher._utils.runner" local r = require "gopher._utils.runner"
local u = require "gopher._utils" local u = require "gopher._utils"
local log = require "gopher._utils.log"
local installer = {} local installer = {}
local urls = { local urls = {
@ -10,25 +11,43 @@ local urls = {
iferr = "github.com/koron/iferr", iferr = "github.com/koron/iferr",
} }
---@param pkg string ---@param opt vim.SystemCompleted
local function install(pkg) ---@param url string
local url = urls[pkg] .. "@latest" local function handle_intall_exit(opt, url)
r.sync(c.go, { if opt.code ~= 0 then
args = { "install", url }, u.deferred_notify("go install failed: " .. url)
on_exit = function(data, status) log.error("go install failed:", "url", url, "opt", vim.inspect(opt))
if not status == 0 then return
error("go install failed: " .. data) end
return
end u.deferred_notify("go install-ed: " .. url)
u.notify("installed: " .. url) end
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 end
---Install required go deps ---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 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
end end

View file

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