From 0b1d8fa7c2430402ee26b43b77eef64e2cf7c8e5 Mon Sep 17 00:00:00 2001 From: Smirnov Oleksandr Date: Fri, 7 Oct 2022 17:07:04 +0300 Subject: [PATCH] feat: remove boilerplate code, add go work suport --- lua/gopher/_utils/commands.lua | 40 ++++++++++++++++++++++++++++++++++ lua/gopher/api.lua | 3 +++ lua/gopher/gogenerate.lua | 26 ---------------------- lua/gopher/goget.lua | 33 ---------------------------- lua/gopher/gomod.lua | 27 ----------------------- 5 files changed, 43 insertions(+), 86 deletions(-) create mode 100644 lua/gopher/_utils/commands.lua delete mode 100644 lua/gopher/gogenerate.lua delete mode 100644 lua/gopher/goget.lua delete mode 100644 lua/gopher/gomod.lua diff --git a/lua/gopher/_utils/commands.lua b/lua/gopher/_utils/commands.lua new file mode 100644 index 0000000..4f5a59a --- /dev/null +++ b/lua/gopher/_utils/commands.lua @@ -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 diff --git a/lua/gopher/api.lua b/lua/gopher/api.lua index f708c52..6e854af 100644 --- a/lua/gopher/api.lua +++ b/lua/gopher/api.lua @@ -22,5 +22,8 @@ end API.generate = function(...) cmd("generate", ...) end +API.work = function(...) + cmd("work", ...) +end return API diff --git a/lua/gopher/gogenerate.lua b/lua/gopher/gogenerate.lua deleted file mode 100644 index 5df4390..0000000 --- a/lua/gopher/gogenerate.lua +++ /dev/null @@ -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 diff --git a/lua/gopher/goget.lua b/lua/gopher/goget.lua deleted file mode 100644 index 2740179..0000000 --- a/lua/gopher/goget.lua +++ /dev/null @@ -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 diff --git a/lua/gopher/gomod.lua b/lua/gopher/gomod.lua deleted file mode 100644 index f53ba28..0000000 --- a/lua/gopher/gomod.lua +++ /dev/null @@ -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