feat: remove boilerplate code, add go work suport

This commit is contained in:
Smirnov Oleksandr 2022-10-07 17:07:04 +03:00
parent 294902970d
commit 0b1d8fa7c2
5 changed files with 43 additions and 86 deletions

View file

@ -0,0 +1,40 @@
local Job = require "plenary.job"
local c = require("gopher.config").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.notify("please provice any arguments", "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.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
u.notify(cmd .. " " .. unpack(cmd_args), "debug")
return
end
u.notify("go " .. cmd .. " was success runned", "info")
end,
}):start()
end

View file

@ -22,5 +22,8 @@ end
API.generate = function(...)
cmd("generate", ...)
end
API.work = function(...)
cmd("work", ...)
end
return API

View file

@ -1,26 +0,0 @@
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---run "go generate"
return function(...)
local args = { ... }
if #args == 1 and args[1] == "%" then
args[1] = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
end
local cmd_args = vim.list_extend({ "generate" }, args)
Job:new({
command = c.go,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
return
end
u.notify("go generate was success runned", "info")
end,
}):start()
end

View file

@ -1,33 +0,0 @@
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---run "go get"
return function(...)
local args = { ... }
if #args == 0 then
u.notify("please provide a package url to get", "error")
return
end
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
local cmd_args = vim.list_extend({ "get" }, args)
Job:new({
command = c.go,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
return
end
u.notify("go get was success runned", "info")
end,
}):start()
end

View file

@ -1,27 +0,0 @@
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---run "go mod"
return function(...)
local args = { ... }
if #args == 0 then
u.notify("please provide any mod command", "error")
return
end
local cmd_args = vim.list_extend({ "mod" }, args)
Job:new({
command = c.go,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
return
end
u.notify("go mod was success runned", "info")
end,
}):start()
end