refactor: commands such as :GoGet runs with new runner
This commit is contained in:
parent
12b01a95ec
commit
640a36a7f2
4 changed files with 59 additions and 50 deletions
|
|
@ -1,43 +0,0 @@
|
||||||
local Job = require "plenary.job"
|
|
||||||
local c = require("gopher.config").commands
|
|
||||||
local u = require "gopher._utils"
|
|
||||||
|
|
||||||
---Run any go commands like `go generate`, `go get`, `go mod`
|
|
||||||
---@param cmd string
|
|
||||||
---@param ... string|string[]
|
|
||||||
return function(cmd, ...)
|
|
||||||
local args = { ... }
|
|
||||||
if #args == 0 then
|
|
||||||
u.deferred_notify("please provice any arguments", vim.log.levels.ERROR)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if cmd == "generate" and #args == 1 and args[1] == "%" then
|
|
||||||
args[1] = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
|
|
||||||
elseif cmd == "get" then
|
|
||||||
for i, arg in ipairs(args) do
|
|
||||||
---@diagnostic disable-next-line: param-type-mismatch
|
|
||||||
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
|
|
||||||
table.remove(args, i)
|
|
||||||
table.insert(args, i, m)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local cmd_args = vim.list_extend({ cmd }, args) ---@diagnostic disable-line: missing-parameter
|
|
||||||
Job:new({
|
|
||||||
command = c.go,
|
|
||||||
args = cmd_args,
|
|
||||||
on_exit = function(_, retval)
|
|
||||||
if retval ~= 0 then
|
|
||||||
u.deferred_notify(
|
|
||||||
"command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval,
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
u.deferred_notify(cmd .. " " .. unpack(cmd_args), vim.log.levels.DEBUG)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
u.deferred_notify("go " .. cmd .. " was success runned", vim.log.levels.INFO)
|
|
||||||
end,
|
|
||||||
}):start()
|
|
||||||
end
|
|
||||||
52
lua/gopher/_utils/runner/gocmd.lua
Normal file
52
lua/gopher/_utils/runner/gocmd.lua
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
local r = require "gopher._utils.runner"
|
||||||
|
local c = require("gopher.config").commands
|
||||||
|
local gocmd = {}
|
||||||
|
|
||||||
|
---@param args string[]
|
||||||
|
---@return string[]
|
||||||
|
local function if_get(args)
|
||||||
|
for i, arg in ipairs(args) do
|
||||||
|
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
|
||||||
|
table.remove(args, i)
|
||||||
|
table.insert(args, i, m)
|
||||||
|
end
|
||||||
|
return args
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param args unknown[]
|
||||||
|
---@return string[]
|
||||||
|
local function if_generate(args)
|
||||||
|
if #args == 1 and args[1] == "%" then
|
||||||
|
args[1] = vim.fn.expand "%"
|
||||||
|
end
|
||||||
|
return args
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param subcmd string
|
||||||
|
---@param args string[]
|
||||||
|
---@return string[]|nil
|
||||||
|
function gocmd.run(subcmd, args)
|
||||||
|
if #args == 0 then
|
||||||
|
error("please provice any arguments", vim.log.levels.ERROR)
|
||||||
|
end
|
||||||
|
|
||||||
|
if subcmd == "get" then
|
||||||
|
args = if_get(args)
|
||||||
|
end
|
||||||
|
|
||||||
|
if subcmd == "generate" then
|
||||||
|
args = if_generate(args)
|
||||||
|
end
|
||||||
|
|
||||||
|
return r.sync(c.go, {
|
||||||
|
args = { subcmd, unpack(args) },
|
||||||
|
on_exit = function(data, status)
|
||||||
|
if not status == 0 then
|
||||||
|
error("gocmd failed: " .. data, vim.log.levels.ERROR)
|
||||||
|
end
|
||||||
|
vim.notify(c.go .. " " .. subcmd .. " successful runned", vim.log.levels.INFO)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
return gocmd
|
||||||
|
|
@ -8,9 +8,9 @@ local runner = {}
|
||||||
|
|
||||||
---@param cmd string
|
---@param cmd string
|
||||||
---@param opts gopher.RunnerOpts
|
---@param opts gopher.RunnerOpts
|
||||||
---@return string
|
---@return string[]|nil
|
||||||
function runner.sync(cmd, opts)
|
function runner.sync(cmd, opts)
|
||||||
local output = ""
|
local output
|
||||||
Job:new({
|
Job:new({
|
||||||
command = cmd,
|
command = cmd,
|
||||||
args = opts.args,
|
args = opts.args,
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
local tags = require "gopher.struct_tags"
|
local tags = require "gopher.struct_tags"
|
||||||
local tests = require "gopher.gotests"
|
local tests = require "gopher.gotests"
|
||||||
local uc = require "gopher._utils.commands"
|
local gocmd = require("gopher._utils.runner.gocmd").run
|
||||||
local gopher = {}
|
local gopher = {}
|
||||||
|
|
||||||
gopher.setup = require("gopher.config").setup
|
gopher.setup = require("gopher.config").setup
|
||||||
|
|
@ -17,19 +17,19 @@ gopher.test_exported = tests.all_exported_tests
|
||||||
gopher.tests_all = tests.all_tests
|
gopher.tests_all = tests.all_tests
|
||||||
|
|
||||||
gopher.get = function(...)
|
gopher.get = function(...)
|
||||||
uc("get", ...)
|
gocmd("get", { ... })
|
||||||
end
|
end
|
||||||
|
|
||||||
gopher.mod = function(...)
|
gopher.mod = function(...)
|
||||||
uc("mod", ...)
|
gocmd("mod", { ... })
|
||||||
end
|
end
|
||||||
|
|
||||||
gopher.generate = function(...)
|
gopher.generate = function(...)
|
||||||
uc("generate", ...)
|
gocmd("generate", { ... })
|
||||||
end
|
end
|
||||||
|
|
||||||
gopher.work = function(...)
|
gopher.work = function(...)
|
||||||
uc("work", ...)
|
gocmd("work", { ... })
|
||||||
end
|
end
|
||||||
|
|
||||||
return gopher
|
return gopher
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue